I have CustomAdapter which I am using for populating ListView with some data.

Each element in ListView has two variables. For each listview (in onItemClick method) I must check this variables and If they are the same - do some code and If they are different - do another code, for example Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();

So I have tried this:

private List<SomeItem> items = new ArrayList();  
//items were created
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);


listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    for(int i=0; i<=items.size(); i++) {
                        SomeItem item = items.get(position);

                        String tmpCI = item.getFirstVariable();
                        String tmpPCI = item.getecondVariable();

                        if (!tmpCI.equals(tmpPCI)) {
                            //some code
                        } else {
                            Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
                        }

                    }
                    }
            });

But all of my listview elements have values of the first element in those two variables.

So how can I do something like item.next(); for validating all of items in listview?

UPD:

Sorry, I will provide more information about what I am doing after checking variables of listview items for understanding my issue.

I have one more adapter:

SomeAnotherAdapter adapterPr = new SomeAnotherAdapter(this, R.layout.list_tem_another, itemsAnother);

and one more listview:

listViewAnother.setAdapter(adapterPr);

First of all I understood, that first variable should be from first listview and the second variable from another listview.

In this listViewAnother I have many items, which has some "id". For example 1st, 5th and 20th elements have id 90 and other elements have id 100. We can say, that items from the first listview also have "id".

So I must check if(first variable = second variable) and then show in listViewAnother only items that have id which equals ID from clicked item in listView.

I tried: adapterPr.remove(item2); but then I understood, that I need all of items because I can go back to listView and press another item which will need those removed elements.

Now, hope I provided full information and you will be able to help me improve my code.

1 Answers

0
tobiasfried On

Do you need to perform the check on every element of the adapter when you click on one element of the adapter? If not, you don't need a loop. If you do, your loop should be iterating over the original list, and does not need adapter position at all.

In general when using adapters and lists, you should use the adapter's position and the adapter's data set to perform any tasks. It's not good practice to use the adapter position to get an item from the original list.

Simply set one onItemClickListener which gets the corresponding item from the adapter, and do what you need to from there:

private List<SomeItem> items = new ArrayList();  
//items were created
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);


listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        SomeItem item = adapter.getItem(position);

        String tmpCI = item.getFirstVariable();
        String tmpPCI = item.getecondVariable();

        if (!tmpCI.equals(tmpPCI)) {
            //some code
        } else {
            Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
        }

    }
});