2012-12-05 48 views
0

如何在SimpleCursor中的自定義列表項佈局中添加圖像(像複選框一樣工作)在ListFragment中加載的適配器?如何爲simplecursor適配器自定義列表項佈局添加圖像

@Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     setEmptyText("Loading..."); 

     myadapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.my_list_item, null, new String[] {TAG_NAME,TAG_TEXT1,TAG_TEXT2}, new int[] {R.id.name,R.id.text1,R.id.text2},0); 

     setListAdapter(myadapter); 
     myLoader = getLoaderManager().initLoader(0, null, this); 
    } 

my_list_item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
    <!--<ImageView android:src="@drawable/action_settings" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" />--> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="2dip" 
     android:paddingLeft="2dip" 
     android:textColor="@android:color/black" 
     android:textStyle="bold" > 
    </TextView> 

    <TextView 
     android:id="@+id/text1_static" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/name" 
     android:text="@string/text1_static" 
     android:textColor="@android:color/black" /> 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/name" 
     android:layout_marginLeft="5dp" 
     android:layout_toRightOf="@id/text1_static" 
     android:textColor="@android:color/black" /> 

    <TextView 
     android:id="@+id/text2_static" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/symbol_static" 
     android:text="@string/text2_static" 
     android:textColor="@android:color/black" /> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/text1" 
     android:layout_marginLeft="5dp" 
     android:layout_toRightOf="@id/text2_static" 
     android:textColor="@android:color/black" > 
    </TextView> 

</RelativeLayout> 

如何添加圖像(它的工作原理就像列表項的值的ID複選框)用於動態每個列表項?

回答

0
  1. ,如果你正在以下API級別11以上然後設置你的API approperitly FragmentActivty代替ListFragment
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.LoaderManager; 
import android.support.v4.content.Loader; 
import android.widget.Toast; 

public class MyActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Object> { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    getSupportLoaderManager().initLoader(0, null, this); 
} 

@Override 
public Loader<Object> onCreateLoader(int i, Bundle bundle){ 
    return null; // TODO 
} 

@Override 
public void onLoadFinished(Loader loader, Object o) { 
    Toast.makeText(this, "onLoadFinished", Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onLoaderReset(Loader loader) { 
    Toast.makeText(this, "onLoaderReset", Toast.LENGTH_SHORT).show(); 
} 
} 
+0

我的問題是如何在自定義列表項簡單的添加圖片光標適配器 – Ramprasad

+1

http://stackoverflow.com/a/7678706/1339473 simlpe ...你打電話給你的適配器,並設置圖像顯示此。 @Ram – QuokMoon

+0

謝謝altaf,它的效果很好。我可以添加這個添加圖像的點擊事件嗎? – Ramprasad

相關問題