因此,我有一个工作的ArrayAdapter,在我的列表中填充了一个特定的项目布局,但是我希望合并多个项目类型,所以我切换到了一个BaseAdpater。我的问题是,现在ListView不再填充列表了。知道发生了什么,或者如何调试它,即使它没有造成崩溃?
公共类SettingAdapter扩展BaseAdapter {
Context context;
private static final int LIST_ITEM_TYPE_1 = 0;
private static final int LIST_ITEM_TYPE_2 = 1;
private static final int LIST_ITEM_TYPE_3 = 2;
private static final int OPTION_ID_1 = 0;
private static final int OPTION_ID_2 = 1;
private static final int OPTION_ID_3 = 2;
private static final int OPTION_ID_4 = 3;
private static final int LIST_ITEM_TYPE_COUNT = 3;
private LayoutInflater inflater;
private List<Setting> options_list;
public SettingAdapter(Context context, List<Setting> options) {
inflater = LayoutInflater.from(context);
options_list = options;
}
@Override
public int getCount() {
return options_list.size();
}
@Override
public Setting getItem(int position) {
return options_list.get(position);
}
@Override
public long getItemId(int position) {
return options_list.get(position).getID();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
SettingHolder holder;
Setting data = getItem(position);
if(row == null) {
inflater = ((Activity)context).getLayoutInflater();
holder = new SettingHolder();
if(data.getType() == OPTION_ID_1){
Log.d("option1", Integer.toString(OPTION_ID_1));
row = inflater.inflate(R.layout.item_option_1, parent, false);
holder.s_heading = (TextView)row.findViewById(R.id.option1_heading);
holder.s_sub_heading = (TextView)row.findViewById(R.id.option1_sub_heading);
} else if(data.getType() == OPTION_ID_3){
Log.d("option3", Integer.toString(OPTION_ID_3));
row = inflater.inflate(R.layout.item_option_2, parent, false);
holder.s_heading = (TextView)row.findViewById(R.id.option3_heading);
holder.s_checked = (CheckBox)row.findViewById(R.id.option3_checkbox);
} else {
Log.d("option2", Integer.toString(OPTION_ID_2));
row = inflater.inflate(R.layout.item_option_3, parent, false);
holder.s_heading = (TextView)row.findViewById(R.id.option2_heading);
}
row.setTag(holder);
} else {
holder = (SettingHolder)row.getTag();
}
if(options_list.size() != 0 && options_list != null){
Log.d("data", "exists");
if(data == null){
holder.s_heading.setText("heading");
holder.s_sub_heading.setText("sub heading");
holder.s_checked.setChecked(false);
} else {
holder.s_heading.setText(data.getName());
if(data.getType() == OPTION_ID_1) {
holder.s_sub_heading.setText(data.getSubHeading());
}
if(data.getChecked() != null && data.getType() == OPTION_ID_3){
holder.s_checked.setChecked(data.getChecked());
}
}
} else {
Log.d("data", "none");
}
return row;
}
class SettingHolder {
TextView s_heading;
TextView s_sub_heading;
CheckBox s_checked;
}}
和活动类包含:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
// Grab references to UI elements.
ListView option_data = (ListView) findViewById(R.id.settings_list);
setting_db = new SettingDatabaseHandler(this);
List<Setting> setting_list = setting_db.getAllSettings();
if(setting_list.size() == 0){
setting_db.addSetting(new Setting(OPTION_ID_1, "Profile Settings"));
setting_db.addSetting(new Setting(OPTION_ID_2, "Font Size", "20sp"));
setting_db.addSetting(new Setting(OPTION_ID_3, "Proxy", "Configure HTTP proxy for network requests"));
setting_db.addSetting(new Setting(OPTION_ID_4, "Offline Mode", false));
setting_db.addSetting(new Setting(OPTION_ID_5, "Location", true));
setting_db.addSetting(new Setting(OPTION_ID_6, "About", "Help, Terms of Service and Privacy Policy"));
setting_list = setting_db.getAllSettings();
}
if(setting_list != null && setting_list.size() > 0) {
Log.d("Setting list size::", "" + Integer.toString(setting_list.size()));
}
SettingAdapter adapter = new SettingAdapter(this, setting_list);
// Assign adapter to ListView
option_data.setAdapter(adapter);最后的列表布局:
<?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="match_parent"
android:layout_height="match_parent"
tools:context="com.mapbox.aerove.settings.SettingActivity">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/settings_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:layout_alignParentTop="true">
</ListView>
</RelativeLayout>发布于 2015-01-11 03:09:54
检查列表是否为空,大小大于零,然后调用适配器。
if(setting_list != null && setting_list.size() > 0){
Log.d(“列表大小:”、“+setting_list.size()>0”);
SettingAdapter适配器=新SettingAdapter(this,setting_list);}
在适配器getView()方法中
使用代码
设置选项=options_list.get(位置);
而不是
设置选项=数据交换;
发布于 2015-01-11 03:32:57
如果Adapter从BaseAdapter扩展,则必须重写getCount()方法,否则getCount()将始终返回0,而getView()将永远得不到调用。
https://stackoverflow.com/questions/27883328
复制相似问题