2017-03-29 94 views
0

我只是在關於「目的地」信息機器人試圖自定義的ListView(打勾的),這對我來說很陌生,因爲在logcat的,但列表視圖沒有錯誤犯規顯示的項目列表視圖無法顯示

我已經查找了一整天的可能錯誤,但我可以找出真正的原因。請幫幫我!謝謝!

這是我的源:

的ListView適配器的代碼:

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,列表視圖僅僅是一個從它的一部分

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

請幫幫我!它讓我困惑了幾天。謝謝!

回答

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

THX !!!!!!它解決了我的問題!非常感謝!!! –