Android 为ListView列表项中的按钮分配onClick事件
我创建了一个从数据库填充的Android 为ListView列表项中的按钮分配onClick事件,android,listview,android-listview,Android,Listview,Android Listview,我创建了一个从数据库填充的列表视图。每行都有一个按钮,用于更新列表和数据库中的项目值。我想为列表视图项目中使用的按钮添加onClick事件。如何为列表项的按钮分配OnClickListener addtooutlet.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androi
列表视图
。每行都有一个按钮,用于更新列表和数据库中的项目值。我想为列表视图
项目中使用的按钮添加onClick事件。如何为列表项的按钮分配OnClickListener
addtooutlet.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:padding="5dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="46dp"
android:background="#ffffff"
android:padding="5dip"
android:weightSum="100" >
<TextView
android:id="@+id/item_bname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="25"
android:padding="2dp"
android:text="Hello"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="12dip" />
<TextView
android:id="@+id/item_bid"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="25"
android:padding="2dp"
android:text="Hello"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="12dip" />
<Button
android:id="@+id/item_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/item_bname"
android:layout_marginRight="16dp"
android:layout_weight="50"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="2dp"
android:text="Join"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="12dip" />
</LinearLayout>
</LinearLayout>
addtooutlet\u list\u item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:padding="5dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="46dp"
android:background="#ffffff"
android:padding="5dip"
android:weightSum="100" >
<TextView
android:id="@+id/item_bname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="25"
android:padding="2dp"
android:text="Hello"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="12dip" />
<TextView
android:id="@+id/item_bid"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="25"
android:padding="2dp"
android:text="Hello"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="12dip" />
<Button
android:id="@+id/item_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/item_bname"
android:layout_marginRight="16dp"
android:layout_weight="50"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="2dp"
android:text="Join"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="12dip" />
</LinearLayout>
</LinearLayout>
AddTooutlet.java
public class AddToOutlet extends ListActivity {
SessionManager session;
String success, cus_id, bus_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addtooutlet);
session = new SessionManager(getApplicationContext());
session.checkLoginback();
// get user data from session
HashMap<String, String> user = session.getUserDetails();
// ID
final String cus_id = user.get(SessionManager.KEY_ID);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("cus_id", cus_id));
String response = null;
try {
response=LoginHttpClient.executeHttpPost("http://10.0.2.2/android_api/add_to_outlet.php",postParameters);
JSONObject json = new JSONObject(response);
JSONArray jArray = json.getJSONArray("customer");
for (int i = 0; i < jArray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject json_data = jArray.getJSONObject(i);
map.put("bus_name", json_data.getString("bus_name"));
map.put("bus_id", json_data.getString("bus_id"));
success = json_data.getString("success");
mylist.add(map);
}
} catch (Exception e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist,
R.layout.addtooutlet_list_item, new String[] { "bus_name",
"bus_id" },
new int[] { R.id.item_bname, R.id.item_bid });
setListAdapter(adapter);
}
public class Myadapter extends ArrayAdapter<String> {
private OnClickListener callback;
public Myadapter(Context context, int resource, int textViewResourceId,
String[] strings, OnClickListener callback) {
super(context, resource, textViewResourceId, strings);
this.callback = callback;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.addtooutlet_list_item, parent,
false);
Button buttonEdit = (Button) row.findViewById(R.id.item_button);
buttonEdit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Log.i("xx", "Button pressed!");
}
});
return convertView;
}
private LayoutInflater getLayoutInflater() {
// TODO Auto-generated method stub
return null;
}
}
}
公共类AddToOutlet扩展ListActivity{
会话管理器会话;
字符串成功,客户id,总线id;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.addtooutlet);
session=newsessionmanager(getApplicationContext());
session.checkLoginback();
//从会话获取用户数据
HashMap user=session.getUserDetails();
//身份证
最后一个字符串cus_id=user.get(SessionManager.KEY_id);
ArrayList mylist=新的ArrayList();
ArrayList后参数=新的ArrayList();
添加(新的BasicNameValuePair(“cus_id”,cus_id));
字符串响应=null;
试一试{
响应=LoginHttpClient.executeHttpPost(“http://10.0.2.2/android_api/add_to_outlet.php“,后参数);
JSONObject json=新的JSONObject(响应);
JSONArray jArray=json.getJSONArray(“客户”);
for(int i=0;i
您有
View row = inflater.inflate(R.layout.addtooutlet_list_item, parent,
false);
但是你回来了
return convertView;
应该是
View convertView = inflater.inflate(R.layout.addtooutlet_list_item, parent,
false);
及
及
编辑:
正如布莱克贝尔在评论中所建议的那样。您没有使用自定义ArrayAdapter
你可能是说
Myadapter adapter = new MyAdapter(ActivityName.this,R.layout.addtooutlet_list_item,mylist);
setListAdapter(adapter);
然后
现在您应该在layout.addtooutlet\u list\u item.xml中有一个textview,并在getView中更新视图
也考虑使用WistHoad模式
你有
View row = inflater.inflate(R.layout.addtooutlet_list_item, parent,
false);
但是你回来了
return convertView;
应该是
View convertView = inflater.inflate(R.layout.addtooutlet_list_item, parent,
false);
及
及
编辑:
正如布莱克贝尔在评论中所建议的那样。您没有使用自定义ArrayAdapter
你可能是说
Myadapter adapter = new MyAdapter(ActivityName.this,R.layout.addtooutlet_list_item,mylist);
setListAdapter(adapter);
然后
现在您应该在layout.addtooutlet\u list\u item.xml中有一个textview,并在getView中更新视图
也考虑使用WistHoad模式
但他没有使用MyAdapter。他正在使用一个SimpleAdapter。但是,如果你指出的是问题所在,他的应用程序崩溃了。还有一件小事,你可以建议他避免每次崩溃时都膨胀convertView@Amardeep看看编辑过的代码。唯一缺少的是一个ViewHolder@Raghunandan,+1表示高度合作。@Amardeep进一步编辑以添加视图持有者。在addtooutlet\u list\u item.xml
中有两个文本视图和一个按钮。rest所有复制粘贴,但他没有使用MyAdapter。他正在使用一个SimpleAdapter。但是,如果你指出的是问题所在,他的应用程序崩溃了。还有一件小事,你可以建议他避免每次崩溃时都膨胀convertView@Amardeep看看编辑过的代码。唯一缺少的是一个ViewHolder@Raghunandan,+1表示高度合作。@Amardeep进一步编辑以添加视图持有者。在addtooutlet\u list\u item.xml
中有两个文本视图和一个按钮。rest所有复制粘贴发布MyAdapter类发布MyAdapter类