我已经定制了一个带有列表视图的适配器,每个列表项中有四个项目:
TextView id = tid
TextView id = tname
TextView id = tgender
TextView id = tage
现在点击,我想从ID为' tid'的项目的textview中获取文字。
public class TeacherAdapter extends ArrayAdapter <Teacher>{
private LayoutInflater inflater;
private ArrayList<Teacher> teachers;
public TeacherAdapter(Context context, ArrayList<Teacher> teachers) {
super(context, 0, teachers);
inflater = LayoutInflater.from(context);
this.teachers = teachers;
}
public int getCount() {
return teachers.size();
}
public Teacher getItem(int position) {
return teachers.get(position);
}
public long getItemId(int position) {
return position;
}
private static class ViewHolder {
TextView txtName;
TextView txtMobile;
TextView txtDept;
TextView txtId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.teacher_text_item, null);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.tName);
viewHolder.txtMobile = (TextView) convertView.findViewById(R.id.tMobile);
viewHolder.txtDept = (TextView) convertView.findViewById(R.id.tDepartment);
viewHolder.txtId = (TextView) convertView.findViewById(R.id.tId);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txtName.setText(teachers.get(position).getName());
viewHolder.txtMobile.setText(teachers.get(position).getMobile());
viewHolder.txtDept.setText(teachers.get(position).getDepartment());
viewHolder.txtId.setText(teachers.get(position).getId()+"");
return convertView;
}
}
我在多个列表视图中有四个项目:name,mobileno,department,id。点击它后,我想要做的就是列表项中的getId
。
答案 0 :(得分:1)
更正代码
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
teacher= parent.getItemAtPosition(position)
String text = teacher.getId();
//TextView tid = (TextView) view.findViewById(R.id.tid);
//String text = tid.getText().toString();
}
});
答案 1 :(得分:1)
此代码解决了我的问题。
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (v != null){
TextView textView = (TextView)v.findViewById(R.id.tId);
Log.e("idthatclicked",textView.getText().toString());
}
}
答案 2 :(得分:0)
在listview的onItemClick方法中,编写此代码以获取id。在这里你需要直接从适配器中获取值,如下所述
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// TODO Auto-generated method stub
int id= adapter1.getItem(position).getId();
}
或
@Override
protected void onListItemClick(ListView list, View view, int position, long id) {
super.onListItemClick(list, view, position, id);
int id = getListView().getItemAtPosition(position).getId();
}
答案 3 :(得分:0)
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ViewHolder holder= (ViewHolder ) view.getTag();
String text = holder.txtId.getText().toString();
}
});
通过这种方式,您可以跳过resouceId部分的查找,而不是使用holder。
答案 4 :(得分:-1)
如果您有OnItemClickListener,则会将单击的视图传递给它。检查视图是否为空。然后,做:
@Override
public void onItemClick(AdapterView<?> arg0, View viewThatWasClicked,
int position, long arg3) {
TextView tid = (TextView) viewThatWasClicked.findViewById(R.id.tid);
String text= tid.getText().toString();
}