Q如何在android中处理listview中的click事件?

下面是我创建列表视图的测试代码,列表视图显示成功,但是点击事件有错误。我想创建一个意图,向新活动发送硬代码消息。但是,它显示行的错误

Intent intent = new Intent(context, SendMessage.class);

所以,问题是,我应该为这门课提供什么?

另外,如何捕获列表视图行中的数据并传递给新活动,而不是硬编码输出消息?例如,第一行的BBB,AAA,R.drawable.tab1_hdpi

谢谢。

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.ListViewTest.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayList<ListEntry> members = new ArrayList<ListEntry>(); 
        members.add(new ListEntry("BBB","AAA",R.drawable.tab1_hdpi));
        members.add(new ListEntry("ccc","ddd",R.drawable.tab2_hdpi));
        members.add(new ListEntry("assa","cxv",R.drawable.tab3_hdpi));
        members.add(new ListEntry("BcxsadvBB","AcxdxvAA"));
        members.add(new ListEntry("BcxvadsBB","AcxzvAA"));
        members.add(new ListEntry("BcxvBB","AcxvAA"));
        members.add(new ListEntry("BvBB","AcxsvAA"));
        members.add(new ListEntry("BcxvBB","AcxsvzAA"));
        members.add(new ListEntry("Bcxadv","AcsxvAA"));
        members.add(new ListEntry("BcxcxB","AcxsvAA"));
        ListView lv = (ListView)findViewById(R.id.listView1);
        Log.i("testTag","before start adapter");
        StringArrayAdapter ad = new StringArrayAdapter (members,this);
        Log.i("testTag","after start adapter");
        Log.i("testTag","set adapter");
        lv.setAdapter(ad);
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                Intent intent = new Intent(context, SendMessage.class);
                String message = "abc";
                intent.putExtra(EXTRA_MESSAGE, message);
                startActivity(intent);
            }
        });
    }
2019-09-18 19:24:10  Bishan

A回答

  • 1

    我看不出你在哪里申报context。为了创建意图,您可以使用MainActivity.this

     lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {
                    Intent intent = new Intent(MainActivity.this, SendMessage.class);
                    String message = "abc";
                    intent.putExtra(EXTRA_MESSAGE, message);
                    startActivity(intent);
                }
            });
    

    要在单击后检索对象,可以使用AdapterView

    ListEntry entry = (ListEntry) parent.getItemAtPosition(position);
    
    2019-09-19 22:53:22  Blackbelt
    • 谢谢,上下文到底是什么意思? – user782104 Jul 25 '13 at 7:33
    • 1
      您能换个说法吗? – Blackbelt Jul 25 '13 at 7:37
    • 这意味着,intent类中的第一个参数实际上表示什么?mainActivity是什么意思?谢谢 – user782104 Jul 25 '13 at 7:38
    • 2
      来自文档This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents。它包含Android启动SendMessage活动所需的信息 – Blackbelt Jul 25 '13 at 7:39
    • 也谢谢,为什么找不到显式活动类{com.example.listViewTest/com.example.listViewTest.itemDetails};您是否在AndroidManifest.xml中声明了此活动? – user782104 Jul 25 '13 at 7:44
    • 我已经创建了public class itemsdetails extends activity – user782104 Jul 25 '13 at 7:45
    • 必须在androidmanifest.xml中声明itemdetails。所有活动都必须在androidmanifest.xml中声明 – Blackbelt Jul 25 '13 at 7:47
    • 已修复。最后一个问题…如果条目对象(listentry)有3个条目?两个字符串和一个整数(照片存储),如何公开它并传递给新活动(是否有必要)? – user782104 Jul 25 '13 at 7:53
    • 如果listentry是可序列化的,则可以传递整个对象,否则必须通过itent传递所需的所有字段(您已经通过额外的消息传递了)。请注意,它像哈希映射一样工作。对于每个字段,您必须提供不同的键 – Blackbelt Jul 25 '13 at 7:56
    • 将位于位置的对象传递给目标活动是否更有意义? – peterb Apr 17 '16 at 12:30
    • @peterb它可以。这取决于用例。ListEntry,在本例中,应该是可分包/可序列化的,以便传递给新活动 – Blackbelt Apr 17 '16 at 12:51
    • @黑带能给我上课的机会吗? – TapanHP Dec 13 '16 at 5:57
    • @tapanhp listentry不是框架的一部分,它是操作模型类 – Blackbelt Dec 13 '16 at 8:01
    • 好的,收到了@blackbelt – TapanHP Dec 13 '16 at 8:12
    • 很多!它&39;非常有用 – Serg Shapoval Apr 10 '17 at 10:38
  • 2

    ListView具有项Click Listener回调。您应该在onItemClickListener中设置ListView。回调包含AdapterViewposition作为参数。它可以给你ListEntry

    lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {
                    ListEntry entry= (ListEntry) parent.getAdapter().getItem(position);
                    Intent intent = new Intent(MainActivity.this, SendMessage.class);
                    String message = entry.getMessage();
                    intent.putExtra(EXTRA_MESSAGE, message);
                    startActivity(intent);
                }
            });
    
    2019-09-19 22:53:26  Kapil Vats
    • 1
      谢谢。如果条目对象有3项?两个字符串和一个整数(照片存储),如何公开它并传递给新活动(是否有必要)? – user782104 Jul 25 '13 at 7:49
    • 您可以在listentry中实现parcele或serializable,并将整个对象传递给新活动。 – Kapil Vats Oct 24 '15 at 18:59
  • 3

    正如您所说,此语句中的代码出现错误

    Intent intent = new Intent(context, SendMessage.class);
    

    这是因为您在intent构造函数中提供了onitemclicklistener匿名类的上下文,但是根据intent的构造函数

    android.content.Intent.Intent(Context packageContext, Class<?> cls)
    

    您必须提供您使用intent的活动的上下文,intent是mainactivity类上下文。因此,您给出错误的语句将转换为

    Intent intent = new Intent(MainActivity.this, SendMessage.class);
    

    另外,要将您的信息从此主活动发送到SendMessage类,请参见下面的代码

    lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {
                    ListEntry entry= (ListEntry) parent.getAdapter().getItem(position);
                    Intent intent = new Intent(MainActivity.this, SendMessage.class);
                    intent.putExtra(EXTRA_MESSAGE, entry.getMessage());
                    startActivity(intent);
                }
            });
    

    如果这对您有帮助,请告诉我

    编辑:-如果您发现有问题要获取list的值,请做一件事:清除数组列表

    ArrayList<ListEntry> members = new ArrayList<ListEntry>();
    

    全球范围内,即在创建和更改您的听众之前,如下所示

     lv.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
                        Intent intent = new Intent(MainActivity.this, SendMessage.class);
                        intent.putExtra(EXTRA_MESSAGE, members.get(position));
                        startActivity(intent);
                    }
                });
    

    因此,您的整个代码将显示为

    public class MainActivity extends Activity {
        public final static String EXTRA_MESSAGE = "com.example.ListViewTest.MESSAGE";
    ArrayList<ListEntry> members = new ArrayList<ListEntry>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            members.add(new ListEntry("BBB","AAA",R.drawable.tab1_hdpi));
            members.add(new ListEntry("ccc","ddd",R.drawable.tab2_hdpi));
            members.add(new ListEntry("assa","cxv",R.drawable.tab3_hdpi));
            members.add(new ListEntry("BcxsadvBB","AcxdxvAA"));
            members.add(new ListEntry("BcxvadsBB","AcxzvAA"));
            members.add(new ListEntry("BcxvBB","AcxvAA"));
            members.add(new ListEntry("BvBB","AcxsvAA"));
            members.add(new ListEntry("BcxvBB","AcxsvzAA"));
            members.add(new ListEntry("Bcxadv","AcsxvAA"));
            members.add(new ListEntry("BcxcxB","AcxsvAA"));
            ListView lv = (ListView)findViewById(R.id.listView1);
            Log.i("testTag","before start adapter");
            StringArrayAdapter ad = new StringArrayAdapter (members,this);
            Log.i("testTag","after start adapter");
            Log.i("testTag","set adapter");
            lv.setAdapter(ad);
            lv.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
                            Intent intent = new Intent(MainActivity.this, SendMessage.class);
                            intent.putExtra(EXTRA_MESSAGE, members.get(position).getMessage());
                            startActivity(intent);
                        }
                    });
        }
    

    其中getMessage()将是listentry类中指定的getter方法,您将使用该方法获取先前设置的消息。

    2019-09-19 22:53:31  Abhinav Singh Maurya
    • 没有为类型listentry定义getMessage()方法? – user782104 Jul 25 '13 at 8:02
    • 它在entry.getMessage()处出错,很抱歉忘记提及 – user782104 Jul 25 '13 at 8:02
    • 1
      @user782104请检查我编辑的代码 – Abhinav Singh Maurya Jul 25 '13 at 8:18
  • 4

    首先,类必须实现click listener:

    implements OnItemClickListener
    

    然后将侦听器设置为列表视图

    yourList.setOnItemclickListener(this);
    

    最后,创建clic方法:

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
    Toast.makeText(MainActivity.this, "You Clicked at ",   
     Toast.LENGTH_SHORT).show();
    }
    
    2019-09-19 22:53:32  Avinash Garg
  • 5
        //get main activity
        final Activity main_activity=getActivity();
    
        //list view click listener
        final ListView listView = (ListView) inflatedView.findViewById(R.id.listView_id);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String stringText;
    
                //in normal case
                stringText= ((TextView)view).getText().toString();                
    
                //in case if listview has separate item layout
                TextView textview=(TextView)view.findViewById(R.id.textview_id_of_listview_Item);
                stringText=textview.getText().toString();                
    
                //show selected
                Toast.makeText(main_activity, stringText, Toast.LENGTH_LONG).show();
            }
        });
    
        //populate listview
    
    2019-09-19 22:53:34  Rijul Sudhir
  • 6

    根据我的测试,

    1. 实现OnEmClickListener->;工作。

    2. setonitemclicklistener->;工作。

    3. 默认情况下,可以单击ListView(API 19)

    重要的是,click只对textview有效(如果选择simple_list_item_1.xml作为item)。这意味着,如果您为listview提供文本数据,那么当您单击文本区域时,click可以工作。单击空白区域不会触发单击事件。

    2019-09-19 22:53:35  Chaohsiung Huang