Addev
Addev

Reputation: 32233

get the View at the position X of a ListView

How can I retrieve the View at the position X in a ListView? I dont want to inflate a new one, just retrieve the cell visible in the screen for change some parameters programmatically

Upvotes: 0

Views: 115

Answers (3)

Vinoth
Vinoth

Reputation: 1349

I didn't clearly understand your problem. But to what I've understood I would suggest you use a frame layout within a linear layout. You can use another frame layout to do your manipulations.

Upvotes: 1

Addev
Addev

Reputation: 32233

Found a dirty solution:

  1. You should be able of identify each row generated. For example adding a TextView with visibility=gone and writing a unique value there when generating (or recycling the row)
  2. In the listactivity call to getListView.setSelection(position) to the desired cell
  3. Survey the listview list for the row (until displayed)

    lv=getListView();
    for (int i=0;i <lv.getChildCount();i++){
       if (((TextView)lv.findViewById(R.id.my_hidden_textview)).getText.equals(mykey)){
       // view found
       } else {
       // schedule another survey "soon"
       }
     }
    

    For the schedule you can use something like:

    final int RETRY_DELAY=100;
    new Handler(){
        public void handleMessage(Message msg){
           if (msg.what<0) return; //something went wrong and retries expired
                   lv=getListView();
           for (int i=0;i <lv.getChildCount();i++){
             if (((TextView)lv.findViewById(R.id.my_hidden_textview)).getText.equals(mykey)){
                //result = lv.findViewById(R.id.my_hidden_textview);
             } else {
                this.sendEmptyMessageDelayed(msg.what-1,RETRY_DELAY);
             }
           }     
        }
    }.sendEmptyMessageDelayed(10,RETRY_DELAY);
    

As I said is a very ugly solution but it works

Upvotes: 1

havexz
havexz

Reputation: 9590

Since views in ListView are re-used/re-cycled. There is no direct way of getting a view reference from the ListView.

If you want to access a view you need to extend ArrayAdapter and then override getView. There you should call the super.getView and write your own custom code.

If we you really need to control more than try extending BaseAdapter or CursorAdapter.

Upvotes: 2

Related Questions