2011-12-04 5 views
0

私はアンドロイドのウェブサイトからノートパッドのチュートリアルを行っています。私は独自のModule-Classを追加しました。今私はまた、独自のベースアダプターを追加したい。しかし、実装には問題があります。メモ帳のチュートリアル:ベースアダプターを追加

私の問題は、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); 

} 

どうもありがとう!

フェリックス

+0

私はこの問題を理解していません。fillDataにカーソルが必要かどうかを確認するだけですか?あるいは、何か具体的なものがありませんか、またはあなたがする方法を知らないですか? – gianpi

+0

コードを少し編集しました。私の問題は、dbからの値がロードされていないということです。キーワード:title、device_type、home_codeのみが表示されます。何か案が? – FelixA

+0

これで明らかになりました。人々が答える前に質問を編集してください。たとえば、データベースに値が格納されていると言わないなどです。 – gianpi

答えて

1

問題は、あなたのデータがデータベースにあるので、あなたは、あなたがコメントアウトコードでやっていたようSimpleCursorAdapterを使用しておく必要があります。

あなたの新しいコードは、ArrayListの中でデータベース列(およびない実際のデータ)の名前で埋めモジュールを置く:

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

それはそれらの値が表示されるので、その後にカスタムアダプタが、正常に動作します。

おそらくあなたは、同じ文字列をSimpleCursorAdapterに渡していたが、そのアダプタは列名を使用してデータベースからデータをフェッチしていたということになるだろう。代わりに、あなたのカスタムアダプターは単にリストにあるものを表示します。

データベースから値を表示する場合は、SimpleCursorAdapterを使用する必要があります(または、さらに処理が必要な場合は拡張する必要があります)。

+0

ありがとうございます。しかし、私はSimpleCursorAdapterを使いたくありません。後でListViewにボタンとアイコンを表示したいので、BaseAdapterを使いたいです。データベースでBaseAdapterを使用することはできませんか? – FelixA

+1

いいえ、適切なアプローチは、データベースでCursorAdapterを使用することです。リストの外観をカスタマイズする場合は、独自の[ViewBinder]を作成します(http://developer.android.com/reference/android/widget/ SimpleCursorAdapter.ViewBinder.html)、セルのカスタムレイアウトを作成します。 – gianpi

+0

Hmm。私はこのチュートリアルを終えました。 http://www.vogella.de/articles/AndroidListView/article.html#listadvanced_interactive 6.2では、男はArrayAdapter で作業しており、CursorAdapterでは作業していません。これは、私のケースでBaseAdapterを使うのが良い方法だと思ったからです。 – FelixA

関連する問題