Question

I’m trying show List-view in Sherlock-fragment using SimpleCursorAdapter. On emulator shows up empty layout - without List-view.

My 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);
}

My OnCreate in SQLiteOpenHelper:

@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);
        }
    }

My Layout:

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

My custom item for 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>

I suppose something wrong is in my Sherlock-fragment, cause I use both layouts from another project. But..who knows. Please, help.

Was it helpful?

Solution

Found it. DB_TABLE instead of 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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top