开发者

Android ListView item from custom adapter not selectable

I'm making a custom adapter so that I can display a list of items with icons which looks like the menu that comes up when you long click the home screen.

For some reason though the list items are not clickable. The can be navigated to with the D-pad but they cannot be clicked in any way. I thought maybe the problem was with the AlertDialog I was using so I replaced a working adapter I had elsewhere but I have the same issue there.

My adapter looks like this:

ArrayList<IconListItem> mItems;
LayoutInflater mInflater;

public IconListAdapter(Context context, ArrayList<IconListItem> items) {
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mItems = items;
}

public IconListAdapter(Context context) { 
    this(context, new ArrayList<IconListItem>()); 
}

public void add(IconListItem item) {
    mItems.add(item);
    notifyDataSetChanged();
    return item;
}

public void remove(IconListItem item) {
    mItems.remove(item);
    notifyDataSetChanged();
}

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

@Override
public IconListItem getItem(int position) {
    return mItems.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    if(convertView == null){
        view = mInflater.inflate(R.layout.two_line_icon_list_item, null);
    } else {
        view = convertView;
    }


    IconListItem item = mItems.get(position);
    TextView lineOne = (TextView) view.findViewById(R.id.firstLine);
    TextView lineTwo = (TextView) view.findViewById(R.id.secondLine);
    ImageView iconImage = (ImageView) view.findViewById(R.id.icon);

    lineOne.setVisibility(View.VISIBLE);
    lineTwo.setVisibility(View.VISIBLE);
    iconImage.setVisibility(View.VISIBLE);

    lineOne.setText(item.getText());

    if (item.getSubtext() == null)
        lineTwo.setVisibility(View.GONE);
    else 
        lineTwo.setText(item.getSubtext());



    if (item.getIcon() == null)
        iconImage.setVisibility(View.GONE);
    else 
        iconImage.setImageDrawable(item.getIcon());


    return view;
}

And the XML it's inflating:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="6dip"
>
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dip"
    />
    <LinearLayout
        android:orientation="vertical"  
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="fill_parent"
    >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            andr开发者_如何学Gooid:id="@+id/firstLine"
            android:ellipsize="marquee"
            android:inputType="text"
        />
        <TextView  
            android:layout_width="wrap_content"
            android:layout_height="0dip"
            android:layout_weight="1"          
            android:ellipsize="marquee"
            android:id="@+id/secondLine"
            android:inputType="text"
        />           
    </LinearLayout>
</LinearLayout>

EDIT:

When I say they are not clickable, I mean that they items do not highlight in orange when I press them nor does onItemClick ever get called.

In my code I have this, a different adapter for another purpose and it works perfectly

this.foos= foos;
fa = new FooAdapter(this.foos);
fooList.setAdapter(fa);

// View the details for an item when it is selected
fooList.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        Intent i = new Intent(MyActivity.this, MyOtherActivity.class);
        i.putExtra("foo", ((fooAdapter)arg0.getAdapter()).getItem(arg2));
        i.putExtra("list_position", arg2);
        MyActivity.this.startActivityForResult(i, 0);
    }
});

But when I swap out the first 3 lines for

IconListAdapter ila = new IconListAdapter(this);
ila.add(0, "Test 1", android.R.drawable.ic_menu_mylocation);
ila.add(0, "Test 2", android.R.drawable.ic_menu_edit);
ila.add(0, "Test 3", android.R.drawable.ic_menu_search);
groupList.setAdapter(ila);

it stops working.


I found the problem. I deleted lines from my layout file one by one until I found the cause.

The lines that were causing the problem were the two lines android:inputType="text". They must have been causing the system to think that the two TextViews were inputs. I changed it to android:inputType="none" and now it works.

It certainly would have been nice if the huge tooltip saying that singleLine was deprecated had suggested that.


After spending few hours, I found that if this property is set on any of the textviews within your list then listview items will not be selectable:

android:textIsSelectable="true"


You need to implement setOnItemClickListener which will be called when an item in your ListView is clicked.

Something like this:

ListView lv = (ListView)findViewById(R.id.your_list_view);

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
        Log.v(TAG, "Item clicked");
        // whatever you want to do with the clicked item...
    }
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜