gpt4 book ai didi

java - 处理 fragment ListView 时出错,找不到符号 'list'

转载 作者:行者123 更新时间:2023-11-29 21:02:37 24 4
gpt4 key购买 nike

因此,我正在将我在全 Activity 环境中工作的代码移动到使用一个或两个 Activity 以及一堆 fragment 的代码。我遇到的问题是现在我正在尝试转换一个 Activity ,该 Activity 从网络上获取一堆基于 JSON 文本的对象,将它们保存到本地 SQLite 数据库,然后查询它们并将它们放入 ListView。移植 JSON SQLite 的东西并不太难,但我在 ListView 中遇到问题......特别是在 findViewById 行,它“找不到符号‘列表’”

这是我的 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>

我的 Activity 代码与此代码之间的主要区别是我使用了 fragment (列表)模板,它自动将 fragment_event_list.xml 的 id 命名为列表,而不是我在 activity_home.xml 中使用的 listview1 而不是使用 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,但我不确定如何/为什么以及如何修复错误。

最佳答案

我认为您的 Listview 的 id 属性在 xml 上设置不正确

更改:android:id="@android:id/list"

对于:android:id="@+id/list"

关于java - 处理 fragment ListView 时出错,找不到符号 'list',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25558311/

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