Android Listview with Button get Item position

时间:2015-10-16 16:19:20

标签: android listview button

我有一个列表视图,里面有一个按钮。有没有办法检查点击了哪个按钮以及列表视图的位置?

enter image description here

这是我的代码。

    final RelativeLayout layoutFooter2 = (RelativeLayout) getLayoutInflater().inflate(R.layout.foodlist, null);
    final Button btnBack = (Button) layoutFooter2.findViewById(R.id.Back);
    // buttonadd
    final Button btnadd = (Button) layoutFooter2.findViewById(R.id.btnaddd);
    // buttondelete
    final ListView listView = getListView();
    final TextView txvSum = (TextView) layoutFooter.findViewById(R.id.Sum);
    txvSum.setText("");
    listView.invalidateViews();
    listView.setTextFilterEnabled(true);
    listView.addFooterView(layoutFooter);
    listView.setAdapter(getListAdapter());
    // changes have been done here in order to fit the buttons in textview55+ changes to large/foodlist.
    setListAdapter(new ArrayAdapter<Object>(this, R.layout.foodlist, R.id.textview55, ListItemSalads));
    getWindow().getDecorView().setBackgroundColor(Color.BLACK);

    btnadd.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {

            Toast.makeText(ActivityFoodsSalads.this, "Button Clicked", Toast.LENGTH_SHORT).show();

        }
    });

xml文件

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
           android:id="@+id/kati" 
        > 

    <TextView
  android:id="@+id/textview55" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"
  android:background="@drawable/listviewborder"
  android:textColor="#FFFFFF"
  android:padding="12dp"
  android:textSize="24sp" >
  </TextView>


     <Button
        android:id="@+id/btnaddd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="112dp"
        android:alpha="1"
        android:background="#3b3974"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:minWidth="90dp"
        android:text="+"
        android:textColor="#f1ecef" />

任何建议都非常适合。欢呼。

4 个答案:

答案 0 :(得分:0)

我建议您删除TextView中的按钮,然后使用ItemClickListener收听ListView内的点击。

尝试如下

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
          // TODO Auto-generated method stub
          Log.d("Item clicked..position: ",""+arg2);     
   }

});

答案 1 :(得分:0)

尝试在适配器的getView方法

中设置点击监听器

注意:,因为您没有发布您的foodlist.xml,请更改“R.id.buttonId”以引用您为foodlist.xml中按钮的android:id属性设置的ID值

// changes have been done here in order to fit the buttons in textview55+ changes to large/foodlist.
setListAdapter(new ArrayAdapter<ListItemSalads>(this, R.layout.foodlist, R.id.textview55, ListItemSalads) {
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View inflatedView = super.getView(position, convertView, parent);

        // set a click listener 
        // TODO change "R.id.buttonId" to reference the ID value you set for the button's android:id attribute in foodlist.xml
        inflatedView.findViewById(R.id.buttonId).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 Toast.makeText(v.getContext(), "Button 1  clicked for row position=" + position, Toast.LENGTH_SHORT).show();
            }
        });
        return inflatedView;

    }
});
getWindow().getDecorView().setBackgroundColor(Color.BLACK);
//btnadd.setOnClickListener is not needed anymore

答案 2 :(得分:0)

在布局中,设置按钮的onclick定义为onClickItemButton。

 public void onClickItemButton(View view) {
    View item = (View) view.getParent();
    int pos = listView.getPositionForView(item);
    long id = listView.getItemIdAtPosition(pos);
 }

答案 3 :(得分:-2)

您可以为按钮设置操作。如果,您为listview设置了动作侦听器,则无法获得所选位置