0

我已经从 android webside 完成了记事本教程。我添加了一个自己的模块类。现在我还想添加一个自己的baseadapter。但我在实施上有问题。

我的问题是 fillData() 方法。它在第三个代码部分。我也不确定,是否需要光标?

我希望任何人都可以帮助我,纠正 fillData() 方法。

我的模块类

public class Module {
private String title;
private String device_type;
private String home_code;
private String device_code;

public Module(String n, String m, String hc, String mc) {
    title = n;
    device_type = m;
    home_code = hc;
    device_code = mc;
}

public String getTitle() { return title; }
public String getDeviceType() { return device_type; }
public String getHomeCode() { return home_code; }
public String getDeviceCode() { return device_code; }

}

我的模块适配器:

public class ModuleAdapter extends BaseAdapter implements OnClickListener {
private Context context;

private List<Module> listModule;

public ModuleAdapter(Context context, List<Module> listModule) {
    this.context = context;
    this.listModule = listModule;
}

public int getCount() {
    return listModule.size();
}

public Object getItem(int position) {
    return listModule.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup viewGroup) {
    Module entry = listModule.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.notes_row, null);
    }
    TextView tvTitle = (TextView) convertView.findViewById(R.id.text1);
    tvTitle.setText(entry.getTitle());

    TextView tvDeviceType = (TextView) convertView.findViewById(R.id.text2);
    tvDeviceType.setText(entry.getDeviceType());

    TextView tvHomeCode = (TextView) convertView.findViewById(R.id.text3);
    tvHomeCode.setText(entry.getHomeCode());

    TextView tvDeviceCode = (TextView) convertView.findViewById(R.id.text4);
    tvDeviceCode.setText(entry.getDeviceCode());

    return convertView;
}

@Override
public void onClick(View view) {
    Module entry = (Module) view.getTag();
    listModule.remove(entry);
    // listModule.remove(view.getId());
    notifyDataSetChanged();

}

private void showDialog(Module entry) {
    // Create and show your dialog
    // Depending on the Dialogs button clicks delete it or do nothing
}

}

主代码中的 fillData() 方法:

    private void fillData() {
    //Cursor notesCursor = mDbHelper.fetchAllNotes();
    //startManagingCursor(notesCursor);

    final List<Module> from = new ArrayList<Module>();
    from.add(new Module(NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_DEVICETYPE, NotesDbAdapter.KEY_HOMECODE, NotesDbAdapter.KEY_DEVICECODE));

    // Now create a simple cursor adapter and set it to display
    //SimpleCursorAdapter notes = 
    //    new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
    //notes.setViewBinder(new ModuleViewBinder());

    ModuleAdapter adapter = new ModuleAdapter(this, from);
    setListAdapter(adapter);

}

非常感谢!

菲利克斯

4

1 回答 1

1

问题是您的数据在数据库中,因此您应该像在注释掉的代码中那样继续使用 SimpleCursorAdapter。

您的新代码在 ArrayList 中放置了一个填充有数据库列名称(而不是实际数据)的模块:

from.add(new Module(NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_DEVICETYPE, NotesDbAdapter.KEY_HOMECODE, NotesDbAdapter.KEY_DEVICECODE));

然后您的自定义适配器可以正常工作,因为它会显示这些值。

可能让您感到困惑的是,您将相同的字符串传递给 SimpleCursorAdapter,但该适配器使用列名从数据库中获取数据。您的自定义适配器只是显示列表中的内容。

如果你想显示数据库中的值,你应该坚持使用 SimpleCursorAdapter(或者如果你需要做更多的事情,可以扩展它)。

于 2011-12-04T13:00:25.653 回答