如何在ListView中实现动作侦听器?

时间:2011-08-28 01:52:03

标签: android listview

基本上,我想创建一个包含大量项目的ListView,每当用户点击任何项目时,它将根据他点击的项目打开一个新的Activity。请帮助我,我会非常感激。

这是我的代码:

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]);
                        }
                    }
                }


            }
            });


        }



    }

1 个答案:

答案 0 :(得分: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);
        }
    });