2010-12-10 10 views
1

私はListViewを使用してデータベースから結果を表示したいというアクティビティを持っています。表には、単語、説明、カテゴリの3つの列があります。アクティビティページでは、配列から読み込まれるカテゴリのリストがあります。私はそれを設定して、リスト(例えばCat1)上の項目をクリックすると、カーソルから返される結果はDB内のカテゴリCat1のすべての単語/説明になります。現在のところ、クリックするとそのカテゴリの名前が表示されたトーストが表示されます。OnItemClick後のListViewでのDBからのカーソル結果の表示

現在のところ、アクティビティは正しく実行されていません。私はウェブ上で読んだことがあり、どのように進むべきかわかりません。ここに私がこれまで持っていたコードがあります。誰かが私を助けることができれば、本当に感謝します。

public class Categories extends ListActivity { DataBaseHelper db = new DataBaseHelper(this); プライベートSQLiteDatabaseデータ。 int位置;

private ListView list; 
private String[] categories = { 
    "C1", "C2", "C3", "C4", 
    "C5", "C6", "C7", "C8", 
    "C9", "C10", "C11", "C12" 
}; 

@Override 
public void onCreate(Bundle icicle){ 
    super.onCreate(icicle); 
    setContentView(R.layout.cat_list); 

    list = (ListView)findViewById(R.id.cat_listing); 
    list.setAdapter(new ArrayAdapter<String>(this, 
     R.layout.categories, categories)); 
    list.setTextFilterEnabled(true); 

    Cursor cursor = data.rawQuery("SELECT term, desc FROM words WHERE cat = '" + categories[position] + "'", null); 
    startManagingCursor(cursor); 

    String columns[] = new String[] { "term", "desc" }; 
    int[] to = new int[] { R.id.cat_term, R.id.cat_desc }; 

    SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.cat_result, cursor, columns, to); 
    this.setListAdapter(myAdapter); 

    list.setOnItemClickListener(new OnItemClickListener(){ 

     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id){ 
      CharSequence text = categories[position]; 
      Toast toast = Toast.makeText(getApplicationContext(), 
        text, Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
    }); 
} 

}

答えて

4

私は私のアプリで同様の機能を持っています。リストのレイアウトを定義するXMLファイルを作成し、Javaコードで呼び出す必要があります。ここで

は、例えば、XMLです:

LIST:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:orientation="vertical" > 
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 
    </ListView> 
    <TextView 
     android:id="@+id/android:empty" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="@string/no_projects" 
     android:padding="10dp" 
     android:textSize="16sp" 
     android:textStyle="bold" > 
    </TextView> 
</LinearLayout> 

これは、カスタムレイアウトXMLです。

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:orientation="vertical" > 
    <TextView 
     android:id="@+id/text1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
    </TextView> 
    <TextView 
     android:id="@+id/text2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
    </TextView> 
    <TextView 
     android:id="@+id/text3" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
    </TextView> 
</LinearLayout> 

そして、これはJavaコードです:私はそれがlist_rows呼び出す

String[] fields = new String[] { db.TABLE_PRJ_NAME, db.TABLE_PRJ_TYPE, db.TABLE_PRJ_DESC }; 
    int[] views = new int[] { /*android.R.id.text1*/ R.id.text1, R.id.text2, R.id.text3 }; 

    c = db.getAllProjects(); 
    startManagingCursor(c); 

    // Set the ListView 
    SimpleCursorAdapter prjName = new SimpleCursorAdapter(
      this, 
      R.layout.project_list_rows, 
      //android.R.layout.simple_list_item_1, 
      c, fields, views); 
    setListAdapter(prjName); 

onListItemClickListener

// This section of code is for handling the Click Event of the Projects List 
@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 

    Cursor o = (Cursor) this.getListAdapter().getItem(position); 
    String projectName = o.getString(1); 

    Intent showProjectDetails = new Intent(this, ProjectDetails.class); 
    Bundle bundle = new Bundle(); 

    bundle.putString("sendProjectName", projectName); 

    showProjectDetails.putExtras(bundle); 
    startActivity(showProjectDetails); 
} 

何のコードの新しい部分がやっていることは、単に選択した項目を送信しています意図を通して別の活動に次に、新しいアクティビティで、バンドルから選択したアイテム名を使用してDBにクエリを実行し、結果を表示します。

詳しい説明が必要かどうか尋ねます。

+0

Siddharth、 私のコードを見れば、それは私がすでに持っているものです。私は、このコードをエラーなく実行するのに問題があります。抜粋ではなく、ソース全体を投稿できますか?あなたはOnItemClickを使っていますか?ありがとう... – Devin

+0

追加コードを追加しています。元の投稿を確認してください。 –

+0

本当に申し訳ありませんが、ここで問題があります。私はCursorの準備とSimpleCursorAdapterの作成に何が関係しているのか理解しています。しかし、私は新しい活動を望んでいません。誰かがリスト内のアイテムをクリックすると、DBに照会し、クリックされたカテゴリのすべての単語のリストを返します。 投稿したコードを試しましたが、そのアクティビティを開くときに強制終了する必要があります(テストするカテゴリリストにもアクセスできません)。上記のコードは、次の行でSimpleCursorAdapter宣言とsetListAdapter呼び出しを削除すると動作します。 – Devin

0

私は非常によく似たプログラムを作っています。私はアンドロイド/ javaのための私の最初のプログラムとしてそれに問題があります。私が得た助けはウェブと私の父親からJavaを教えることです。

私はデータベースをセットアップするのを手伝ってくれました。私はテーブルをセットアップして、カテゴリがその下にあるアイテムとペアになるように説明しました。したがって、そのカテゴリのプライベートキーだけでなく、そのカテゴリに表示されるタイトルも含めて、カテゴリのテーブルがあります。次に、カテゴリから、タイトルの次のレベルのテーブルが必要です。これには、タイトルと主キーのテキストが含まれます。次に、それらをペアにする別のテーブルを作成する必要があります。タイトルキー、カテゴリキー、およびプライマリキー。

そこから、最後のテーブルに基づいてそれらをペアにするようにjavaに指示する必要があります。

私はnoobですので、私はより多くの/より良い情報を与えることができなかった申し訳ありません。しかし、私はこの問題で私がここに投稿し、幸運を見つけることがわかります。

関連する問題