【问题标题】:BaseAdapter not populating listBaseAdapter 未填充列表
【发布时间】:2015-01-11 02:17:33
【问题描述】:

所以我有一个工作的 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;
}

}

Activity 类包含:

@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);

最后是 List 布局:

<?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>

【问题讨论】:

  • '如何调试它,即使它没有导致崩溃?' - 打印到 logcat

标签: java android android-listview android-arrayadapter baseadapter


【解决方案1】:

检查列表不为空且大小大于零,然后调用您的适配器。

if(setting_list != null && setting_list.size() > 0){

Log.d("列表大小::",""+setting_list.size() > 0);

SettingAdapter 适配器 = new SettingAdapter(this, setting_list); }

在您的适配器 getView() 方法中

使用代码

设置选项 = options_list.get(position);

而不是

设置选项 = 数据[位置];

【讨论】:

    【解决方案2】:

    如果您的 Adapter 从 BaseAdapter 扩展,您必须重写 getCount() 方法,否则 getCount() 将始终返回 0,您的 getView() 将永远不会被调用。

    【讨论】:

    • 我只是忽略了它,因为它似乎与这个问题无关,因为它们基本上包含 1 行代码。不用担心它们在那里,如果它们不在,它毕竟不会编译,因为 BaseAdapter 是抽象的。您还需要 getItem 和 getItemId 才能工作。
    • 我建议将您所有的适配器代码放在这里,因为很多东西似乎对我们隐藏,我们不确定这是否是问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 2014-10-14
    相关资源
    最近更新 更多