0

This happened every time an item was selected. I have a ListView with an OnItemClickListener and when the event is triggered it changes the property text on the TextView to some random string, after that the selected items turned into unselected.

But if I comment the line where text is changed, run again the application and fired again the same event the item will stay selected.

Here is the code, I reduced it to the simplest way I could.

public class TestsActivity extends Activity {
ListView optionsListView;
TextView text;
String[] stringArray = { "Option 1", "Option 2", "Option 3", "Option 4",
        "Option 5", "Option 6", "Option 7", "Option 8", "Option 9",
        "Option 10" };
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout);       
    optionsListView = (ListView) findViewById(R.id.list);
    OptionsAdapter ItemsAdapter = new OptionsAdapter(this, R.id.list,stringArray);
    optionsListView.setAdapter(ItemsAdapter);
    text = (TextView)findViewById(R.id.textView);
    optionsListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position,
                long id) {
            Toast.makeText(getApplicationContext(), "Clicked" + position, Toast.LENGTH_SHORT).show();
            text.setText("Something"); // <- If you comment this, it will work
        }
    });
}

private class OptionsAdapter extends BaseAdapter {
    String[] options;

    public OptionsAdapter(Context context, int textViewResourceId,
            String[] options) {
        this.options = options;
    }

     @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater vi = LayoutInflater.from(getApplicationContext());
            convertView = vi.inflate(R.layout.list, null);
        }
        TextView post = (TextView) convertView.findViewById(R.id.post);
        post.setText(options[position]);
        return convertView;
    }

    public int getCount() {
        return options.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
}
}

list_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2" >

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:text="textView" >
</TextView>

<ListView
    android:id="@+id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:listSelector="#AABBCC" >
</ListView>

</LinearLayout>

list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content" >

   <TextView
       android:id="@+id/post"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:gravity="center_vertical" />

</LinearLayout>

If you removed from the list_layout.xml the lines "android:layout_weight="1"" the selection will work normally but i need that piece of code in my real layout, it is only an example

Nexs
  • 1

1 Answers1

0

When you change the text, the view is relaid out because of change in content and especially the weight needs to reposition the elements as well. This is why the focus is reset. This is the root cause...

Maybe you should consider to make the TextView not focusable or save and restore the focus of the listview item.

Prakash Nadar
  • 2,694
  • 1
  • 19
  • 20
  • I assume that you restored the focus in the event handler, that is not going to work because, it if after the event handler the relaying out is done. I am sorry, I do not know how to solve it exactly, but I understand the cause of the problem. – Prakash Nadar May 11 '12 at 15:38