在android studio中打开listView项目的问题

时间:2015-10-12 16:58:03

标签: android listview android-studio

我在列表中有一个包含2个项目的listView,位置0的第一个项目响应了点击次数,但另一个项目没有响应!我试图复制相同的onclick方法,并将if语句更改为位置1中的列表项,但它不适合我,所以我正在寻找如果有人可以帮助我如何工作?我已经包含了我认为解决此问题所需的唯一代码,



public class TopLevelActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top_level);
        //Create an OnItemClickListener
        AdapterView.OnItemClickListener itemClickListener =
            new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> listView,
                                        View v,
                                        int position,
                                        long id) {
                    if (position == 0) {
                        Intent intent = new Intent(TopLevelActivity.this,
                                                   DrinkCategoryActivity.class);
                        startActivity(intent);
                    }
                }
            };
        //Add the listener to the list view
        ListView listView = (ListView) findViewById(R.id.list_options);
        listView.setOnItemClickListener(itemClickListener);
    

      //what ive tried to open the foodCategoryActivity list item

AdapterView.OnItemClickListener itemClickListener =
            new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> listView,
                                        View v,
                                        int position,
                                        long id) {
                    if (position == 1) {
                        Intent intent = new Intent(TopLevelActivity.this,
                        
                        FoodCategoryActivity.class);
                        startActivity(intent);
                    }
                }
            };
        //Add the listener to the list view
        ListView listView = (ListView) findViewById(R.id.list_options);
        listView.setOnItemClickListener(itemClickListener);
    }
}
&#13;
&#13;
&#13;

谢谢。

2 个答案:

答案 0 :(得分:2)

您只能设置一个OnItemClickListener

您需要通过position或View对象本身来区分已实现的方法:

AdapterView.OnItemClickListener itemClickListener =
        new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> listView,
                                    View v,
                                    int position,
                                    long id) {
                if (position == 0) {
                  //code for drink category
                }
                else {
                  //code for food category
                }
            }
        };
    //Add the listener to the list view
    ListView listView = (ListView) findViewById(R.id.list_options);
    listView.setOnItemClickListener(itemClickListener);

答案 1 :(得分:2)

目前为同一个ListView使用两个不同的点击侦听器,对不同的项目执行不同的操作,单击onItemClick,根据ListView中的单击位置无法执行任务。

使用单击监听器和AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> listView, View v, int position, long id) { if (position == 0) { } else if (position == 1) {{ } } }; 方法内部使用switch-case或if-else梯来执行以下任务:

ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);

并删除您正在使用两次的以下行:

listView

因为无需创建setOnItemClickListener并多次调用 <!DOCTYPE html> <html> <head> <link rel='stylesheet' type='text/css' href='stylesheet.css'/> <script type='text/javascript' src='script.js'></script> </head> <body> <div class="header"> </div> <div class="main"> <div id="comic"></div> <div id="caption"></div> <div id="prompt"> <h3>Hello, world!</h3> <div class="btn" id="yes"><img src="YESsmall.jpg"/></div> <div class="btn" id="no"><img src="NOsmall.jpg"/></div> </div> </body> </html> ,所以只需执行一次。