从列表视图错误的项目被选中 [英] Wrong item from the listview is selected
问题描述
最初,它显示所选择正确的项目......但是重新填充列表则显示错误的项目被选中后......请帮我找到错误
Intially列表中填充的onCreate()
和重新填充在onOptionsItemSelected(菜单项项目)
公共类MainActivity延伸活动{ MyListActivity适配器;
ListView控件列表; 的String [] =网站{
杰里
沃尔特斯
};
整数[] = imageId {
R.drawable.ic_launcher,
R.drawable.ic_launcher
}; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
适配器=新MyListActivity(MainActivity.this,网络,imageId);
名单=(ListView控件)findViewById(R.id.listView1);
list.setAdapter(适配器);
list.setOnItemClickListener(新AdapterView.OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
Toast.makeText .show()(MainActivity.this,+网站[+位置],Toast.LENGTH_SHORT你在点击了);
}
});
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
} @覆盖
公共布尔onOptionsItemSelected(菜单项项){ 开关(item.getItemId()){ 案例R.id.action_settings:
的String [] = WEB2 {
沃尔特斯
杰里
};
适配器=新MyListActivity(MainActivity.this,web2上,imageId);
list.setAdapter(适配器);
返回true;
}
返回false;
}}
您有这
网站[位置]
//基于位置的web2上获得的Web字符串数组字符串不
位置是该指数。
网站[0] =杰里
网站[1] =沃尔特斯
您重新填充的ListView你用不同的阵列。
网站
和 web2的
是两个不同的阵列。
如果要更新,更新Web数组并调用 notifyDataSetChanged
适配器刷新列表视图上
Initially it shows the correct item selected...but after re-populating the list it shows the wrong item being selected...please help me find the mistake
Intially the list is populated in onCreate() and is re-populated in onOptionsItemSelected(MenuItem item)
public class MainActivity extends Activity {
MyListActivity adapter;
ListView list;
String[] web = {
"jerry",
"walters"
} ;
Integer[] imageId = {
R.drawable.ic_launcher,
R.drawable.ic_launcher
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MyListActivity(MainActivity.this, web, imageId);
list=(ListView)findViewById(R.id.listView1);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
String[] web2 = {
"walters",
"jerry"
} ;
adapter = new MyListActivity(MainActivity.this, web2, imageId);
list.setAdapter(adapter);
return true;
}
return false;
}
}
You have this
web[position]
// get string from web string array not from web2 based on position
Position is the index.
web[0] = "jerry"
web[1] ="walters"
You are re-populating your listview with a different array.
web
and web2
are two different arrays.
If you want to update , update the web array and call notifyDataSetChanged
on your adapter to refresh listview
这篇关于从列表视图错误的项目被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!