ListView 不响应点击

发布于 2024-12-07 12:24:26 字数 1618 浏览 0 评论 0原文

这门课有什么问题吗?列表视图中的点击未注册,我尝试记录日志,但它没有进入 setItemOnClickListener

public class Chosen extends Activity{
SimpleCursorAdapter adapter;
String[] getResult;
Cursor c;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chosen);
    Intent i=getIntent();
    Bundle extras=i.getExtras();
    final TextView t=(TextView) findViewById(R.id.tv1);
    int num=extras.getInt("category");
    ArrayList al=new ArrayList<String>();
    switch(num)
    {
    case 0:c=Splash.db.getSocial(Login.uname);break;
    case 1:c=Splash.db.getMail(Login.uname);break;
    case 2:c=Splash.db.getBank(Login.uname);break;
    case 3:c=Splash.db.getMisc(Login.uname);break;
    }
    if(c.moveToFirst())
    {
        do
        {
            al.add(c.getString(1));
        }while(c.moveToNext());
    }

    getResult=new String[al.size()];
    al.toArray(getResult);
    ListView lv=(ListView) findViewById(R.id.list);
    lv.setClickable(true);
    ArrayAdapter ad=new ArrayAdapter(this,R.layout.chosenitemlist,R.id.client,getResult);
    lv.setAdapter(ad);

    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long id) {
            // TODO Auto-generated method stub
            Log.w("akash", "in list item click");
            t.setText("clicked");
            Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
        }
    });
}

}

whats wrong with this class? the clicks in the listview arent registered, i tried to do a log, but it doesnt go into the setItemOnClickListener

public class Chosen extends Activity{
SimpleCursorAdapter adapter;
String[] getResult;
Cursor c;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chosen);
    Intent i=getIntent();
    Bundle extras=i.getExtras();
    final TextView t=(TextView) findViewById(R.id.tv1);
    int num=extras.getInt("category");
    ArrayList al=new ArrayList<String>();
    switch(num)
    {
    case 0:c=Splash.db.getSocial(Login.uname);break;
    case 1:c=Splash.db.getMail(Login.uname);break;
    case 2:c=Splash.db.getBank(Login.uname);break;
    case 3:c=Splash.db.getMisc(Login.uname);break;
    }
    if(c.moveToFirst())
    {
        do
        {
            al.add(c.getString(1));
        }while(c.moveToNext());
    }

    getResult=new String[al.size()];
    al.toArray(getResult);
    ListView lv=(ListView) findViewById(R.id.list);
    lv.setClickable(true);
    ArrayAdapter ad=new ArrayAdapter(this,R.layout.chosenitemlist,R.id.client,getResult);
    lv.setAdapter(ad);

    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long id) {
            // TODO Auto-generated method stub
            Log.w("akash", "in list item click");
            t.setText("clicked");
            Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
        }
    });
}

}

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

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

发布评论

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

评论(2

枕头说它不想醒 2024-12-14 12:24:26

有趣的。
首先,尝试我自己检查过的几个场景: http://xjaphx.wordpress.com/2011/07/14/listview-doesnt-respond-to-onitemclicklistener/

如果问题仍然存在,您可能需要分享一下你的源代码,我想分析一下这是否是一个新的场景。如果您无法共享完整源代码,请尝试创建一个新项目并放置所有必要的代码,然后共享:)

Interesting.
First, try on several scenarios I've checked myself: http://xjaphx.wordpress.com/2011/07/14/listview-doesnt-respond-to-onitemclicklistener/

If problem still, you might want to share your source code, I'd like to analyze if it's a new scenario. In case you cant' share full source, then try to create a new project and put all necessary code, and share :)

圈圈圆圆圈圈 2024-12-14 12:24:26

我发现的另一种情况(xjaphx 未列出):我的行布局有一个 textview 和两个图像,其中一些有“clickable = true”。将其设置为“false”解决了该问题。

One more scenario I have found (not listed by xjaphx): my row layout has had a textview and two images, and some of them has had "clickable = true". Setting it to "false" fixed the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文