2011-08-28 121 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