开发者

How to show the selected item in ListView (Android)

The problem is when the ListView loses focus, the selected item isn't highlighted.

I use a custom ArrayAdapter which uses the following layout for items:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ListTextBox"
    android:layout_width="fill_parent"
    android:开发者_如何学Pythonlayout_height="wrap_content"
    android:padding="@dimen/Padding"
    android:textSize="@dimen/TextSize"
  />

The ListSelector is a simple Shape drawable.

Thank you

edit...

Here is the ArrayAdapter:

listWords = new ArrayList<String>();
arrayAdapter = new DictListAdapter(this, R.layout.listtext, listWords); 
list.setAdapter(arrayAdapter);

and the code for DictListAdapter:

public class DictListAdapter extends ArrayAdapter<String> {
    public DictListAdapter(Context context, int textViewResourceId,
            List<String> objects) {
        super(context, textViewResourceId, objects);
        this.context = (MainWindow) context;
        this.objects = objects;
        this.res = textViewResourceId;
    }

    Typeface font;

    private List<String>  objects;
    private final MainWindow   context;

    int res;
    int gravity=Gravity.LEFT;

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public String getItem(int position) {
        return objects.get(position);
    }

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

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

        String obj = objects.get(position);

        TextView tv = new TextView(context, null, res);
        tv.setText(obj);
        tv.setTypeface(context.getFont());
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 
context.getResources().getDimension(R.dimen.TextSize));
        tv.setGravity(gravity);

        return tv;
        }

    public void setGravity(int g){
        this.gravity=g;
    }
}


i guess you have created array adapter in the following manner. if i am correct use the following:

static final String[] color = new String[]{"red","green","blue"};

We need to use the setListAdapter(..) method of the ListActivity class to bid the two (data and view). Android provides many implementations of the ListAdapter. We will use the simple ArrayAdapter.

What does the ArrayAdapter expect?

It expects the context object, the row layout and the data in an array.

So here it is:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, PENS));
    getListView().setTextFilterEnabled(true);       
  }

use below code to show selected item

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Object o = this.getListAdapter().getItem(position);
    String pen = o.toString();
    Toast.makeText(this, "You have chosen the color: " + " " + color, Toast.LENGTH_LONG).show();
}


The simplest way is to add android:background="?android:attr/activatedBackgroundIndicator" in the layout you use for the row

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜