我如何知道在ListView中单击了哪个按钮

时间:2016-11-16 12:57:36

标签: android listview

我创建了一个自定义ListView适配器。在getView中,我有另一种覆盖方法onClick。我的问题是如何知道ListView中的哪个按钮被点击了?

item_category

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical" android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/specialCatItemName"
        android:textColor="#000"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="View"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnSpecialView"
        android:textAllCaps="false"
        android:padding="3dp"
        android:background="#CCBA56"
        android:textColor="#fff"
        android:minHeight="40dp"
        android:layout_alignBaseline="@+id/btnSpecialImages"
        android:layout_alignBottom="@+id/btnSpecialImages"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="15dp"/>

    <Button
        android:text="Images"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnSpecialImages"
        android:textAllCaps="false"
        android:paddingTop="2dp"
        android:paddingRight="2dp"
        android:paddingBottom="2dp"
        android:paddingLeft="2dp"
        android:background="#011E4D"
        android:textColor="#fff"
        android:minHeight="40dp"
        android:layout_marginTop="11dp"
        android:layout_below="@+id/specialCatItemName"
        android:layout_toRightOf="@+id/btnSpecialView"
        android:layout_toEndOf="@+id/btnSpecialView"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginBottom="15dp"/>

</RelativeLayout>

光标适配器

 class categoryCursorAdaptor extends BaseAdapter {

    Context context;
    String[] data;
    private static LayoutInflater inflater = null;
    int gPosition;
    String row;

    public categoryCursorAdaptor(Context context, String[] data) {
        this.context = context;
        this.data = data;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.length;
    }

    @Override
    public Object getItem(int position) {
        return data[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        this.gPosition = position;
        this.row = data[position];
        if (vi == null) vi = inflater.inflate(R.layout.item_category, null);
        TextView text = (TextView) vi.findViewById(R.id.specialCatItemName);
        text.setText(data[position]);

        Button Button1= (Button) vi.findViewById(R.id.btnSpecialView);
        Button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
               //How do I know which button is got clicked?
            }
        });
        return vi;
    }
}

6 个答案:

答案 0 :(得分:1)

@Devil Raily您可以使用setTag和getTag方法获取位置。使用这样的代码

Button Button1= (Button) vi.findViewById(R.id.btnSpecialView);
Button1.setTag(position);
Button1.setOnClickListener(new View.OnClickListener(){
            @Override 
            public void onClick(View view) {
               //How do I know which button is got clicked? 
                int position = (Integer)view.getTag();
            } 
        });

答案 1 :(得分:1)

 Button Button1= (Button) vi.findViewById(R.id.btnSpecialView);
 Button1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {

        if(gPosition ==1){
              // btn 2 was clicked 
         }
      else if(gPosition ==2){
          // btn 3 was clicked
        }
           // and so on

        }
    });

请注意,位置值从0开始

答案 2 :(得分:1)

也许您可以在活动中实施View的OnClickListner ...覆盖点击方法。

Button b1= (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);

完整代码:

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1= (Button) findViewById(R.id.button1);
    Button b2= (Button) findViewById(R.id.button2);
    Button b3= (Button) findViewById(R.id.button3);
    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
    b3.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.button1 :
        Toast.makeText(MainActivity.this,"button1", 1000).show();
        break;
    case R.id.button2 :
        Toast.makeText(MainActivity.this,"button2", 1000).show();
        break;
    case R.id.button3 :
        Toast.makeText(MainActivity.this,"button3", 1000).show();
        break;  


    }

}
 }

答案 3 :(得分:1)

替换

 Button1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
           //How do I know which button is got clicked?
        }
    });

使用:

 Button1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
           //How do I know which button is got clicked?
           onMyButtonClicked(position, view);
        }
    });

并实施

void onMyButtonClicked(int position, View view){
  //DoStuff
}

答案 4 :(得分:0)

设置适配器时,您需要在活动中实施点击。像这样:

adapter = new categoryCursorAdaptor(yourListBase, this);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            adapter.getItem(position); // return the object from the position of your list

        }
    });

答案 5 :(得分:0)

//我怎么知道点击了哪个按钮?

添加以下内容:

int clickedButton = view.getId();

开关(clickedButton){

   case R.id.btnSpecialView : 

      // btnSpecialView is clicked - Do something break; 

      break;

   case R.id.btnSpecialImages : 

       // btnSpecialImages is clicked - Do something break;

       break;

}