所以我有一个工作的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>
答案 0 :(得分:0)
检查列表是否为空,大小大于零,然后调用适配器。
if(setting_list!= null&amp;&amp; setting_list.size()&gt; 0){
Log.d(“List size ::”,“”+ setting_list.size()&gt; 0);
SettingAdapter adapter = new SettingAdapter(this,setting_list); }
在你的适配器getView()方法
使用代码
设置选项= options_list.get(position);
而不是
设置选项=数据[位置];
答案 1 :(得分:0)
如果你的适配器从BaseAdapter扩展,你必须覆盖getCount()方法,否则它将getCount()将始终返回0并且你的getView()永远不会被调用。