android - 如何在ListView中实现 Action 监听器?

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


            }
            });


        }



    }

最佳答案

试试这个,

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

关于android - 如何在ListView中实现 Action 监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7218565/

相关文章:

java - Android 后退按钮无法正常使用

android - 如何在 Honeycomb 及以上版本中使用选项菜单直至 GingerBread 和操作栏?

android - HttpURLConnection (java.net.CookieManager) 和 WebView (android.webkit.CookieManager) 之间 cookie 的两种同步方式

android - 突出显示列表项

android - RelativeLayout 的行为与 ListView 项不同

android - 长按 ListView android 后显示一个对话框

java - 在 Fragment 中设置 onClickListener 时出现 NullPointerException

Android自定义加载器,LoaderManagerImpl.LoaderInfo.callOnLoadFinished只调用一次

java - 更改 ListView 项的颜色

Android notifyDataSetChanged 删除后不刷新项目