gpt4 book ai didi

java - Android 资源光标适配器 : Only one item displayed in ListView

转载 作者:行者123 更新时间:2023-12-01 18:03:49 25 4
gpt4 key购买 nike

我正在开发一个适配器,它应该显示从 SQLite 数据库派生的项目列表。 ListView 嵌入在 Fragment 中,填充在自定义适配器中,该适配器是 ResourceCursorAdapter 的子类。迭代光标并打印其内容表明它包含许多条目。但是,仅针对第一个条目调用 bindView,它是 ListView 中显示的唯一条目。我做错了什么?

这是(精简的)代码:

XML 片段

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/network_settings"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">

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

</LinearLayout>

适配器列表项 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/addresses_list_item"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/remote_ip_address"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.45"/>

<TextView
android:id="@+id/remote_port"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.25"/>

<TextView
android:id="@+id/address_protocol"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.2"/>

<ImageButton
android:id="@+id/delete_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/delete_address"
android:src="@drawable/close"/>

</LinearLayout>

Fragment 类(经过简化,仅包含 onCreateView,因为这是实例化适配器的地方。片段本身工作正常)

public class NetworkSettingsFragment extends Fragment {

private View mView;

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

mView = inflater.inflate(R.layout.network_settings, container, false);

final SQLiteDatabase db = mActivity.getDatabase();
final ListView addressesList = mView.findViewById(R.id.addresses_list);

final String[] addrFields = new String[]{
SettingsContract.AddressSettingsEntry._ID,
SettingsContract.AddressSettingsEntry.IP_ADDRESS,
SettingsContract.AddressSettingsEntry.PORT,
SettingsContract.AddressSettingsEntry.PROTOCOL
};
final String sortOrder = SettingsContract.AddressSettingsEntry.IP_ADDRESS + " DESC";

final Cursor addressesCursor = db.query(
SettingsContract.AddressSettingsEntry.TABLE_NAME,
addrFields,
null,
null,
null,
null,
sortOrder
);

final AddressesListAdapter addressesListAdapter = new AddressesListAdapter(
getActivity(), R.layout.address_list_item, addressesCursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER
);
addressesList.setAdapter(addressesListAdapter);
}
}

适配器

public class AddressesListAdapter extends ResourceCursorAdapter {

final private static String TAG = "AddressesListAdapter";
private int mLayout;

public AddressesListAdapter(Context context, int layout, Cursor c, int flags) {
super(context, layout, c, flags);
mLayout = layout;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(mLayout, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
Log.d(TAG, "the view: " + view + "\ncursor: " + cursor.getPosition() + "\nnum items: " + cursor.getCount());

TextView ipText = view.findViewById(R.id.remote_ip_address);
TextView portText = view.findViewById(R.id.remote_port);
TextView protocolText = view.findViewById(R.id.address_protocol);

final long id = cursor.getLong(cursor.getColumnIndexOrThrow(SettingsContract.AddressSettingsEntry._ID));
final String ip = cursor.getString(cursor.getColumnIndexOrThrow(SettingsContract.AddressSettingsEntry.IP_ADDRESS));
final int port = cursor.getInt(cursor.getColumnIndexOrThrow(SettingsContract.AddressSettingsEntry.PORT));
final String protocol = cursor.getString(cursor.getColumnIndexOrThrow(SettingsContract.AddressSettingsEntry.PROTOCOL));

ipText.setText(ip);
portText.setText(String.valueOf(port));
protocolText.setText(protocol);
}
}

最佳答案

回答我自己的问题:最终证明是布局问题。我没有考虑到的是,我的 Fragment 中的 ListView 是在 ScrollView 中创建的(这不是布局 xml 的一部分,我在我的问题中发布了。在该 ScrollView 中,选项列表(指向各种设置的链接)被包含 ListViewFragment 替换。但是,原来的ScrollView并不一定能填满屏幕的整个高度,所以我只需要给ScrollView设置一个属性android:fillViewport即可> 的 XML 并将其设置为 true。这允许 ListView 拉伸(stretch)到屏幕底部和项目,其中我首先认为它们没有被考虑通过我的适配器,突然变得可见。

这是父 ScrollView 的简化源代码(在 FrameLayout 内)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/settings_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/colorDarkTransparentBackground">

<ScrollView
android:id="@+id/settings_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingTop="5dp"
android:paddingEnd="20dp"
android:paddingRight="20dp"
android:paddingBottom="20dp"
android:fillViewport="true"
android:visibility="visible" />

</FrameLayout>

关于java - Android 资源光标适配器 : Only one item displayed in ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60591028/

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