如何在listview中单击列表项时调用新的类视图?

时间:2011-05-24 07:01:18

标签: android listview

我正在android中开发一个应用程序,我必须在列表视图中点击一个项目来调用一个类。我已经为它开发了类但是在单击时没有调用类视图。我不明白为什么?

这是我要调用的类的代码

public void onItemClick(AdapterView parent, View v, int position, long id)
     {
         Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");

         Intent itemintent = new Intent(this,ShowDescription.class);

         Bundle b = new Bundle();
         b.putString("title", feed.getItem(position).getTitle());
         b.putString("description", feed.getItem(position).getDescription());
         b.putString("link", feed.getItem(position).getLink());
         b.putString("pubdate", feed.getItem(position).getPubDate());

         itemintent.putExtra("android.intent.extra.INTENT", b);

         startSubActivity(itemintent,0);
     }


    private void startSubActivity(Intent itemintent, int i) {
        // TODO Auto-generated method stub

    }
提前

和thanx

2 个答案:

答案 0 :(得分:0)

将您的代码更改为

public void onItemClick(AdapterView parent, View v, int position, long id)
{
ListView feed = (ListView) findViewById(R.id.yourID);
Intent itemintent = new Intent(this,ShowDescription.class);

 itemintent.putExtra("title", feed.getItemAtPosition(position).getTitle());
 itemintent.putExtra("description", feed.getItemAtPosition(position).getDescription());
 itemintent.putExtra("link", feed.getItemAtPosition(position).getLink());
 itemintent.putExtra("pubdate", feed.getItemAtPosition(position).getPubDate());

 startActivity(itemintent);

}

我希望这里的“feed”是你的ListView。如果要在下一个活动中获取值,请使用以下命令: -

 Bundle extra= getIntent().getExtras();
 String title = extra.getString("title");
 String description = extra.getString("description");
 String link = extra.getString("link");
 String pubdate = extra.getString("pubdate");

这将有所帮助..如果问题仍然存在,请回复..

答案 1 :(得分:0)

listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {


                    Intent arDetail = new Intent(this,ShowDescription.class);
                    arDetail.putString("title", feed.getItem(position).getTitle());
    arDetail.putString("description", feed.getItem(position).getDescription());
    arDetail.putString("link", feed.getItem(position).getLink());
   arDetail.putString("pubdate", feed.getItem(position).getPubDate());
                    startActivity(arDetail);

                }
            });