2011-06-20 6 views
0

私はこのコードを持っています。リストビューのアイテムのテキストを取得できるようにするには本当にうれしいです。私は別のタイプのリストビューでトーストすることができる例を行ったが、これは私が作ったものではない。どんなヘルパー?事前に THX :)私のリストアイテムからテキストを引き出すことができない

private void listChoices(){ 
    mySQLiteAdapterChoices = new SQLiteAdapterChoices(this); 
    mySQLiteAdapterChoices.openToRead(); 
    Cursor cursor = mySQLiteAdapterChoices.queueAll(); 
    startManagingCursor(cursor); 
    String choic=SQLiteAdapterChoices.KEY_CHOICE_SUB; 

    String[] from = new String[]{choic}; 
    int[] to = new int[]{R.id.textUserChoice}; 

    SimpleCursorAdapter cursorAdapter = 
     new SimpleCursorAdapter(this, R.layout.userrow, cursor, from, to); 
    setListAdapter(cursorAdapter); 
    mySQLiteAdapterChoices.close(); 



    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 



      switch(position) { 
      case 0: 
       final String text = ("txtUserChoice"); 
       Toast.makeText(getApplicationContext(), 
         text, 
         Toast.LENGTH_SHORT).show(); 
       startActivity(new Intent(getApplicationContext(), 
         First.class)); 
       break; 

...

XML createcontrol.xml

<?xml version="1.0" encoding="utf-8"?> 
<AbsoluteLayout 
android:id="@+id/widget0" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
> 
<ListView 
android:id="@android:id/list" 

android:layout_width="305px" 
android:layout_height="270px" 
android:layout_x="8px" 
android:layout_y="10px" 
> 
</ListView> 
<EditText 
android:id="@+id/txtEdt" 
android:layout_width="185px" 
android:layout_height="wrap_content" 
android:text="" 
android:textSize="18sp" 
android:autoText="true" 
android:capitalize="words" 
android:layout_x="8px" 
android:layout_y="293px" 
> 
</EditText> 
<Button 
android:id="@+id/btnNewItem" 
android:layout_width="107px" 
android:layout_height="wrap_content" 
android:text="Submit" 
android:layout_x="199px" 
android:layout_y="295px" 
> 
</Button> 
</AbsoluteLayout> 

XMLから以下のコードを修正userrow.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip"> 
    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="6dip" 
     android:src="@drawable/icon" /> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:layout_height="fill_parent"> 
     <TextView 
      android:id="@+id/textUserChoice" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:gravity="center_vertical" 
     /> 

    </LinearLayout> 
</LinearLayout> 

から..私はいくつかの変数をいくつかのスコープに移動し、コードをこれに変更しました

  case 0: 
       int index = cursor.getColumnIndex(choic); 
       final String text = cursor.getString(index); 
       Toast.makeText(getApplicationContext(), 
         text, 
         Toast.LENGTH_SHORT).show(); 
       startActivity(new Intent(getApplicationContext(), 
         First.class)); 
       break; 
      case 1:.... 

答えて

0

私はカーソルからデータを取得するのを忘れていると思います。あなたは final String text = ("txtUserChoice"); を書いて、あなたは int index = cursor.getColumnIndex(choic); final String text = cursor.getString(index); のように書いて、インスタンスメンバとしてカーソルとchoic変数を作成する必要があります。

+0

イェーイ!それはそれを固定!どうもありがとうございます!上の固定コードを掲示する! – tricknology

0

これはListViewから選択した項目から文字列のコンテンツを抽出するのに役立ちます:

listview.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       // TODO Auto-generated method stub 

       String selection = listview.getItemAtPosition(arg2).toString(); 
       Toast.makeText(getApplicationContext(), "you have selected "+selection, 5000).show(); 

      } 
     }); 
関連する問題