Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give listview items tags without custom arrayadapter

I've got a question that should be simple, but I can't seem to solve it.

I'm using DragSortListView. I want to give each listview-item a tag with it's original location. When the user is done reordering I want to return a string with original id's (such as 2,3,4,1 if the user moved the first item to the last place). If a item is deleted, I also would like to know the orignal ID.

Here is my code.

DragSortListView mainListView = (DragSortListView) findViewById( R.id.list_pages );  
String[] names = settings.getString("pages", "Error,error").split(",");    
ArrayList<String> list = new ArrayList<String>(Arrays.asList(names));
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.row, list);
mainListView.setAdapter(adapter);
DragSortController controller = new DragSortController(mainListView);
...

Now I thought it would be best to loop through all items and give them tags with their original id's. I'm trying to loop through the items using something like this

for (int i = 0; i < mainListView.getCount(); i++)
{
  View v = mainListView.getChildAt(i);
  TextView tx = (TextView) v.findViewById(R.id.text);
  tx.setTag(i);
}

However this listview appears to be empty, even though the adapter has been set.

I can't use a custom ArrayAdapter, as the app will crash when an item is dropped or swiped away.

Additional info: The listview is populated when data is returned by a service using a BroadcastReceiver. I don't think this influences this, but you never know.

My question:

  • How do I store each list-items original position, so that I can access it later on?

Thanks in advance!

like image 455
Vic V Avatar asked Jun 24 '13 20:06

Vic V


1 Answers

If you don't want to use a custom Adapter, then you can just do it like this:

    List<Item> dataForTheAdapter = new ArrayList<Item>();

    // 'dataRetrivedfromService' is the data, that gets returned by the service
    for (int counter = 0; counter < dataRetrivedfromService.size(); counter++) {
        dataForTheAdapter.add(new Item(dataRetrivedfromService.get(counter), counter));
    }

    mContext = MyActivity.this;
    mAdapter = new ArrayAdapter<Item>(mContext, R.layout.row, dataForTheAdapter);

    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(mAdapter);

This code is pretty self explanatory. Now looking at the Item.java class, you'll see that I overrode the toString() method. The toString() method get's called on the supplied object the ListView want's to fill up its data with.

public class Item {

    private String mText;
    private int mPosition;

    public Item(String text, int position) {

        this.mText = text;
        this.mPosition = position;
    }

    public int getPosition() {

        return mPosition;
    }

    public void setPosition(int position) {

        this.mPosition = position;
    }

    public String getText() {

        return mText;
    }

    public void setText(String text) {

        this.mText = text;
    }

    @Override
    public String toString() {

        return this.mText;
    }
}

Now you won't have to write a custom Adapter, but can fill up your ListView with an Item's object.

To test this if it works, you can set an OnItemClickListener on your ListView and do this:

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                Toast.makeText(mContext, String.valueOf(mAdapter.getItem(position).getPosition()), Toast.LENGTH_SHORT).show();
            }
        });
like image 132
Ahmad Avatar answered Nov 17 '22 20:11

Ahmad