我如何拥有一个具有常数项目位置的listView
?
假设您listview
上searchbox
actionbar
position
。现在您想要过滤结果,并希望即使在过滤后项目的listView
号也是不变的。
当我在public class About extends ActionBarActivity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
SearchView searchview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
//searchview = new SearchView(getSupportActionBar().getThemedContext());
mainListView = (ListView) findViewById( R.id.mainListView );
String[] mTitles = getResources().getStringArray(R.array.lstview_Content);
String[] values= new String[5];
for(int i=0;i<values.length;i++){
values[i] = mTitles[i];
}
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(values) );
listAdapter = new ArrayAdapter<String>(About.this, R.layout.lstview_layout,R.id.rowTextView, planetList);
mainListView.setAdapter( listAdapter );
mainListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
int itemposition = position;
String itemvalue = (String) mainListView.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
"Position :"+itemposition+" ListItem : " +itemvalue , Toast.LENGTH_LONG).show();
}
});
mainListView.setTextFilterEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.about, menu);
MenuItem searcehItem = menu.findItem(R.id.action_search);
searchview = (SearchView) MenuItemCompat.getActionView(searcehItem);
searchview.setQueryHint("ُُSearch");
searchview.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String arg0) {
return false;
}
@Override
public boolean onQueryTextChange(String arg0) {
mainListView.setFilterText(arg0);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}
上搜索时,以下代码给出了不同的位置:
{{1}}
答案 0 :(得分:1)
在ActionBarActivity
写:
int ID = 0;
在onItemClick
写:
for (int i = 0; i < values.length; i++) {
if(values[i] == itemvalue)
{
ID = i;
}
}
Toast.makeText(getApplicationContext(),"Position :"+ID+" ListItem : " +itemvalue , Toast.LENGTH_LONG).show();
祝你好运。
答案 1 :(得分:0)
您可以将索引或位置作为数据模型的一部分。