ListActivity中的ListView项目仍然无法点击

时间:2014-09-08 16:31:19

标签: android listview android-listview listactivity

在我的应用程序中有一个Main Acitvity(扩展了List Activity),我有一个列表视图。我想让这个列表视图的项目可以点击并处理点击事件。这是我的代码:

public class MainActivity extends ListActivity {
    private ArrayList<Item> m_parts = new ArrayList<Item>();
    private ItemAdapter m_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    (…)
    m_adapter = new ItemAdapter(this, R.layout.item_layout, m_parts);
    setListAdapter(m_adapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        (…)
    }


}

这是我的主要活动布局的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main_background"
tools:context=".MainActivity">

<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
</ListView>


</LinearLayout>

这是我的Item类:

 public class Item {
 private Bitmap image;
 private Uri uri;
 private String title;
 private String date;
 private String latitude;
 private String longitude;
 public CheckBox checkBox = null;

public Item(){}

public Item(Bitmap bi, Uri ur, String ti, String da, String la, String lo){
    this.image = bi;
    this.uri = ur;
    this.title = ti;
    this.date = da;
    this.latitude = la;
    this.longitude = lo;
}
public Bitmap getImage() {return image;}

public String getTitle(){
    return title;
}

public String getDate(){
    return date;
}

public String getLatitude(){
    return latitude;
}

public String getLongitude(){
    return longitude;
}

public Boolean isChecked(){
    return checkBox.isChecked();
};

public Uri getUri(){
    return uri;
}
}

项目布局:

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

<ImageView
    android:id="@+id/photo"
    android:layout_width="60dp"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:scaleType="fitXY"/>

<TextView
    android:id="@+id/place"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/photo"
    android:textColor="@color/white"
    android:textColorHighlight="@color/white"
    android:textColorHint="@color/white"
    android:textColorLink="@color/white"
    android:hint="@string/founding_place_text" />

<TextView
    android:id="@+id/date"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/photo"
    android:textColor="@color/white"
    android:textColorHighlight="@color/white"
    android:textColorHint="@color/white"
    android:textColorLink="@color/white"
    android:hint="@string/founding_date_text" />

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/photo"
    android:maxLines="1"
    android:textColor="@color/white"
    android:textColorHighlight="@color/white"
    android:textColorHint="@color/white"
    android:textColorLink="@color/white"
    android:hint="@string/founding_title_text_2" />

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"/>
</RelativeLayout>

最终我的物品适配器:

public class ItemAdapter extends ArrayAdapter<Item>{
private ArrayList<Item> objects;


public ItemAdapter(Context context, int textViewResourceId, ArrayList<Item> objects){
    super(context, textViewResourceId, objects);
    this.objects = objects;
}

public View getView(int position, View convertView, ViewGroup parent){
    View v = convertView;
    if(v == null){
        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.item_layout, null);
    }
    Item i = objects.get(position);
    if(i != null){
        ImageView im = (ImageView)v.findViewById(R.id.photo);
        TextView ti = (TextView)v.findViewById(R.id.title);
        TextView da = (TextView)v.findViewById(R.id.date);
        TextView pl = (TextView)v.findViewById(R.id.place);
        CheckBox ch = (CheckBox)v.findViewById(R.id.checkBox);


        if(im != null){
            im.setImageBitmap(i.getImage());
        }

        if(ti != null){
            if(i.getTitle() != null && i.getTitle() != ""){
                ti.setText(i.getTitle());
            }else{
                ti.setText("No title");
            }

        }

        if(da != null){
            da.setText(i.getDate());
        }

        if(pl != null){
            if(i.getLatitude()!=null && i.getLongitude()!=null){
                pl.setText(i.getLatitude() + ", " + i.getLongitude());
            }else{
                pl.setText("No coordinates");
            }

        }

        if(ch != null){
            i.checkBox = ch;
        }
    }
    return v;
}
}

我的问题是当我点击列表视图中的任何项目时,不会调用onListItemClick(...)。更重要的是,它们甚至没有突出显示。您是否知道如何添加到我的代码来处理点击事件?我使用的是Android 4.3。

2 个答案:

答案 0 :(得分:0)

不要将列表视图设置为可点击,也不要设置OnItemClickListener。 (换句话说,通过调用onCreate()来结束setlistAdapter方法。)您只需要覆盖onListItemClicked; ListActivity基础设施负责其余部分。通过自己设置一个监听器,你就会干扰框架的机制。

答案 1 :(得分:0)

从布局中删除android:clickable =“true”并从代码中删除listView.setClickable(true)并设置listView.setOnItemClickListener并放入android:layout_height =“wrap_content”android:layout_width =“match_parent