【问题标题】:Notepad Tutorial: Add baseadapter记事本教程:添加 baseadapter
【发布时间】:2011-12-04 12:24:26
【问题描述】:

我已经完成了安卓网站的记事本教程。我添加了一个自己的模块类。现在我还想添加一个自己的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);

}

非常感谢!

菲利克斯

【问题讨论】:

  • 不确定我是否理解这个问题:你只是问你是否需要在 fillData 中使用光标?还是有什么具体的东西不起作用或者你不知道该怎么做?
  • 我稍微编辑了代码。我的问题是,未加载数据库中的值。仅显示关键字:title、device_type、home_code。有什么想法吗?
  • 好的,现在更清楚了:请在人们回答之前编辑问题,例如,您甚至没有提到您将值存储在数据库中。
  • 哦,是的,对不起。我的数据库中有很多值。在我加载 SimpleCursor Adapter 中的列之前。但现在,它不知道我如何在基本适配器中加载数据库的列。

标签: android arraylist android-listview baseadapter


【解决方案1】:

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

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

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

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

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

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

【讨论】:

  • 谢谢。但我不想使用 SimpleCursorAdapter。我想使用 BaseAdapter,因为稍后我还想在我的列表视图中显示按钮和图标。不应该可以将 BaseAdapter 与数据库一起使用吗?
  • 不,正确的方法是将 CursorAdapter 与数据库一起使用,如果您想自定义列表的外观,那么您可以编写自己的 ViewBinder 并为您的单元格编写自定义布局。
  • 嗯。我已经完成了这个教程。 vogella.de/articles/AndroidListView/… 在 6.2 中,这个家伙正在使用 ArrayAdapter 而不是 CursorAdapter。因为如果是这样,我认为这是在我的情况下使用 BaseAdapter 的好方法。
  • 我查看了教程:它可以工作(这是一个很好的教程),因为它使用字符串只是为了展示事情是如何工作的,它直到第 9 步才从数据库中获取数据,事实上它说:“如果您使用内容提供者或直接使用数据库,您可以使用 SimpleCursorAdapter 为您的 ListView 定义数据。有关 Android 和 SQLight 的更多信息,请参阅 SQLite 和列表教程”。
猜你喜欢
  • 2023-03-09
  • 1970-01-01
  • 2012-02-11
  • 1970-01-01
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多