2012-01-19 57 views
0

我是android新手。如何在ListView(Android)中獲取選定的ListItem?

我在ListView中使用ListView工作,它工作正常,但我想從ListView中選擇ListItem。請任何人幫助我。

謝謝。我試過了: private ListView lv1; private EditText ed; SitesList sitesList = null;

 private String lv_arr[] = {"sam","priya","rahul",}; 

     private ArrayList<String> arr_sort= new ArrayList<String>(); 
     int textlength=0; 

     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.searchbar); 

    // index = lv1.getSelectedItemPosition(); 
    //  selObj = lv1.getSelectedItem(); 
    // System.out.println("selObj "+ selObj); 

     lv1=(ListView)findViewById(R.id.ListView01); 
     ed=(EditText)findViewById(R.id.EditText01); 

     sitesList = MyXMLHandler.sitesList; 
     String LoginToken = sitesList. getCourtName().toString(); 
     System.out.println("ResultLoginToken" + LoginToken); 

     lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, lv_arr)); 
     ed.addTextChangedListener(new TextWatcher() 
     { 
     public void afterTextChanged(Editable s) 
     { 

     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
     int after) 
     { 


     } 

     public void onTextChanged(CharSequence s, int start, int before, 
     int count) 
     { 

     textlength=ed.getText().length(); 
     arr_sort.clear(); 
     for(int i=0;i<lv_arr.length;i++) 
     { 
     if(textlength<=lv_arr[i].length()) 
     { 
     if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength))) 
     { 
     arr_sort.add(lv_arr[i]); 
     } 
     } 
     } 

     lv1.setAdapter(new ArrayAdapter<String>(Searchbar.this,android.R.layout.simple_list_item_1, arr_sort)); 
     } 
     }); 
     } 
     } 

回答

1

將onClick監聽器放在你的listView(lv)中,如下所示。

lv.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, 
        int arg2, long arg3) { 
System.out.println("items--"+lstBarsName.getItemIdAtPosition(arg2)); 

      } 
     }); 
+0

謝謝,但setOnItemClickListener不適用於參數 –

相關問題