本文介绍了带按钮的Android Listview获取项目位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有按钮的列表视图.有什么方法可以检查单击了哪个按钮以及列表视图的哪个位置?
I have a listview with a button in it. is there any possible way to check which button was clicked and in which position of the listview ?
这是我的代码.
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" />
任何建议都非常有用.干杯.
Any advice is much appriciated.Cheers.
推荐答案
尝试在适配器的getView
方法
注意::由于您没有发布foodlist.xml,因此请更改"R.id.buttonId"以引用您在foodlist.xml中为按钮的android:id属性设置的ID值
NOTE: since you did not post your foodlist.xml, please change "R.id.buttonId" to reference the ID value you set for the button's android:id attribute in your foodlist.xml
// 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
这篇关于带按钮的Android Listview获取项目位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!