Java Android:活动中的 onListItemClick
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4080165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: onListItemClick in Activity
提问by Vic V
Previous time I asked a question here I learned a lot so I guess it's worth a shot to try it again.
上一次我在这里问了一个问题,我学到了很多,所以我想值得再试一次。
I am using the lazy list by Fedor from this link: Lazy load of images in ListView
我从这个链接使用 Fedor 的惰性列表: ListView 中的图像延迟加载
It's working like a charm. BUT, Fedor is making his main class extend Activityinstead of ListActivity. Because of this, I am no longer able to use a listItemClick listener. Eclipse declares some errors around onListItemClick(). It works when I turn
它就像一个魅力。但是,Fedor 正在让他的主类扩展Activity而不是ListActivity. 因此,我无法再使用 listItemClick 侦听器。Eclipse 声明了一些错误onListItemClick()。当我转动时它起作用
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Intent launcher here
}
into
进入
protected void onListItemClick(ListView l, View v, int position, long id) {
// Intent launcher here
}
But the intent launcher doesn't work. Neither does a toast notification.
但是意图启动器不起作用。Toast 通知也没有。
When I turn the Activityin a ListActivity, Eclipse doesn't stagger, but my emulator gives me a force close.
当我打开Activitya 时ListActivity,Eclipse 不会错开,但我的模拟器让我强制关闭。
How do I get
如何得到
- Either
onListItemClick()click in the activity (preferable) - Or do I transform the code into a
ListActivitywithout force close?
- 无论
onListItemClick()在活动点击(优选) - 或者我是否将代码转换
ListActivity为非强制关闭?
Thanks a lot in advance.
非常感谢。
采纳答案by Vinay Vissh
I am writing my answer as:
我正在写我的答案:
1) the code by @Falmarri needs some update
2) My suggested edit was totally rejected XD
3) Stackoverflow is not allowing me to write a comment.
1)@Falmarri 的代码需要更新
2)我建议的编辑被完全拒绝 XD
3)Stackoverflow 不允许我写评论。
Here is the code:
这是代码:
ListView listView = (ListView) findViewById(R.id.my_listview_in_layout);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
//Do stuff
//...
}
});
Reference:According to android.widget.AdapterView.OnItemClickListener , public method onItemClick() is the method invoked when an item is clicked {instead of unknown protected method onListItemClick()}
参考:根据android.widget.AdapterView.OnItemClickListener,公共方法 onItemClick() 是单击项目时调用的方法 {而不是未知的受保护方法 onListItemClick()}
回答by Falmarri
A listItemClickListener is attached to a ListView. When you changed ListActivityto Activity, your class no longer has a view associated with it and thus an Activityclass has no idea what to do with an onListItemClickListener.
listItemClickListener 附加到ListView. 当您更改ListActivity为 时Activity,您的类不再具有与其关联的视图,因此Activity类不知道如何处理 onListItemClickListener。
You just have to attached a listener to your ListView:
您只需要将侦听器附加到您的ListView:
listview.setOnItemClickListener(new OnItemClickListener(){
@Override
protected void onListItemClick(AdapterView<?> parent, View view, int position, long id){
//Do stuff
}
});
回答by Yoni Samlan
For a non-ListActivityto have an item-clicked-listener for a ListView, you have to call the setOnItemClickedListener()on the ListView(you may need to get that using findViewById()if it's coming from XML)
对于非ListActivity有一个项目,点击,监听器ListView,你必须调用setOnItemClickedListener()上ListView(您可能需要获得使用findViewById(),如果它是从XML来)
Rather than just overriding ListActivity's onListItemClickListener(), here you'd have your invoking Activityimplement AdapterView.onItemClickedListener()and pass it as the parameter to setOnItemClickedListener().
在这里,您将拥有调用工具并将其作为参数传递给 ,而不仅仅是覆盖ListActivity's 。onListItemClickListener()ActivityAdapterView.onItemClickedListener()setOnItemClickedListener()
(If you read the source code for ListActivity(which I recommend), you'll see it just does exactly that behind the scenes by creating an internal listener object that calls your overridden onListItemClick()).
(如果您阅读了ListActivity(我推荐的)的源代码,您会看到它通过创建一个调用您的覆盖的内部侦听器对象在幕后完全做到这一点onListItemClick())。
回答by Cristian
If you are using ListActivitythen you want to do something like this:
如果您正在使用,ListActivity那么您想要执行以下操作:
public class YourClass extends ListActivity implements OnItemClickListener{
@Override
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.your_layout);
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// your stuff here
}
}
This is not the only way to set an OnItemClickListener. Look other answers. This is the way I like to do it since it's clearer and easier to read.
这不是设置OnItemClickListener. 看看其他答案。这是我喜欢这样做的方式,因为它更清晰,更容易阅读。
回答by Nathan Kellert
I have been working on this all day and after making my own ArrayAdapterI couldn't figure out how to change classes in my list.
我一整天都在研究这个,在自己制作之后,我ArrayAdapter不知道如何更改我列表中的课程。
Here's how I found out how to do it. After I called my array i simply finished out my code in that method by doing.
这是我如何发现如何做到这一点。在我调用我的数组后,我只是通过执行完成了该方法中的代码。
ListView lv =getListView();
lv.setOnItemClickListener(this);
Then after all my text i put
然后在我所有的文字之后
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
String item = (String) getListAdapter().getItem(position);
if (item.equals("Economy"))
{
Intent intent = new Intent(packages.this, economy.class);
startActivity(intent);
}
else if (item.equals("Basic"))
{
Intent intent = new Intent(packages.this, basic.class);
startActivity(intent);
}
else if (item.equals("Professional"))
{
Intent intent = new Intent(packages.this, professional.class);
startActivity(intent);
}
else if (item.equals("Custom Applications"))
{
Intent intent = new Intent(packages.this, applications.class);
startActivity(intent);
}
}
Between I managed to completely customize my ListViewwith custom font and backgrounds. I'm sure a ton of you don't really care. But I'm exited and was hoping by posting this that I might help someone in the future.
在我设法完全自定义我ListView的自定义字体和背景之间。我相信很多人并不真正关心。但是我退出了,并希望通过发布此信息可以在将来帮助某人。
回答by ray
I have simply added following in onCreate():
我只是在以下内容中添加了以下内容onCreate():
listvview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setPaymentDetails();
}
});
Outside of onCreate()added setPaymentDetails():
在onCreate()添加之外setPaymentDetails():
protected void setPaymentDetails()
{
Intent intent = new Intent(this, SetPaymentDetailsActivity.class);
startActivity(intent);
}
回答by ares
FE.java
FE.java
package com.example.rfe;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class FE extends ListActivity {
public List<String> d = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file);
d = new ArrayList<String>();
Scanner in = null;
File f = new File("/sdcard/input.txt");
try
{
in = new Scanner(new FileReader(f));
while(in.hasNext()==true)
{
d.add(in.nextLine());
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, d);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
super.onListItemClick(l, v, position, id);
}
}
回答by J4cK
Assume you have datasource Fruit contain few strings. You can define the onCreate()as following:
假设您有数据源 Fruit 包含几个字符串。您可以定义onCreate()如下:
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,
FRUITS)); // context is this, style is list_fruit, Context, ,data_binding to FRUITs
And then write the onListItemClick()as following:
然后写onListItemClick()如下:
protected void onListItemClick(ListView parent, View v, int position, long id) {
Toast.makeText(this, " You selected " + FRUITS[position], Toast.LENGTH_SHORT).show();
}
I hope it works for you :)
我希望这个对你有用 :)

