ONItemClickListener不起作用

时间:2013-05-25 08:37:56

标签: android android-listview onclicklistener

我正在尝试实现自定义列表视图,但是在ItemClickListener上不起作用。我有活动:Pregled和此活动必须从Activty扩展,而不是从ListActivity扩展,因为我想添加一些其他功能。我使用自定义ListView。代码是这样的:

list_item_choice.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="fill_parent"
android:orientation="vertical"
android:background="@android:color/white"
android:gravity="bottom" >
<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white" >
  <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_toLeftOf="@+id/name"         
         />
  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/icon"
        android:orientation="vertical" >
  <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#003366"      
      />
    </LinearLayout>
  </RelativeLayout>
  </LinearLayout>

activity_pregled.xml:

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

<ListView
    android:id="@+id/ChoiceList"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1" 
  >
</ListView>

</LinearLayout>

Pregled.java:

public class Pregled扩展了Activity {

ListView choiceList;
ArrayList<Choice> choice;
AdapterView.AdapterContextMenuInfo info;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pregled_main);
    setTitle("Преглед");

    choiceList = (ListView) findViewById(R.id.ChoiceList);
    choice = new ArrayList<Choice>();
    Choice c;
    c = new Choice();

    c.setIcon(R.drawable.coffee);
    c.setName("Кафулиња");
    choice.add(c);
    c = new Choice();
    c.setIcon(R.drawable.nokniklubovi);
    c.setName("Ноќни клубови");
    choice.add(c);
    c = new Choice();
    c.setIcon(R.drawable.pivnici);
    c.setName("Пивници");
    choice.add(c);
    c = new Choice();
    c.setIcon(R.drawable.restorani);
    c.setName("Ресторани");
    choice.add(c);
    c = new Choice();
    c.setIcon(R.drawable.nacionalnirestorani);
    c.setName("Национални ресторани");
    choice.add(c);
    c = new Choice();
    c.setIcon(R.drawable.hoteli);
    c.setName("Хотели");
    choice.add(c);
    c = new Choice();
    c.setIcon(R.drawable.znamenitosti);
    c.setName("Знаменитости");
    choice.add(c);

    choiceList.setAdapter(new CustomAdapter(choice, this));

    choiceList.setClickable(true);

    choiceList
            .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    if (arg0.getSelectedItemPosition() == 0) {
                        // кафулиња
                    } else if (arg0.getSelectedItemPosition() == 1) {
                        // ноќни клубови
                    } else if (arg0.getSelectedItemPosition() == 2) {
                        // пивници

                    } else if (arg0.getSelectedItemPosition() == 3) {

                        // ресторани
                        Intent k = new Intent(Pregled.this, Restorani.class);
                        startActivity(k);

                    } else if (arg0.getSelectedItemPosition() == 4) {
                        // национални ресторани

                    } else if (arg0.getSelectedItemPosition() == 5) {
                        // хотели
                        Intent k = new Intent(Pregled.this, Hoteli.class);
                        startActivity(k);
                    } else if (arg0.getSelectedItemPosition() == 6) {
                        // знаменитости
                        Intent k = new Intent(Pregled.this,
                                Znamenitosti.class);
                        startActivity(k);
                    }

                }
            });

.....

和CustomAdapter:

public class CustomAdapter extends BaseAdapter {
private ArrayList<Choice> _data;
Activity _c;

CustomAdapter(ArrayList<Choice> data, Activity c) {
    _data = data;
    _c = c;
}

public int getCount() {
    // TODO Auto-generated method stub
    return _data.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return _data.get(position);
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) _c
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.list_item_choice, null);
    }

    ImageView image = (ImageView) v.findViewById(R.id.icon);
    TextView fromView = (TextView) v.findViewById(R.id.name);

    Choice msg = _data.get(position);
    image.setImageResource(msg.icon);
    fromView.setText(msg.name);

    return v;
}

}

1 个答案:

答案 0 :(得分:1)

您希望OnItemClickListener中由arg2直接提供的点击项目位置。您无需检查getSelectedItemPosition,它实际上会返回“所选项目”而不是点击的项目,只能与OnItemSelectedListener一起使用。

ListView的Child元素也是复杂的,只是为了在TextView的左边有一个图像。考虑使用单个textview替换整个子布局,将要传递的图像作为coumpound TextView的drawableLeft属性。