如何为简单的光标适配器自定义列表项布局添加图像

时间:2012-12-05 04:11:32

标签: android android-listfragment

如何在ListFragment中加载的simpleCursor Adapter中的自定义列表项布局中添加图像(像复选框一样工作?)

@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>

如何动态地为每个列表项添加图像(它的工作方式类似于listitem值为id的复选框)?

1 个答案:

答案 0 :(得分:0)

  
      
  1. 如果您的API等级高于11级,请将API设置为适当使用FragmentActivty而不是ListFragment
  2.   
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();
}
}