customlistadapter adapter1=new customlistadapter(userfrontpage.this, usernameq,groupnames, imgid);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView extratxt = (TextView) findViewById(R.id.textView1);
String text = extratxt.getText().toString();
TextView txtTitle = (TextView) findViewById(R.id.item);
String texx = txtTitle.getText().toString();
if(text.equals("personal")) {
String friend = ((TextView) view).getText().toString();
XMPPManager.getInstance().connect(username, password);
startMainActivity(friend,"personal");
}
else{
String friend = ((TextView) view).getText().toString();
XMPPManager.getInstance().connect(username, password);
startMainActivity(friend,"group");
}
}
以下是customlistadapter:
public class customlistadapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
private final String[] groupnamess;
public customlistadapter(Activity context, String[] itemname,String[] groupnames, Integer[] imgid) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub
this.context = context;
this.itemname = itemname;
this.imgid = imgid;
this.groupnamess= groupnames;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.mylist, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
if(position< itemname.length-groupnamess.length) {
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[0]);
extratxt.setText("personal");
}
else {
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[1]);
extratxt.setText("group");
}
return rowView;
}
显示了5行。无论我点击第四个还是第五个,id和位置都是正确的。但是,String text和texx指的是第一个。为什么会这样?请帮助。非常感谢你。
答案 0 :(得分:1)
初始化TextView的方式不正确。
TextView extratxt = (TextView) findViewById(R.id.textView1);
这应该改为这个
TextView extratxt = (TextView)view. findViewById(R.id.textView1);
答案 1 :(得分:0)
更改此
TextView txtTitle = (TextView) findViewById(R.id.item);
String texx = txtTitle.getText().toString();
到
String texx = itemname[position]
答案 2 :(得分:0)
这看起来像一个范围问题(当你调用findViewById()时会返回什么)。基本上它在Content View上调用findViewById设置为Activity(setContentView)。很难预测TextView刚被选中。我很惊讶应用程序没有崩溃
在以下行( tldr 答案):
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
您可能会注意到回调涉及多个变量。要获得特定的TextView,请尝试view.findViewById(_____)(可能是Andro Selva试图传达的内容)。
最有可能发生的事情是你的findViewById()正在返回屏幕上的许多TextView之一。那是因为每个Id都是与TextView相关联的整数。但是,ListView适配器中每行中的每个TextView都具有完全相同的Id。
findViewById转到最顶层的View,检查其子节点的所有ID,如果找到它,则返回该View。这包括儿童的孩子,就像在ListView中一样。在onItemClick()调用的情况下,将其指向view.findViewById的位置更接近您要查找的级别