Listview - 使用OnItemClickListener打开一个新活动

时间:2015-12-15 22:06:29

标签: android listview android-activity onitemclicklistener

我有一个listview,当用户使用OnItemClickListener选择列表中的项目时,我正在尝试接收“click”事件。应该发生的是当单击列表项时它应该打开一个新活动,但是,每次运行应用程序并尝试单击任何列表项时都没有任何反应。我一直试图找出我的代码可能出现的问题,但还没有找到解决方案,并希望得到任何建议。

这是我的主要活动代码:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //linking of variable to the UI for list view element with id
    newsListView = (ListView) findViewById(R.id.newsFeedList);
    //create adapter
    adapter = new NewsListAdapter (this, R.layout.adapter_news_list, newsListData);
    //attach adapter to list view
    newsListView.setAdapter(adapter);
    EDANewsApp app = EDANewsApp.getInstance();
    //URL that we want to communicate with
    String url = "http://www.efstratiou.info/projects/newsfeed/getList.php";
    //Attach on item click listener to list view
    newsListView.setOnItemClickListener(listListener);
    //Create a request(we expect a JSON array as a response
    JsonArrayRequest jsonRequest = new JsonArrayRequest(url, listener, errorListener);
    //Submit the request
    app.requestQueue.add(jsonRequest);
}
AdapterView.OnItemClickListener listListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Create an Intent
        Intent intent = new Intent(MainActivity.this, NewsItemActivity.class);
        NewsRecord record = new NewsRecord();
        //Add parameters
        intent.putExtra("NEWS_ITEM_ID", record.recordId);
        //Retrieve record object for selected news item
        newsListData.get(position);
        startActivity(intent);
    }
};

4 个答案:

答案 0 :(得分:0)

你没有开始新的活动。

Intent intent = new Intent(MainActivity.this, NewsItemActivity.class);

创建意图后,您必须致电

startActivity(intent)

来自活动。在您的代码中将是MainActivity。 另请检查NewsItemActivity是否作为活动添加到清单文件

答案 1 :(得分:0)

就像亚当指出的那样,你错过了:

startActivity(intent)

所以你的代码必须如下:

AdapterView.OnItemClickListener listListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Create an Intent
        Intent intent = new Intent(MainActivity.this, NewsItemActivity.class);
        NewsRecord record = new NewsRecord();
        //Add parameters
        intent.putExtra("NEWS_ITEM_ID", record.recordId);
        //Retrieve record object for selected news item
        newsListData.get(position);
       startActivity(intent)
    }
};

答案 2 :(得分:0)

@Janet Rodriguez: 我遇到了你的问题。你失去了适配器。 见:

adapter = new NewsListAdapter (this, R.layout.adapter_news_list, newsListData);

您在此处设置自定义适配器。

但在这里:

AdapterView.OnItemClickListener listListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {});

在这里,您将获得一个从未在列表视图中使用的不同适配器的onitemclicklistener。 在最后解决方案: 你可以直接从listview调用这个方法。 像:

newsListView .setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(MainActivity.this, NewsItemActivity.class);
    NewsRecord record = new NewsRecord();
    //Add parameters
    intent.putExtra("NEWS_ITEM_ID", record.recordId);
    //Retrieve record object for selected news item
    newsListData.get(position);
   startActivity(intent);

        }
    });

尝试此操作,如果出现更多问题,请回复:

答案 3 :(得分:0)

 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
//linking of variable to the UI for list view element with id
 newsListView = (ListView) findViewById(R.id.newsFeedList);
//create adapter
 adapter = new NewsListAdapter (this, R.layout.adapter_news_list, newsListData);
//attach adapter to list view
newsListView.setAdapter(adapter);
EDANewsApp app = EDANewsApp.getInstance();
//URL that we want to communicate with
String url = "http://www.efstratiou.info/projects/newsfeed/getList.php";
//Attach on item click listener to list view
newsListView.setOnItemClickListener(listListener);
//Create a request(we expect a JSON array as a response
JsonArrayRequest jsonRequest = new JsonArrayRequest(url, listener, errorListener);
//Submit the request
app.requestQueue.add(jsonRequest);

// List View Listener

list.setOnItemClickListener(new OnItemClickListener() {

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

    Intent intent = new Intent(MainActivity.this, NewsItemActivity.class);
    NewsRecord record = new NewsRecord();
    //Add parameters
    intent.putExtra("NEWS_ITEM_ID", record.recordId);
    //Retrieve record object for selected news item
    newsListData.get(position);
    startActivity(intent);          
            }
        });

}

将此代码替换为您的oncreate并请删除clickview上的clickview,这肯定会帮助您,如果不是让我知道我会找到另一个完美的解决方案