-
Android Lurker
Can Not Set or Get the ItemId of Listview
Hello all,
on item click of listview I populated the following code
Code:
public class Ws extends Activity {
Button btn;
ArrayAdapter<String> aa;
ArrayList<String> al;
SimpleAdapter sa;
ArrayList<HashMap<String, String>> mylist;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ws);
lv = (ListView) findViewById(R.id.listView1);
mylist = new ArrayList<HashMap<String,String>>();
al = new ArrayList<String>();
HttpClient httpclient = new DefaultHttpClient();
try {
String responseBody = httpclient.execute(new HttpPost("http://mydomain/userlist.php"),new BasicResponseHandler());
JSONArray jArray = new JSONArray(responseBody);
for (int i = 0; i < jArray.length(); i++) {
JSONObject e = jArray.getJSONObject(i);
//al.add(e.getString("name"));
// Toast.makeText(this, e.getString("name"), Toast.LENGTH_SHORT).show();
HashMap<String, String> map = new HashMap<String, String>();
map.put("_id", e.getString("id"));
map.put("c1", e.getString("name"));
map.put("c2", e.getString("city"));
map.put("c3", e.getString("birthdate"));
mylist.add(map);
}
String[] ColumnName = new String[] { "c1", "c2", "c3" };
int[] ColumnId = new int[] {R.id.colm1, R.id.colm2, R.id.colm3};
sa = new SimpleAdapter(this, mylist, R.layout.custom_layout, ColumnName, ColumnId);
lv.setAdapter(sa);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(Ws.this, String.valueOf(arg2), Toast.LENGTH_SHORT).show();
Toast.makeText(Ws.this, String.valueOf(arg3), Toast.LENGTH_SHORT).show();
}
});
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
But here both arg2 & arg3 giving me the same value that is position of list. But I need the id that I have set as _id in my mylist ArrayList
-
09-04-2012 08:19 AM
# ADS
Google Advertisement
-
Super Moderator
Welcome to the site. Hope someone can help with this.
-
Android Lurker
Hello, Noone is there to help me !!!
Please....
-
Android Lurker
Hi Meral,
I hope you have already found a solution to your problem.
To overcome your problem, you'll need to have a custom Adapter class.
In the getView() method of your custom Adapter class, tag the view with your _id from myList.
Something like this (Warning: pseudo-code. Most probably will not compile)
Code:
ArrayList<HashMap<String,String>> myList;
public View getView(int position, View convertView, ViewGroup parent) {
View newView= inflater.inflate(R.layout.custom_layout, null, false);
newView.setTag(myList.get(position).get("_id"));
}
Then to retrieve _id in your onItemClick definition:
Code:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int _id = (int) arg1.getTag();
Toast.makeText(Ws.this, String.valueOf(arg2), Toast.LENGTH_SHORT).show();
Toast.makeText(Ws.this, String.valueOf(arg3), Toast.LENGTH_SHORT).show();
}
});