如何自定义使用 sqliteopenhelper 的列表视图?
How to customize listview that uses sqliteopenhelper?
我正在尝试自定义 listview
(特别是更改字体大小和颜色)以列出我使用 SQLiteOpenHelper
保存的一些信息。为此,我尝试在 ListViewAdapter.java
中连接 list_view_items.xml
中的自定义设置。但是我的listview没有自定义,显示基本字体和字号如下:
如何解决此问题以显示自定义列表?
这里是ListViewAdapter.java
:
package com.example.dailybible3;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.example.dailybible3.ListViewDataModel;
import com.example.dailybible3.R;
import java.util.ArrayList;
public class ListViewAdapter extends ArrayAdapter<ListViewDataModel> implements View.OnClickListener {
private ArrayList<ListViewDataModel> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView list_view_item;
}
public ListViewAdapter(ArrayList<ListViewDataModel> data, Context context) {
super(context, R.layout.list_view_items, data);
this.dataSet = data;
this.mContext=context;
}
@Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(position);
ListViewDataModel dataModel = (ListViewDataModel) object;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
ListViewDataModel dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_view_items, parent, false);
viewHolder.list_view_item = (TextView) convertView.findViewById(R.id.text);
result = convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result = convertView;
}
viewHolder.list_view_item.setText(dataModel.getNote());
// Return the completed view to render on screen
return convertView;
}
}
这是我的 listview_items.xml
:
<?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:theme="@style/FontStyle"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:text="Example"
android:textColor="@color/colorPrimary"
android:textSize="15sp"
android:textStyle="bold"
android:lineSpacingMultiplier="1.3"/>
</LinearLayout>
我发现我使用 android.R.layout.simple_list_item_1
而不是我想要使用的布局是一个简单的错误。为了以防万一有人发现类似我的问题,我会在下面留下我的代码。
我之前的代码:
ListAdapter adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, listData);
mListView.setAdapter(adapter);
我编辑的代码:
ListAdapter adapter = new ArrayAdapter<String>(getContext(),R.layout.list_view_items, listData);
mListView.setAdapter(adapter);
我正在尝试自定义 listview
(特别是更改字体大小和颜色)以列出我使用 SQLiteOpenHelper
保存的一些信息。为此,我尝试在 ListViewAdapter.java
中连接 list_view_items.xml
中的自定义设置。但是我的listview没有自定义,显示基本字体和字号如下:
如何解决此问题以显示自定义列表?
这里是ListViewAdapter.java
:
package com.example.dailybible3;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.example.dailybible3.ListViewDataModel;
import com.example.dailybible3.R;
import java.util.ArrayList;
public class ListViewAdapter extends ArrayAdapter<ListViewDataModel> implements View.OnClickListener {
private ArrayList<ListViewDataModel> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView list_view_item;
}
public ListViewAdapter(ArrayList<ListViewDataModel> data, Context context) {
super(context, R.layout.list_view_items, data);
this.dataSet = data;
this.mContext=context;
}
@Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(position);
ListViewDataModel dataModel = (ListViewDataModel) object;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
ListViewDataModel dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_view_items, parent, false);
viewHolder.list_view_item = (TextView) convertView.findViewById(R.id.text);
result = convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result = convertView;
}
viewHolder.list_view_item.setText(dataModel.getNote());
// Return the completed view to render on screen
return convertView;
}
}
这是我的 listview_items.xml
:
<?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:theme="@style/FontStyle"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:text="Example"
android:textColor="@color/colorPrimary"
android:textSize="15sp"
android:textStyle="bold"
android:lineSpacingMultiplier="1.3"/>
</LinearLayout>
我发现我使用 android.R.layout.simple_list_item_1
而不是我想要使用的布局是一个简单的错误。为了以防万一有人发现类似我的问题,我会在下面留下我的代码。
我之前的代码:
ListAdapter adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, listData);
mListView.setAdapter(adapter);
我编辑的代码:
ListAdapter adapter = new ArrayAdapter<String>(getContext(),R.layout.list_view_items, listData);
mListView.setAdapter(adapter);