這是從以前的問題的後續:ImageButton within row of ListView android not working 但經過來自SO大師的建議,有人建議我發佈一個新問題。 問題是我有一個不顯示任何數據的自定義適配器。我已經研究過其他問題,但沒有提供解決方案。自定義適配器沒有顯示任何項目
在我的主要活動我有幾個按鈕,其中的一個:待辦事項,應該創建一個從SQLite數據庫顯示一排數據,並根據某些因素(主要是日期),它顯示了一個類型的紅綠燈這是存儲爲可繪製的。
這一行中的項目的一部分是一個圖像按鈕,我希望用戶能夠點擊和圖像應該改變。用戶應該也可以點擊實際的行並開始一個新的活動。
我的問題是沒有數據顯示。
所以,這裏是我的代碼:
public class MainActivity extends Activity {
// definitions etc ...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// definitions etc ...
}
public void ToDo(View v){ // the user has clicked in the ToDo button
IgroDatabaseHelper helper = new IgroDatabaseHelper(getBaseContext()); // create instance of SQLIte database
numRows = helper.NumEntries("ToDo"); // Get the number of rows in table
int i = 1;
ArrayList<RowItem> rowItems = new ArrayList<>();
RowItem myItem1;
while (i <= numRows){
// get items from database
// depending on value select different drawable
// put data into List Array of RowItem
myItem1 = new RowItem(TheWhat, R.drawable.teamworka, R.drawable.redtrafficlight, R.drawable.checkbox, TheWhenBy);
rowItems.add(myItem1);
//
i = i+ 1;
}
ListView yourListView = (ListView) findViewById(R.id.list);
CustomListViewAdapter customAdapter = new CustomListViewAdapter(this, R.layout.todo_row, rowItems);
yourListView.setAdapter(customAdapter);
}
的CustomListViewAdapter看起來是這樣的:
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
ArrayList<RowItem> _rowItems;
public CustomListViewAdapter(Context context, int resourceId,
ArrayList<RowItem> rowItems) {
super(context, resourceId);
this.context = context;
_rowItems = rowItems;
System.out.println("I am in the custom Adapter class "+ _rowItems);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
System.out.println("This is the get view");
View row = convertView;
RowItem item = _rowItems.get(position);
// you can now get your string and drawable from the item
// which you can use however you want in your list
String columnName = item.getColumnName();
int drawable = item.getDrawable();
if (row == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = mInflater.inflate(R.layout.todo_row, parent, false);
}
ImageButton chkDone = (ImageButton) row.findViewById(R.id.chkDone);
chkDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
System.out.println("I am in position "+ position);
}
});
return row;
}
}
的RowItem類的樣子:
public class RowItem {
private String _heading;
private int _icon;
private int _lights;
private int _chkdone;
private String _date;
public RowItem(String heading, int icon, int lights, int chkDone, String date) {
_heading = heading;
_icon = icon;
_lights = lights;
_chkdone = chkDone;
_date = date;
System.out.println("adding stuff to my rows");
System.out.println("my column Name is " + heading);
System.out.println("My drawable int is "+ icon);
}
public String getColumnName() {
System.out.println("column Names is "+ _heading);
return _heading;
}
public int getDrawable() {
return _icon;
}
public int getLights(){
return _lights;
}
public int getchkDone(){
return _chkdone;
}
public String getDate(){
return _date;
}
}
我顯然缺少的東西,正如我前面提到的,沒有數據顯示。我知道有2行項目被傳遞給CustomListViewAdapter。但是我也知道CustomListViewAdapter中的View getView實際上並沒有被調用。
我希望我已經提供了足夠的信息/代碼,但是如果您覺得我需要進一步解釋某些內容,請說。
非常感謝提前!
你的getCount()做什麼? – r2DoesInc
我不認爲我有一個getCount()... – user3079872
嘗試將它添加到您的自定義適配器,然後:「@Override public int getCount(){ return _rowItems.size(); } @Override public Object getItem(int i){ return _rowItems.get(i); (int i){ } return i; }'不知道是否需要其他的,但不應該傷害他們以及:) – Klotor