0

我怎麼能有一個listView恆定的項目位置?我如何可以在一個列表視圖上使用常量項目位置來過濾項目?

假設您有listviewsearchboxactionbar。現在,您要過濾結果,並且還希望即使在過濾之後,該項目的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

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(); 

好運。

0

你可以把一個索引或位置數據模型的一部分。

+0

我需要動態位置。 –

+0

您能否提供您的需求示例? – nyarlathotep77

+0

我的所有代碼都在我的朋友之上。 –

相關問題