我正在研究使用ListView显示人物数量和名称的Android应用程序。但编译器无法识别“champs1”和“champs2”。
public class Second extends Activity {
ListView vue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
vue=(ListView) findViewById(R.id.listV2);
String [][] rep=new String[][]{{"1","aaaaaaa" },
{"2","bbbbbbb" },
{ "3","ccccccc" } };
List<HashMap<String,String> > list= new ArrayList<HashMap<String, String>>();
HashMap<String,String> element;
for(int i=0 ;i<rep.length;i++ ){
element =new HashMap<String, String>();
element.put("chmaps1",rep[i][0]);
element.put("champs2",rep[i][1]);
list.add(element);
}
ListAdapter adapter=new SimpleAdapter(this,
list,
android.R.layout.simple_list_item_2,
new String[] {"chmaps1","champs2"},
new int[] {android.R.id.champs1, android.R.id.champs2 });
vue.setAdapter(adapter);
}
}
错误:(39,71)错误:找不到符号变量champs1 错误:(39,71)错误:找不到符号变量champs2
答案 0 :(得分:1)
将android.R.id.champs1更改为android.R.id.text1
将android.R.id.champs2更改为android.R.id.text2
公共类Second扩展Activity { ListView vue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
vue = (ListView) findViewById(R.id.listView1);
String[][] rep = new String[][] { { "1", "aaaaaaa" },
{ "2", "bbbbbbb" }, { "3", "ccccccc" } };
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> element;
for (int i = 0; i < rep.length; i++) {
element = new HashMap<String, String>();
element.put("chmaps1", rep[i][0]);
element.put("champs2", rep[i][1]);
list.add(element);
}
ListAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, new String[] { "chmaps1",
"champs2" }, new int[] { android.R.id.text1,
android.R.id.text2 });
vue.setAdapter(adapter);
}
}
答案 1 :(得分:0)
将此代码放入for循环
HashMap<String, String> element = new HashMap<String, String>();
并且没有问题我认为android.R.id.champs1或R.id.champs1都是正确的。
更改此项而不是此 vue.setAdapter(适配器)
尝试这样 vue.setListAdapter(适配器);
答案 2 :(得分:0)
您正在接受:
android.R.id.champs1
而不是:
R.id.champs1
另外要小心你有时会调用变量chmaps1,有时会调用champs1。