2013-02-21 83 views
0

我有一個ListView其中有3個按鈕。所有這些都可以點擊。這是一個自定義ListView與一個單獨的CustomAdapter類。Android Listview with clickable buttons

我想點擊一個按鈕,將它another activity和第二個按鈕將remove the row從列表

我該怎麼辦呢?請幫忙。

+0

使用自定義列表視圖我認爲它會幫助您 – 2013-02-21 06:14:32

+0

我已經使用自定義列表視圖,使我的按鈕焦點屬性爲false,可以在單擊按鈕時在日誌中打印消息,但它是在公共適配器類中,我不能因爲它是一個單獨的類,因此也不能生成將其發送給其他活動的意圖。 – Sanghita 2013-02-21 06:32:32

+0

你可以請atleat告訴我如何從列表中刪除一行時,其中的按鈕被點擊? – Sanghita 2013-02-21 06:47:00

回答

0

通過使用此方法,您可以獲得Listview項目position.and然後無論您需要設置。

public void onListItemClick(ListView parent, View v, int position,long id) { selection.setText(items[position]); }

我希望這將幫助你:)

0
For this we have to customize the layout instead of listview. it is not possible with listview because listview takes items so individual onclick methods developing is not possible.see the following code. it will help you alot. 

Activity: 

package com.example.customlistexample; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.os.Bundle; 
import android.util.TypedValue; 
import android.view.Gravity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TableLayout.LayoutParams; 
import android.widget.TextView; 

public class SearchResults extends Activity { 
    /** Called when the activity is first created. */ 



    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     /** create a new layout to display the view */ 
     final LinearLayout mainLinearLayout = (LinearLayout) findViewById(R.id.mainLinearLayout); 

     String listData[] = new String[]{"A","B","C","D","E","F","G"}; 





      for (int i = 0; i < listData.length; i++) { 

       TextView tv1 = new TextView(this); 
       tv1.setText(listData[i]); 
       tv1.setLayoutParams(new LayoutParams(50,50,Gravity.LEFT)); 
       tv1.setTextColor(Color.BLACK); 
       tv1.setTypeface(null, Typeface.BOLD); 
       tv1.setTextSize(TypedValue.COMPLEX_UNIT_PX, 22); 


        ImageView iv1 = new ImageView(this); 
        iv1.setLayoutParams(new LayoutParams(50, 50,Gravity.LEFT)); 
        iv1.setImageResource(R.drawable.ic_launcher); 




        ImageView iv2 = new ImageView(this); 
        iv2.setImageResource(R.drawable.ic_launcher); 
        iv2.setLayoutParams(new LayoutParams(50,50,Gravity.LEFT)); 




        final LinearLayout ll3 = new LinearLayout(this); 
        ll3.setOrientation(LinearLayout.HORIZONTAL); 
        ll3.setLayoutParams(new LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.WRAP_CONTENT,Gravity.CENTER_HORIZONTAL)); 
        ll3.addView(tv1); 
        ll3.addView(iv1); 
        ll3.addView(iv2); 
        ll3.setPadding(0, 20, 0, 25); 


        mainLinearLayout.addView(ll3); 

        final LinearLayout ll5 = new LinearLayout(this); 
        ll5.setLayoutParams(new LayoutParams(1, 1)); 
        ll5.setBackgroundColor(Color.parseColor("#CCCCCC")); 
        mainLinearLayout.addView(ll5); 
        mainLinearLayout.setPadding(10, 10, 10, 30); 

        iv1.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 
          // TODO Auto-generated method stub 
          startActivity(new Intent(SearchResults.this,SecondActivity.class)); 

         } 
        }); 
        iv2.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 
          // TODO Auto-generated method stub 
          mainLinearLayout.removeView(ll3); 
          mainLinearLayout.removeView(ll5); 
         } 
        }); 
       } 
      } 

} 

Layout: 


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".SearchResults" > 

<ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 


     <LinearLayout 
      android:id="@+id/mainLinearLayout" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="left" 
      android:orientation="vertical"> 


      <TextView 
       android:id="@+id/tv_results" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Results" 
       android:layout_marginTop="10dp" 
       android:layout_marginBottom="10dp" 
       android:layout_marginLeft="10dp" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:textColor="#800000" 
       android:textSize="22px" 
       android:textStyle="bold" > 
      </TextView> 
     </LinearLayout> 
    </ScrollView> 

</RelativeLayout> 

try this definately helps you. Let me know your doubt is clarified or not. 
0

謝謝大家對你的答案,我已經理解了它這樣的:

爲了使按鈕之一一排帶我到另一個活動 -

我創建的主要活動內部的專用適配器類,當我那個按鈕,它onClickListener我點擊在「活動」中提到另一個公共函數,它會激發另一個活動的意圖。

但我仍然不確定如何刪除一行點擊其中的一個按鈕。