因此,我正在将在全部活动环境中工作的代码移动到使用一个或两个活动以及一堆片段的代码。我现在遇到的问题是,我正在尝试转换从Web获取大量基于JSON文本的对象的活动,将它们保存到本地SQLite DB,然后查询它们并将它们放入一个ListView。移植JSON SQLite的东西并不是很难,但是我对列表视图有问题...特别是在findViewById行,它找不到符号' list& #39;"
这是我的onCreateView代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_event, container, false);
dbHelper.open();
Cursor cursor = dbHelper.fetchAllTestObjects();
dbHelper.close();
// The desired columns to be bound
String[] columns = new String[]{
SQLiteDbAdapter.test_object_id,
};
// the XML defined views which the data will be bound to
int[] to = new int[]{
R.id.test_object_id,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(
getActivity(), R.layout.home_test_objects_info,
cursor,
columns,
to,
0);
listView = (ListView) view.findViewById(R.id.list);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
int testObjectId = cursor.getInt(cursor.getColumnIndexOrThrow(SQLiteDbAdapter.test_object_id));
DynamoDBManager.CoreInfo listCore = new DynamoDBManager.CoreInfo(testObjectId, "", "");
// Do action based on listCore
}
});
return view;
}
对于我的fragment_event_list.xml,我有:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.navigationdrawerexample.EventFragment">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
对于将填充ListView的对象的对象设置,home_events_info.xml将在其中包含其他TextView项目:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/eventsRelLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip">
<TextView
android:id="@+id/test_object_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</RelativeLayout>
我的活动代码与此之间的主要区别在于我使用了Fragment(List)模板,它将fragment_event_list.xml的id自动命名为list而不是listview1,而我在activity_home.xml中没有这样,并且没有#39 ; t使用LinearLayout但FrameLayout。
activity_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
我认为这些差异是我当前问题的原因,因为Fragment和FrameLayout的工作方式与Activity和LinearLayout不同,但我不确定如何/为何以及如何修复错误。
答案 0 :(得分:2)
我认为Listview的id属性没有在xml上正确设置
更改:android:id =“@ android:id / list”
For:android:id =“@ + id / list”