自定义适配器未显示任何项目
custom adapter isn't showing any items
这是先前问题的后续问题:
但是在 SO gurus 的建议之后,有人建议我 post 一个新问题。
问题是我有一个不显示任何数据的自定义适配器。我已经研究了其他问题,但没有提供解决方案。
在我的 Main Activity 中,我有几个按钮,其中之一:ToDo,应该创建一行来显示来自 SQLite 数据库的数据,并且根据某些因素(主要是日期),它显示一种存储为可绘制对象的交通灯。
此行中的部分项目是一个图像按钮,我希望用户能够单击该按钮并且图像应该更改。用户还应该能够单击实际行并开始新的 activity。
我遇到的问题是未显示任何数据。
所以,这是我的代码:
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 Class 看起来像:
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 实际上并没有被调用。
我希望我已经说得够多了 information/code,但如果您觉得我需要进一步解释,请告诉我。
在此先感谢大家!
我没有看到 getCount() 方法。你应该像这样覆盖它:
@Override
public int getCount() {
return _rowItems.getCount();
}
或者,调用 super(context, resourceId, rowItems);
也应该修复它。
您的 ListView 认为没有要显示的项目。如果您使用自己的数组,则必须覆盖 getCount() 方法以指示要显示的项目数。
这是先前问题的后续问题:
在我的 Main Activity 中,我有几个按钮,其中之一:ToDo,应该创建一行来显示来自 SQLite 数据库的数据,并且根据某些因素(主要是日期),它显示一种存储为可绘制对象的交通灯。
此行中的部分项目是一个图像按钮,我希望用户能够单击该按钮并且图像应该更改。用户还应该能够单击实际行并开始新的 activity。
我遇到的问题是未显示任何数据。
所以,这是我的代码:
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 Class 看起来像:
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 实际上并没有被调用。
我希望我已经说得够多了 information/code,但如果您觉得我需要进一步解释,请告诉我。
在此先感谢大家!
我没有看到 getCount() 方法。你应该像这样覆盖它:
@Override
public int getCount() {
return _rowItems.getCount();
}
或者,调用 super(context, resourceId, rowItems);
也应该修复它。
您的 ListView 认为没有要显示的项目。如果您使用自己的数组,则必须覆盖 getCount() 方法以指示要显示的项目数。