PDA

View Full Version : Can Not Set or Get the ItemId of Listview



meral
09-04-2012, 08:19 AM
Hello all,

on item click of listview I populated the following 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

cali
09-04-2012, 09:50 AM
Welcome to the site. Hope someone can help with this.

meral
09-17-2012, 05:14 AM
Hello, Noone is there to help me !!! :superstition: Please....

chakdev
10-09-2012, 09:08 PM
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)




ArrayList<HashMap<String,String>> myList;

public View getView(int position, View (http://developer.android.com/reference/android/view/View.html) convertView, ViewGroup (http://developer.android.com/reference/android/view/ViewGroup.html) 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:


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();
}
});