I have created a ListView
that is populated from a database.Each row has a button to update the item value from the list and the database.I want to add onClick event for buttons used in item of ListView
. How can I assign an OnClickListener for buttons for list items ?
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>
addtooutlet_list_item.xml
<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;
}
}
}