Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

ListView search

I have a listview with search that works correctly except displaying the correct details from a second array dependant on the listview arrays position. if i dont search the list everything displays perfectly after the item click but if i search then click on the item it returns the item position not the array position how would i get the item array position.

public class ActivityShotList extends Activity {

    private String[] shotlistArray;
    private String[] shotDetailsListArray;
    private ListView listShots = null;
    ArrayAdapter<String> adapter;
    EditText inPutSearch;
    int dt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listShots = (ListView) findViewById(R.id.listViewShotsReport);
        shotlistArray = getResources().getStringArray(R.array.shotslist);
        shotDetailsListArray = getResources().getStringArray(
                R.array.shotsDetails);

        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1,
                shotlistArray);

        listShots.setAdapter(adapter);

        inPutSearch = (EditText) findViewById(R.id.inputSearch);



        listShots.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                // Get the item that was clicked   [position] 
                Object itemName = listShots.getItemAtPosition(position);
                String keyword = itemName.toString();
                String info = shotDetailsListArray[position];


                //Toast.makeText(getApplicationContext(),"You have selected:  " + keyword, Toast.LENGTH_SHORT).show();
                //
                Intent i = new Intent(getBaseContext(),
                        ActivityShotDetails.class);
                i.putExtra("info", info);
                i.putExtra("name", keyword);
                startActivity(i);

        }
        });

        inPutSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int start, int before, int count) {
                ActivityShotList.this.adapter.getFilter().filter(cs);

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

    }
}

4 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Ah, I think I understand. If you have a list of 10 items, but search filters that to only 2 items, then tapping on an item gives you 0 or 1, which doesn't correspond to the item's position in the full list. Is that correct?

To make it work with your current setup, you need to find the actual index of the item in the search results. Since you have string arrays, the easiest way is probably just to loop through the array and get the index where shotListArray[i].equals(keyword). Then you can use that index instead of position, i.e.

Object itemName = listShots.getItemAtPosition(position);
String keyword = itemName.toString();
int index = -1;
for (int i = 0; i < shotListArray.length; i++) {
  if (shotListArray[i].equals(keyword)) {
    index = i;
  }
}
if (index >= 0) {
  String info = shotDetailsListArray[index];
  // etc...
}
else {
  // error for some reason!
}

As an alternative, you could create a custom Java object that ties your main string (items in R.array.shotslist) with the detail strings (items in R.array.shotDetails). Then you'd have the information you need when tapping on the item in the filtered results. This would require a custom list adapter, which is a whole other thing (that we haven't covered at Treehouse yet).

Thank you sir. I tried this code and still no luck I even tried different combinations. I could send you the complete file if you would have time some time in the future. I will continue to read and search the net for other possibilities before working on learning a custom list view. Again thank you for your support.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

If you zip up your project and email it to help@teamtreehouse.com then it will get funneled to me. :) I'm a bit backed up with support right now but I will try to take a look in a timely manner. Meanwhile, if you make any progress, let us know in this thread.

this was my final answer and it worked. again thank you for the support

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            // Get the item that was clicked   [position] 
            Object itemName = listShots.getItemAtPosition(position);
            String keyword = itemName.toString();
            List<String> shotlists = Arrays.asList(shotlistArray);
            int index = shotlists.indexOf(keyword);
            String info = shotDetailsListArray[index];




            Intent n = new Intent(getBaseContext(),
                    ActivityShotDetails.class);
            n.putExtra("info", info);
            n.putExtra("name", keyword);
            startActivity(n);

    }
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Awesome - glad you got it working! :)