gpt4 book ai didi

android - ListView 不显示。使用 SherlockFragment 和 SimpleCursorAdapter

转载 作者:行者123 更新时间:2023-11-30 03:05:01 25 4
gpt4 key购买 nike

我正在尝试使用 SimpleCursorAdapter 在 Sherlock-fragment 中显示 ListView 。在模拟器上显示空布局 - 没有 ListView 。

我的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 fragment 有问题,因为我使用了另一个项目的两种布局。但是……谁知道呢。请帮忙。

最佳答案

找到了。 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);
}
}

关于android - ListView 不显示。使用 SherlockFragment 和 SimpleCursorAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21952163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com