我試圖用左邊的字符串(食物項目名稱)和右邊的項目計數編號填充ListView
。我做了一個CustomAdapter
類(見下文),它在繼續下面的一些教程之後擴展了BaseAdapter
,我可以看到(通過打印語句)至少調用了getCount()
方法,但我的getView()
方法沒有被調用,我的ListView
也沒有沒有出現在我的應用程序中。我究竟做錯了什麼?我認爲這是因爲我不知道如何使getView()
在正確的地方運行。當我使用正常的ArrayAdapter
和一串字符串時,它工作正常。自定義ListView不顯示任何東西
這裏是我試圖在我的MainActivity類別填充ListView控件:
CustomAdapter customAdapter = new CustomAdapter();
ListView lv1 = (ListView) findViewById(R.id.listView);
lv1.setAdapter(customAdapter);
customAdapter.notifyDataSetChanged();
現在,這裏是我的CustomAdapter類:
import java.util.ArrayList;
import java.util.List;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
List<foodItem> mItemList = getDataForListView();
public CustomAdapter(List<foodItem> list){
this.mItemList = list;
// tried this as an alternative
}
@Override
public int getCount() {
return mItemList.size();
}
@Override
public foodItem getItem(int arg0) {
return mItemList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup parent) {
System.out.println("Item list size: " + mItemList.size()); // doesn't show anything in console
if(arg1==null) {
LayoutInflater inflater = (LayoutInflater) LayoutInflater.from(parent.getContext());
arg1 = inflater.inflate(R.layout.listrow, parent ,false);
// R.layout.listrow is an XML file defined and formatted at 60sp tall
// as that is how tall I want each item in the ListView to be
}
TextView foodItem = (TextView)arg1.findViewById(R.id.foodTitle);
TextView likeCount = (TextView)arg1.findViewById(R.id.likeCount);
foodItem i = mItemList.get(arg0);
foodItem.setText(i.getItem());
likeCount.setText(i.getLikes());
return arg1;
}
public List<foodItem> getDataForListView() {
List<foodItem> itemList = new ArrayList<foodItem>();
// Test method populating with some data
for(int i=0; i<50; i++) {
itemList.add(new foodItem(i, "Item " + i));
}
return itemList;
}
}
我也試圖加入我自己的構造,但結果是一樣的。任何幫助將不勝感激!
編輯:我的主要活動佈局:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.inventoryList.MainActivity" >
<ListView
android:id="@+id/foodOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
</RelativeLayout>
末,這裏是我的listrow.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:orientation="horizontal"
android:padding="5sp" >
<TextView
android:id="@+id/foodTitle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="12sp"
android:text="123"
android:gravity="center" />
<TextView
android:id="@+id/likeCount"
android:layout_width="12sp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="@+string/likeCount"
android:textSize="10sp"
tools:ignore="SmallSp" />
</RelativeLayout>
在調用setAdapter之後是否將任何項目添加到適配器? – Harry 2015-02-06 17:02:27
在調用'lv1.setAdapter(customAdapter);' – 2015-02-06 17:04:00
後'listrow.xml'和您的活動佈局後,嘗試調用'customAdapter.notifyDataSetChanged()'。 – njzk2 2015-02-06 17:31:17