I have sucessfully got the data from my server and displayed it in my ListView and now i want to implement Click event in the ListView Items. For now i am displaying the "id" and "SomeText" in my List view.
My Question is How to implement a Click event in the ListView in such a way that if i click one particular row, it would display the "id" from the server(not from the actual array which starts from "0") in the toast message.
Below is the Code and screen shot of what i am getting when the list items are being clicked.
I hope someone could help me out with this one; any link or a snippet of code would be very helpful.
Thank you soo much. Christine
SCREENSHOT
CODE public class Welcome extends Activity {
public static String token;
String nam;
String dat;
String name;
JSONArray tasks;
long id2;
TextView textView;
ListView lvList;
private ArrayAdapter<String>mListData;
String emailAdd = "rohan@bbtdigital.com";
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
//======================================================================================================
//======================== GETTING THE VALUE FROM THE SHARED PREF ======================================
SharedPreferences sharedpreferences = getSharedPreferences(Login.MyPREFERENCES, Context.MODE_PRIVATE);
token = sharedpreferences.getString("tokenKey","");
//Log.e("TOKEN", token);
//======================================================================================================
//=====================================================================================================
lvList = (ListView) findViewById(R.id.chat_drawer);
textView = (TextView) findViewById(R.layout.msg_chat);
mListData = new ArrayAdapter<String>(this, R.layout.msg_chat);
lvList.setAdapter(mListData);
mListData.setNotifyOnChange(true);
historyMessage();
}
private void historyMessage() {
// TODO Auto-generated method stub
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
try{
HttpClient client = new DefaultHttpClient();
String SetServerString = "";
HttpGet httpget = new HttpGet("http://xxxx.xxx.com/api/v1/agent-tasks?token="+token);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
SetServerString = client.execute(httpget, responseHandler);
//Log.e("LIST vIEW ", SetServerString);
HttpResponse mike = client.execute(httpget);
HttpEntity entit = mike.getEntity();
dat = EntityUtils.toString(entit);
//Log.e("STRING From ", dat);
try{
JSONObject responseObject = new JSONObject(SetServerString);
JSONArray responseArray = responseObject.getJSONArray("response");
JSONObject firstResponse = responseArray.getJSONObject(0);
tasks = firstResponse.getJSONArray("tasks");
//System.out.println("asdfasdfsadf" + tasks);
runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < tasks.length(); i++){
try{
JSONObject task = tasks.getJSONObject(i);
id2 = task.getInt("id");
name = task.getString("name");
String fulldata = id2 + "." + " " + name;
mListData.add(fulldata);
//mListData.notifyDataSetChanged();
ListView lvList = (ListView) findViewById(R.id.chat_drawer);
lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
long id2) {
// TODO Auto-generated method stub
TextView textView = (TextView) viewClicked;
String message = "you clicked #"+id2+" "+ ",which is string:"+ " " +textView.getText().toString();
Toast.makeText(Welcome.this, message, Toast.LENGTH_LONG).show();
}
});
}
catch (JSONException e){
e.printStackTrace();
}
}
}
});
} catch (JSONException e){
e.printStackTrace();
}
}catch (ClientProtocolException e){
Log.d("HTTPCLIENT", e.getLocalizedMessage());
} catch (IOException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
}
}
}).start();
}