0

私はアンドロイドのウェブサイドからメモ帳のチュートリアルを行いました。独自の Module-Class を追加しました。ここで、独自の baseadapter も追加したいと考えています。しかし、私は実装に問題があります。

私の問題は fillData() メソッドです。コードの 3 番目の部分にあります。また、カーソルが必要かどうかもわかりません。

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 に答える