如何处理android中Listview中的点击事件?
Posted
技术标签:
【中文标题】如何处理android中Listview中的点击事件?【英文标题】:How to handle the click event in Listview in android? 【发布时间】:2013-07-24 23:20:12 【问题描述】:下面是我创建列表视图的测试代码,列表视图显示成功,但是点击事件有错误。我想创建一个向新活动发送硬编码消息的意图。但是,它显示该行的错误
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);
);
【问题讨论】:
【参考方案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);
【讨论】:
谢谢,上下文到底是什么意思? 也就是说,Intent 类中的第一个参数实际代表的是什么? MainActivity.this 是什么意思?谢谢 来自文档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 活动所需的信息
谢谢,为什么找不到明确的活动类 com.example.listviewtest/com.example.listviewtest.ItemDetails;您是否在 AndroidManifest.xml 中声明了此活动?
我已经创建了 public class ItemDetails extends Activity【参考方案2】:
ListView 有 Item 点击监听回调。您应该在ListView
中设置onItemClickListener
。回调包含AdapterView
和position
作为参数。哪个可以给你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);
);
【讨论】:
谢谢。如果条目对象有 3 个项目?两个字符串和一个整数(存储的照片),如何将其公开并传递给新活动(是否有必要)? 您可以在 ListEntry 中实现 Parceble 或 Serializable,并可以将整个对象传递给您的新活动。【参考方案3】:如您所说,此语句中的代码出现错误
Intent intent = new Intent(context, SendMessage.class);
这是由于您将 OnItemClickListener 匿名类的上下文提供给 Intent 构造函数,但根据 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 方法,您可以使用它来获取先前设置的消息。
【讨论】:
ListEntry 类型的 getMessage() 方法未定义?【参考方案4】:首先,该类必须实现点击监听器:
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();
【讨论】:
【参考方案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
【讨论】:
【参考方案6】:根据我的测试,
实现 OnItemClickListener -> 有效。
setOnItemClickListener -> 有效。
ListView 默认可点击(API 19)
重要的是,“点击”仅适用于 TextView(如果您选择 simple_list_item_1.xml 作为项目)。这意味着如果您为 ListView 提供文本数据,“单击”会在您单击文本区域时起作用。点击空白区域不会触发“点击事件”。
【讨论】:
以上是关于如何处理android中Listview中的点击事件?的主要内容,如果未能解决你的问题,请参考以下文章