我正在使用列表视图中的按钮...我的xml代码...
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="false"
android:layout_alignParentEnd="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/patient_info"
android:id="@+id/textView"
android:layout_alignParentTop="false"
android:textSize="30sp"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_toLeftOf="@+id/button3"
android:layout_toStartOf="@+id/button3"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add"
android:id="@+id/button1"
android:layout_alignParentRight="false"
android:layout_alignParentEnd="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sync"
android:id="@+id/button3"
android:layout_alignParentTop="false"
android:layout_toLeftOf="@+id/button1"
android:layout_toStartOf="@+id/button1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<ProgressBar
style="@android:style/Widget.Material.Light.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/progressBar2" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText11"
android:hint="Old Registration Number" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText10"
android:hint="New Registartion Number" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText7"
android:hint="Name" />
<Button
android:text="search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="SEARCH RESULTS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:textSize="20sp"
android:gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
</LinearLayout>
如何处理listview中的每个按钮的点击?活动类
String[] from = new String[]{DBHelper.ID, DBHelper.FNAME, DBHelper.LNAME, DBHelper.ADDRESS, DBHelper.REGID};
int[] to = new int[]{R.id.id,R.id.fname,R.id.lname,R.id.address,R.id.regid};
adapter = new SimpleCursorAdapter(this, R.layout.list, cursor, from, to, 0);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//listview click event handling
id = (TextView) arg1.findViewById(R.id.id);
int id_To_Search = Integer.valueOf(id.getText().toString());
Bundle dataBundle = new Bundle();
dataBundle.putInt("_id", id_To_Search);
Intent intent = new Intent(getApplicationContext(), AddPatient.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});我如何管理列表视图中每一项的按钮点击?我尝试了很多方法..如何解决此问题。?请任何人帮帮我。
发布于 2016-12-16 20:04:11
尝尝这个,
public View getView(final int position, View convertView,ViewGroup parent)
{
if(convertView == null)
{
LayoutInflater inflater = getLayoutInflater();
convertView = (LinearLayout)inflater.inflate(R.layout.YOUR_LAYOUT, null);
}
Button Button1= (Button) convertView .findViewById(R.id.BUTTON1_ID);
Button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// Your code that you want to execute on this button click
}
});
return convertView ;
}它可能会帮助你..。
发布于 2016-12-16 19:59:35
在listview适配器中,像正常一样初始化按钮并设置onclicklistener。
看一下这个,所以回答更多信息:Android: ListView elements with multiple clickable buttons
发布于 2016-12-16 21:45:20
请参阅以下链接
http://androidforbeginners.blogspot.in/2010/03/clicking-buttons-in-listview-row.html
对于你的问题,Athul (https://stackoverflow.com/users/6201929/athul)的回答是最好的。
否则,如果您需要活动上的事件操作。
请参阅以下内容
public class UsersAdapter extends BaseAdapter {
Context context;
OnClickButtonOnList onClickButtonOnList;
public UsersAdapter(Context context, ArrayList<User> users, OnClickButtonOnList onClickButtonOnList) {
this.context = context;
this.users = users;
this.onClickButtonOnList = onClickButtonOnList;
}
public View getView(final int position, View convertView,ViewGroup parent)
{
if(convertView == null)
{
LayoutInflater inflater = getLayoutInflater();
convertView = (LinearLayout)inflater.inflate(R.layout.YOUR_LAYOUT, null);
}
Button Button1= (Button) convertView .findViewById(R.id.BUTTON1_ID);
Button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
onClickButtonOnList.onClickButtonInItem(position);
}
});
return convertView ;
}
public interface OnClickButtonOnList {
public void onClickButtonInItem(int position);
}
}活动
UsersAdapter arrayadapter = new UsersAdapter(this, users, this);
listview.setAdapter(arrayadapter);按Alt + Enter
将创建onClickButtonInItem侦听器。你可以在这个位置上做任何事。
https://stackoverflow.com/questions/41183905
复制相似问题