我已成功从我的服务器获取数据并将其显示在我的ListView中,现在我想在ListView项目中实现Click事件。现在我正在显示" id"和#34; SomeText"在我的列表视图中。
我的问题是如何在ListView中实现Click事件,如果我单击一个特定的行,它将显示" id"在toast消息中从服务器(而不是从" 0"开始的实际数组)。
以下是点击列表项目时所获得的代码和屏幕截图。
我希望有人可以帮我解决这个问题;任何链接或代码片段都会非常有用。
非常感谢你。 恭
SCREENSHOT
CODE 公共类欢迎扩展活动{
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();
}
答案 0 :(得分:1)
您应该实现自定义基本适配器 See where how。将每个元素(id和name)绑定到列表视图行,而不是简单的ArrayAdapter
。
我还建议将服务器的响应包装在java对象中:
public class AgentTask {
private int id;
private String name;
// getters and setters
}
然后,当您调用onItemClick
方法时,您将拥有AgentTask
的实例,而不是简单的字符串。
希望有所帮助
答案 1 :(得分:1)
我想说除了String名称之外你无法获得其他信息的主要原因是因为除了适配器列表中的String名称之外没有其他信息。因此,您需要具有与此模型对应的合适数据模型和适配器。使用ArrayAdapter是完全没问题的。该片段将如下所示:
class Data {
String name;
int id;
public Data(String name, int id){
this.name = name;
this.id = id;
}
}
class ViewHolder {
TextView msg;
public ViewHolder(View convertView){
msg = (TextView)findViewById(R.id.msgid);//appropriate text view element
// from R.layout.msg_chat
}
}
class MyAdapter extends ArrayAdapter<Data>{
public MyAdapter(Context context) {
super(context, R.layout.msg_chat);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.msg_chat, parent, false);
ViewHolder viewHolder = new ViewHolder(convertView);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
Data data = getItem(position);
holder.msg.setText("The id is" + data.id);
return convertView;
}
}
实例化:
private MyAdapter mListData;
...
mListData = new MyAdapter(this);
并将数据添加到适配器,如下所示:
mListData.add(new Data(name, id2));