在我的应用程序中,我需要根据单击的列表从一个活动移动到另一个活动。在每个列表视图中将有三个textview,即:Emp_id,Emp_name,Emp_address,因此每当我点击应该移动到相应Emp_id的下一个活动的特定列表时。所以在这里我想抓住Emp_id,这是我无法做到的。我已经尝试了很多,但只获得了第一个Emp_id而不是我点击的列表。
CODE:
ArrayList<HashMap<String, String>> mylist2 = new ArrayList<HashMap<String, String>>();
try{
JSONObject jObj = new JSONObject(Mydetail);
JSONArray data = jObj.getJSONArray("mydata");
for(int i=0;i<data.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = data.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("Eid", "" + e.getString("E_id"));
map.put("Ename", "" + e.getString("E_name"));
map.put("Eaddress", "" + e.getString("E_address"));
mylist2.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist2 , R.layout.datalist,
new String[] { "Eid", "Ename", "Eaddress" },
new int[] { R.id.TextView1,R.id.TextView2e,R.id.TextView3 });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
//String val = o.get("id");
Pnm = (TextView)findViewById(R.id.TextView1);
String str = ((TextView)Pnm).getText().toString();
Toast.makeText(projects.this, str,Toast.LENGTH_LONG).show();
Intent newActivitySecond = new Intent(projects.this, modules.class);
projects.this.startActivity(newActivitySecond);
}
});
由于
答案 0 :(得分:6)
试试这样:
ListView lv_ArchivePartylist;
lv_ArchivePartylist = (ListView)findViewById(R.id.archive_ListView01);
lv_ArchivePartylist.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// TODO Auto-generated method stub
String message = view.findViewById(R.id.txt_box1).getText().toString;
Toast.makeText(Activityname.this, message, Toast.LENGTH_SHORT).show();
}
}
});
答案 1 :(得分:3)
试试这个
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) mylist2.get(position);
String Emp_id = o.get("Eid");
//use this Emp_id
}
});
答案 2 :(得分:1)
您必须将set onclicklistener用于ListView中的所有元素。我创建了一个列表视图。将适配器设置为listview。我的适配器是负责显示listview的每个元素。所以在getView()里面我将onclicklistener分配给我想要获得点击事件的所有元素
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.item, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.text);;
holder.image=(ImageView)vi.findViewById(R.id.image);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.text.setText("item "+position);
holder.image.setTag(data.get(position));
holder.image.setOnClickListener(this);
holder.text.setOnClickListener(this);
imageLoader.DisplayImage(data.get(position), activity, holder.image);
return vi;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.println("...clicked view is...."+arg0);
}