最初,它顯示所選擇正確的項目......但是重新填充列表則顯示錯誤的項目被選中後......請幫我找錯錯誤的項目被選中
Intially列表在的onCreate() 填充和onOptionsItemSelected(菜單項項目)被重新填充
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;
}
}
試... 開關(item.getItemId()){ 情況R.id.action_settings: 字符串[] WEB2 = { 「沃爾特斯」, 「傑裏」 }; list =(ListView)findViewById(R.id.listView1); adapter = new MyListActivity(MainActivity.this,web2,imageId); list.setAdapter(adapter); 返回true; } –
你有這個 'web [position]'//總是顯示來自web字符串數組而不是web2 – Raghunandan
是的......問題是兩個不同的數組 –