ListView不显示。使用SherlockFragment和SimpleCursorAdapter

时间:2014-02-22 09:07:30

标签: android listview simplecursoradapter

我正在尝试使用SimpleCursorAdapter在Sherlock-fragment中显示List-view。在模拟器上显示空布局 - 没有列表视图。

我的 SherlockFragment:

public class Fragment_A_List extends SherlockFragment implements
    OnItemClickListener {
final String LOG_TAG = "myLogs";
public static String data[];
int v_bread, v_nuts, v_vegetables, v_eggs, v_apple, v_sweet, v_beverage,
        v_milk, v_meat, v_fish, v_cereal, v_fats;
public static int[] v_images = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

ListView list;
DB_MainList db;
SimpleCursorAdapter scAdapter;
Cursor cursor;

public interface onItemEventListener {
    public void someEvent(int position);
}

onItemEventListener someItemtListener;

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
    try {
        someItemtListener = (onItemEventListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement onSomeEventListener");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_list, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    data = getResources().getStringArray(R.array.name);
    fillImagesArray();

    db = new DB_MainList(getActivity());
    db.open();

    cursor = db.getAllData();
    getActivity().startManagingCursor(cursor);

    String[] from = new String[] { DB_MainList.COLUMN_IMG,
            DB_MainList.COLUMN_TXT };
    int[] to = new int[] { R.id.ivIconListA, R.id.tvTextListA };

    scAdapter = new SimpleCursorAdapter(getActivity(), R.layout.item_main,
            cursor, from, to);
    list = (ListView) getActivity().findViewById(R.id.lvListA);
    list.setOnItemClickListener(this);
    list.setAdapter(scAdapter);
}

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
    // TODO Auto-generated method stub
    Log.d(LOG_TAG, "position " + position);
    someItemtListener.someEvent(position);
}

我在SQLiteOpenHelper中的 OnCreate:

@Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(DB_CREATE);
        ContentValues cv = new ContentValues();
        for (int i = 0; i < 12; i++) {
            cv.put(COLUMN_TXT, Fragment_A_List.data[i]);
            cv.put(COLUMN_IMG, Fragment_A_List.v_images[i]);
            db.insert(DB_NAME, null, cv);
        }
    }

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#319F61"
android:orientation="vertical" >

<ListView
    android:id="@+id/lvListA"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

我的 ListView自定义项:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/ivIconListA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" >
</ImageView>

<TextView
    android:id="@+id/tvTextListA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="10dp"
    android:text="text"
    android:textSize="18sp" >
</TextView>

</LinearLayout>

我想我的Sherlock片段有问题,因为我使用了另一个项目的两个布局。但是......谁知道呢。请帮忙。

1 个答案:

答案 0 :(得分:1)

找到它。 DB_TABLE而不是DB_NAME。

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(DB_CREATE);
    ContentValues cv = new ContentValues();
    for (int i = 0; i < 12; i++) {
        cv.put(COLUMN_TXT, Fragment_A_List.data[i]);
        cv.put(COLUMN_IMG, Fragment_A_List.v_images[i]);
        db.insert(DB_TABLE, null, cv);
    }
}