Android:Activity 中的 onListItemClick

发布于 2024-09-30 11:27:47 字数 1007 浏览 6 评论 0原文

上次我在这里问了一个问题,我学到了很多东西,所以我想值得再试一次。

我正在使用来自此链接的 Fedor 的惰性列表: ListView 中的图像延迟加载

它的作用就像一个魅力。但是,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
}

工作

   protected void onListItemClick(ListView l, View v, int position, long id) {
     // Intent launcher here
   }

。 Toast 通知也没有。

当我在 ListActivity 中转动 Activity 时,Eclipse 不会错开,但我的模拟器会强制关闭。

如何

  • 在活动中单击 onListItemClick() (首选)
  • 或者将代码转换为 ListActivity 而无需强制关闭?

预先非常感谢。

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

It's working like a charm. BUT, Fedor is making his main class extend Activity instead 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

    @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.

When I turn the Activity in a ListActivity, Eclipse doesn't stagger, but my emulator gives me a force close.

How do I get

  • Either onListItemClick() click in the activity (preferable)
  • Or do I transform the code into a ListActivity without force close?

Thanks a lot in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

北座城市 2024-10-07 11:27:47

listItemClickListener 附加到 ListView。当您将 ListActivity 更改为 Activity 时,您的类不再具有与其关联的视图,因此 Activity 类不知道如何处理一个 onListItemClickListener。

您只需将侦听器附加到您的 ListView 即可:

listview.setOnItemClickListener(new OnItemClickListener(){
    @Override
    protected void onListItemClick(AdapterView<?> parent, View view, int position, long id){
        //Do stuff
    }
});

A listItemClickListener is attached to a ListView. When you changed ListActivity to Activity, your class no longer has a view associated with it and thus an Activity class has no idea what to do with an onListItemClickListener.

You just have to attached a listener to your ListView:

listview.setOnItemClickListener(new OnItemClickListener(){
    @Override
    protected void onListItemClick(AdapterView<?> parent, View view, int position, long id){
        //Do stuff
    }
});
淡淡の花香 2024-10-07 11:27:47

我的答案写为:

1) @Falmarri 的代码需要一些更新
2)我建议的编辑被完全拒绝了XD
3) Stackoverflow 不允许我写评论。

这是代码:

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

参考:根据 android.widget.AdapterView .OnItemClickListener ,公共方法 onItemClick() 是单击某个项目时调用的方法{而不是未知的受保护方法 onListItemClick() }

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.

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

记忆消瘦 2024-10-07 11:27:47

我一整天都在研究这个问题,在制作了自己的 ArrayAdapter 之后,我不知道如何更改列表中的类。

以下是我如何找到如何做到这一点的方法。在我调用我的数组之后,我只是通过执行该方法完成了我的代码。

ListView lv =getListView();
lv.setOnItemClickListener(this);

然后,在我将所有文本放在

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

之间之后,我设法使用自定义字体和背景完全自定义我的 ListView 。我确信你们很多人并不真正关心。但我很兴奋,并希望通过发布这篇文章可以帮助将来的某人。

I have been working on this all day and after making my own ArrayAdapter I couldn't figure out how to change classes in my list.

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 ListView with 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.

心头的小情儿 2024-10-07 11:27:47

对于非 ListActivity 来说,要为 ListView 提供项目点击侦听器,您必须调用 setOnItemClickedListener() /code> (如果来自 XML,您可能需要使用 findViewById() 来获取它),

而不仅仅是覆盖 ListActivityonListItemClickListener() >,在这里你可以调用 Activity 实现 AdapterView.onItemClickedListener() 并将其作为参数传递给 setOnItemClickedListener()

(如果您阅读 ListActivity 的源代码(我推荐),您会发现它只是通过创建一个内部侦听器对象来调用您重写的 onListItemClick() 在幕后执行此操作。 )。

For a non-ListActivity to 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)

Rather than just overriding ListActivity's onListItemClickListener(), here you'd have your invoking Activity implement AdapterView.onItemClickedListener() and pass it as the parameter to 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()).

疾风者 2024-10-07 11:27:47

如果您使用的是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
    }
}

这不是设置OnItemClickListener 的唯一方法。看看其他答案。这是我喜欢的方式,因为它更清晰、更容易阅读。

If you are using ListActivity then you want to do something like this:

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.

傲鸠 2024-10-07 11:27:47

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

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);
}
}
孤独陪着我 2024-10-07 11:27:47

假设您的数据源 Fruit 包含很少的字符串。您可以按如下方式定义 onCreate()

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,
                FRUITS)); // context is this, style is list_fruit, Context, ,data_binding to FRUITs

然后按如下方式编写 onListItemClick()

protected void onListItemClick(ListView parent, View v, int position, long id) {
                Toast.makeText(this,  " You selected  " + FRUITS[position], Toast.LENGTH_SHORT).show();
        }

我希望它对您有用:)

Assume you have datasource Fruit contain few strings. You can define the onCreate() as following:

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:

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 :)

止于盛夏 2024-10-07 11:27:47

我只是在 onCreate() 中添加了以下内容:

    listvview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            setPaymentDetails();
        }
    });

onCreate() 之外添加了 setPaymentDetails()

protected void setPaymentDetails()
{
    Intent intent = new Intent(this, SetPaymentDetailsActivity.class);
    startActivity(intent);
}

I have simply added following in onCreate():

    listvview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            setPaymentDetails();
        }
    });

Outside of onCreate() added setPaymentDetails():

protected void setPaymentDetails()
{
    Intent intent = new Intent(this, SetPaymentDetailsActivity.class);
    startActivity(intent);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文