gpt4 book ai didi

java - 当 ListView 中有多个项目时,读取所选项目的文本

转载 作者:行者123 更新时间:2023-12-01 11:51:29 25 4
gpt4 key购买 nike

我自定义了一个带有 ListView 的适配器,每个列表项中有四个项目:

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

我在多个 ListView 中有四个项目:名称、手机号码、部门、ID。我想要做的只是在单击列表项时从列表项中获取getId

最佳答案

这段代码解决了我的问题。

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

关于java - 当 ListView 中有多个项目时,读取所选项目的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28791735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com