Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

java - filtered list item open wrong activity


        list = new ArrayList<String>();
//videos list
        list.add("Video1");
        list.add("Video2");
        list.add("Video3");
        list.add("Video4");
        list.add("Video5");
        list.add("Video6");

        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);
//opens activities
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {


                switch (Integer.parseInt((String) adapter.getItem(position))) {
                    case 0:
                        Intent newActivity = new Intent(list_videos.this,Video1.class);
                        startActivity(newActivity);
                        break;

                }
            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {

                adapter.getFilter().filter(s);

                return false;
            }
        });




    }

}

I have created a list-view and on top of that I added a search-bar.

When I use the search-bar, to filter the results... when I click on item 7, instead of opening the specific clicked activity i.e. 7, it always starts from the first one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have only one case statement for the switch which is just:

case 0:

I think you should add the rest of the cases.

I also see that you are trying to parse the integer from the Strings "Video1, Video2 ...etc" in your case you should start from case 1 since your list's string-integer combination starts from "1" (i.e. Video1).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...