0

When user clicks on item of ListView I show Edit button at this item of list. lastSelectedPosition has the position of the list item that was clicked last.

code:

public class MainActivity extends Activity {
    private ListView listView;
    private MyAdapter adapter;

    // static - If the activity is re-created, the data
    // about list items remain.
    private static List<String> items = loadItems();

    private MyItemClickListener listener = new MyItemClickListener();
    private int lastSelectedPosition = ListView.INVALID_POSITION;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        listView = (ListView) findViewById(R.id.myListView);
        adapter = new MyAdapter(R.layout.my_item, items);
        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setOnItemClickListener(listener);

        if (savedInstanceState != null) {
            // Is recreating the activity because 
            // of the change screen orientation. 

            lastSelectedPosition = savedInstanceState.getInt("lastSelectedPos");
            if (lastSelectedPosition != ListView.INVALID_POSITION) {
                // !!! getChildAt() always returns null !!!
                View listItem = listView.getChildAt(lastSelectedPosition);
                View editButton = listItem.findViewById(R.id.editButton);

                // Only selected list item has visible EditButton, 
                // other list items have not
                editButton.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // Is destroying the activity because 
        // of the change screen orientation.

        outState.putInt("lastSelectedPos", lastSelectedPosition);               
    }

    // Other methods
}

I think that getChildAt() always returns null in Activity.onCreate(), because at that moment the list is not yet fully developed. How I can get View of list item to show Edit button on it?

And sorry for my English.

Thanks

leonidandand
  • 115
  • 1
  • 11
  • possible duplicate of [ListView getChildAt returning null for visible children](http://stackoverflow.com/questions/6766625/listview-getchildat-returning-null-for-visible-children) – rds Oct 06 '13 at 17:09

2 Answers2

1

Your adapter needs to know about this selected position so it can be rendered properly in getView(). Just modify the constructor for your adapter to take the int, and update that int value whenever the onclick is called. In general, you should always have all the information you need to recreate the state of your list items within your adapter.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • **dmon**, now I set visibility of Edit Button in `MyAdapter` and the button to actually will be shown. But if I call `listView.getChildAt(lastSelectedPosition).findViewById(R.id.editButton).setVisibility(View.INVISIBLE)` when user clicks other list item, then button does not disappear visually, but `listView.getChildAt(lastSelectedPosition).findViewById(R.id.editButton).getVisibility()` returns `View.INVISIBLE`. What's wrong? Bug in Android? – leonidandand Apr 06 '13 at 21:08
  • I don't think listView.getChildAt(position) is the thing you want. The listview should only generate as many views as necessary. If you have a list with thousands of items listView.getChildAt(500) will most certainly return null. Where are you trying to make the button invisible from? By the way, INVISIBLE makes the button not visible but it will still take space in the layout. GONE will remove it. – dmon Apr 06 '13 at 21:27
  • Now I'll change main message – leonidandand Apr 06 '13 at 21:32
  • 1
    You should not do that, create a new question if the original was answered. Otherwise the discussion gets muddled. – dmon Apr 06 '13 at 21:39
0

I think it is because you didn't change your lastSelectedPosition's value when user click on item in your listview. Just implement your onItemClickListener like the following code snippet. And change your lastSelectedPosition's value on this. And remove your

private MyItemClickListener listener = new MyItemClickListener(); 

line. I think the value of lastSelectedPosition is couldn't changed until you define your listener variable. And also I didn't know where you define your listener variable.

lv1.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
lastSelectedPosition = arg2
}
});

I hope this will help you.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • No. I not showed full code for simplicity. The code shown here approximate scheme of my real code. In fact, I change the value of lastSelectedPosition in onItemClickListener(). I checked the value of the variable with logs in onCreate() - the variable has actual value after change screen orientation. The only problem is the method getChildAt() - the method returns null. Maybe there is method in ListView like the ListView.setOnListFullyDevelopedListener(OnListFullyDevelopedListener listener)? – leonidandand Apr 06 '13 at 12:35