我只是尝试在android中自定义ListView(带复选框)" destination"信息,对我来说很奇怪,因为Logcat没有错误,但Listview没有显示项目
我已经搜索了一整天的可能错误,但我可以找出真正的原因。请帮我! THX!
这是我的来源:
public class DestinationAdapter extends BaseAdapter {
private List<Destination> destinationList;
private static HashMap<Integer,Boolean> isSelected;
Context context;
LayoutInflater mInflater = null;
public DestinationAdapter(List<Destination> destinationList, Context context) {
super();
destinationList = new ArrayList<Destination>();
this.context = context;
this.destinationList = destinationList;
mInflater = LayoutInflater.from(context);
isSelected = new HashMap<Integer, Boolean>();
init();
}
private void init(){
for(int i=0; i< destinationList.size(); i++) {
getIsSelected().put(i,false);
}
}
@Override
public int getCount() {
return destinationList.size();
}
@Override
public Destination getItem(int position) {
return destinationList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.destination_item, null);
viewHolder = new ViewHolder();
viewHolder.destinationImage = (ImageView) convertView.findViewById
(R.id.destination_image);
viewHolder.destinationName = (TextView) convertView.findViewById
(R.id.destination_name);
viewHolder.placeSelected = (CheckBox) convertView.findViewById
(R.id.place_select);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.destinationName.setText(destinationList.get(position).getName());
viewHolder.destinationImage.setImageResource(destinationList.get(position).getImageId());
viewHolder.placeSelected.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isSelected.get(position)) {
isSelected.put(position, false);
setIsSelected(isSelected);
} else {
isSelected.put(position, true);
setIsSelected(isSelected);
}
notifyDataSetChanged();
}
});
viewHolder.placeSelected.setChecked(getIsSelected().get(position));
return convertView;
}
public static HashMap<Integer,Boolean> getIsSelected() {
return isSelected;
}
public static void setIsSelected(HashMap<Integer,Boolean> isSelected) {
DestinationAdapter.isSelected = isSelected;
}
class ViewHolder {
ImageView destinationImage;
TextView destinationName;
CheckBox placeSelected;
}
}
和我的活动(我删除了其他显示的无关部分):
public class DestinationActivity extends Activity {
private ListView listView;
private List<Destination> destinationList = new ArrayList<Destination>();
private DestinationAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.destination_layout);
listView = (ListView) findViewById(R.id.list_destination);
initDestinations();
adapter = new DestinationAdapter(destinationList, DestinationActivity.this);
listView.setAdapter(adapter);
}
//初始化数据
private void initDestinations() {
Destination destination1 = new Destination("beijing", R.drawable.car);
destinationList.add(destination1);
Destination destination2 = new Destination("shanghai", R.drawable.car);
destinationList.add(destination2);
Destination destination3 = new Destination("hangzhou", R.drawable.car);
destinationList.add(destination3);
Destination destination4 = new Destination("guangzhou", R.drawable.car);
destinationList.add(destination4);
Destination destination5 = new Destination("shenzhen", R.drawable.car);
destinationList.add(destination5);
Destination destination6 = new Destination("zhuhai", R.drawable.car);
destinationList.add(destination6);
Destination destination7 = new Destination("xiamen", R.drawable.car);
destinationList.add(destination7);
Destination destination8 = new Destination("qingdao", R.drawable.car);
destinationList.add(destination8);
Destination destination9 = new Destination("jinan", R.drawable.car);
destinationList.add(destination9);
Destination destination10 = new Destination("zhengzhou", R.drawable.car);
destinationList.add(destination10);
}
}
和目标对象:
public class Destination implements Serializable {
private int id;
private String name;
private int imageId;
private int priority;
private int rank;
// 经度
private double longitude;
// 纬度
private double latitute;
public Destination(double longitude, double latitute){
this.longitude = longitude;
this.latitute = latitute;
}
public Destination(String name, int imageId){
this.name = name;
this.imageId = imageId;
}
public Destination(int id, String name, int priority){
this.id = id;
this.name = name;
this.priority = priority;
}
public double getLongitude()
{
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public double getLatitute(){
return latitute;
}
public void setLatitute(double latitute) {
this.latitute = latitute;
}
public int getId() { return id; }
public String getName() {
return name;
}
public int getImageId() { return imageId; }
public int getPriority(){ return priority; }
public int getRank() {return rank;}
public void setName(String name){
this.name = name;
}
public void setImageId(int imageId){
this.imageId = imageId;
}
public void setId(int id){ this.id = id; }
}
两个布局xml:
item_destination.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/destination_image"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="@+id/destination_name"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginLeft="10dip" />
<CheckBox
android:id="@+id/place_select"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false" />
destination_layout.xml,listview只是它的一部分
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="@drawable/border"
android:orientation="horizontal"
android:paddingBottom="@dimen/vertical_padding"
android:paddingLeft="@dimen/horizontal_padding"
android:paddingTop="@dimen/vertical_padding" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/departure" />
<TextView
android:id="@+id/from_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="From"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_gravity="top"
android:background="@drawable/border"
android:orientation="horizontal"
android:paddingBottom="@dimen/vertical_padding"
android:paddingLeft="@dimen/horizontal_padding"
android:paddingTop="@dimen/vertical_padding" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/departure" />
<ListView
android:id="@+id/list_destination"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="500dp" >
</ListView>
</LinearLayout>
请帮帮我!这让我困惑了几天。 THX!
答案 0 :(得分:0)
从适配器的构造函数中删除以下行。
destinationList = new ArrayList<Destination>();
您正在从适配器内部对列表进行新的引用。
public DestinationAdapter(List<Destination> destinationList, Context context) {
super();
// Doing this you are creating new reference of passed argument. Delete it
destinationList = new ArrayList<Destination>();
// Here you are getting empty list due to above statement
this.destinationList = destinationList;
}