2011-08-28 120 views
2

基本上,我想創建一個帶有一堆物品的ListView,每當用戶點擊任何物品時,它都會根據他點擊的物品打開一個新的活動。 PLease,幫助我,我真的很感激它。如何在ListView中實現動作監聽器?

這裏是我的我的代碼:

package com.hipeople; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ListView; 

public class char1 extends Activity { 
/** Called when the activity is first created. */ 
private ListView lv1; 
private EditText ed; 
private String lv_arr[]={"Android","Cupcake","Donut","Eclairs","AndroidPeople","Froyo",}; 
private ArrayList<String> arr_sort= new ArrayList<String>(); 
int textlength=0; 
@Override 
    public void onCreate(Bundle icicle) 
     { 
      super.onCreate(icicle); 
      setContentView(R.layout.main); 

      lv1=(ListView)findViewById(R.id.ListView01); 
      ed=(EditText)findViewById(R.id.EditText01); 
      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]); 
         } 
        } 
       } 


      } 
      }); 


     } 



    } 
+0

請查看[ListView的教程](http://developer.android.com/resources/tutorials/views /hello-listview.html)與基礎知識脫節。 –

回答

4

試試這個,

lv1.setOnItemClickListener(new OnItemClickListener() 
     { 
     public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
     { 
      String str = ((TextView) arg1).getText().toString(); 
      Toast.makeText(getBaseContext(),str, Toast.LENGTH_LONG).show(); 
      Intent intent = new Intent(getBaseContext(),your_new_Intent.class); 
      intent.putExtra("list_view_value", str); 
      startActivity(intent); 
     } 
    }); 
+0

感謝您的relpy,但它給出了一些錯誤「類型new AdapterView.OnItemClickListener(){}必須實現繼承的抽象方法AdapterView.OnItemClickListener.onItemClick(AdapterView ,View,int,long)」。 PLease幫我 – user858975

+0

來休閒聊天室 –

+0

你會在聊天室裏嗎? – user858975