0

我正在尝试实现自定义列表视图,但在 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:

公共类 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);
                    }

                }
            });

......

和自定义适配器:

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;
}

}

4

1 回答 1

1

您希望单击的项目位置OnItemClickListener由 直接提供arg2。您不需要检查getSelectedItemPosition,它实际上返回“选定的项目”而不是单击的项目,并且应该仅与 . 一起使用OnItemSelectedListener

ListView 的 Child 元素也很复杂,只是在TextView. 考虑用 Single textview 替换整个子布局,并将图像作为 coumpound 的 drawableLeft 属性传递TextView

于 2013-05-25T08:46:04.880 回答