50

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

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

6 回答 6

112

我看不到你在哪里声明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);
于 2013-07-25T07:30:49.110 回答
13

ListView 具有 Item 单击侦听器回调。您应该onItemClickListenerListView. 回调包含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);
            }
        });
于 2013-07-25T07:34:54.277 回答
6

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

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

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

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

您必须提供您正在使用 Intent 的活动的上下文,即 MainActivity 类上下文。所以你给出错误的语句将被转换为

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

也为了将您的消息从这个 MainActivity 发送到 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);
            }
        });

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

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

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

全局,即在 oncreate 之前并更改您的侦听器,如下所示

 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 方法,用于获取先前设置的消息。

于 2013-07-25T07:52:02.660 回答
6

首先,该类必须实现点击监听器:

implements OnItemClickListener

然后为 ListView 设置一个监听器

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();
}
于 2014-12-03T10:03:53.417 回答
2
    //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
于 2016-04-28T08:57:59.290 回答
2

根据我的测试,

  1. 实现 OnItemClickListener -> 有效。

  2. setOnItemClickListener -> 有效。

  3. ListView 默认是可点击的 (API 19)

重要的是,“单击”仅适用于 TextView(如果您选择 simple_list_item_1.xml 作为项目)。这意味着如果您为 ListView 提供文本数据,“单击”会在您单击文本区域时起作用。点击空白区域不会触发“点击事件”。

于 2016-08-29T01:27:53.493 回答