带有按钮获取项目位置的Android Listview
我有一个带有按钮的列表视图。是否有任何可能的方法来检查单击了哪个按钮以及listview的哪个位置 这是我的密码带有按钮获取项目位置的Android Listview,android,listview,button,Android,Listview,Button,我有一个带有按钮的列表视图。是否有任何可能的方法来检查单击了哪个按钮以及listview的哪个位置 这是我的密码 final RelativeLayout layoutFooter2 = (RelativeLayout) getLayoutInflater().inflate(R.layout.foodlist, null); final Button btnBack = (Button) layoutFooter2.findViewById(R.id.Back); //
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" />
任何建议都是非常宝贵的。干杯。我建议您删除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);
}
});
尝试在适配器的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
您可以设置按钮的操作。如果为listview设置操作侦听器,则无法在布局中获取选定位置,请将按钮的onclick设置为onClickItemButton
public void onClickItemButton(View view) {
View item = (View) view.getParent();
int pos = listView.getPositionForView(item);
long id = listView.getItemIdAtPosition(pos);
}
一个建议是将您尝试的代码、适配器代码和listview项/行xml添加到问题中好的,我现在就这样做,谢谢各位。添加foodlist.xml listview项/行xml工具让我试试这个@Lal。谢谢你的建议。如果我在listview的每一行添加两个按钮会怎么样?因为只有一个textview@Lal,我怎么知道单击了哪一个呢?我试过你说的,但是当我在一行内单击按钮@Lal时,日志发生了。@Kristo正如我在回答中提到的,从列表视图中删除按钮,只需单击列表视图中的项目。请检查我的xml。我不想删除按钮,这就是重点@Lal.Hey@Petey谢谢你的帖子,我已经用xml文件更新了我的问题。让我试试这个解决办法。