listview 项目未在自定义适配器的 getview 方法中显示分配的值
Posted
技术标签:
【中文标题】listview 项目未在自定义适配器的 getview 方法中显示分配的值【英文标题】:listview item is not displaying the assigned value in getview method of custom adapter 【发布时间】:2021-04-19 20:27:55 【问题描述】:我遇到了一个问题,但仍然不知道如何摆脱。我为它制作了一个带有自定义适配器的列表视图。我还实现了一个搜索视图,它会在我每次输入字符时更新列表视图。但问题是列表视图没有显示正确的结果。我在自定义适配器的getView
方法中放置了一些检查点,以检查将显示的值是否正确以及它们是否正确。
我有两个项目,我们称它们为 HD03 和 HD02,因为您可以在每个项目的左上角看到它们的名称。
当我在搜索栏中输入02
时,结果必须是项目HD02
,但我得到的是项目HD03
。虽然,我使用调试器在getView
方法中检查的值是HD02
我想我在某个地方犯了一个愚蠢的错误,但我找不到它。感谢您的时间 ! 这是我的片段和适配器代码。
片段
public class DashboardFragment extends Fragment
private DashboardViewModel dashboardViewModel;
private ListView listViewContract;
private SearchView contractSearchView;
private ContractAdapter productListAdapter;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
dashboardViewModel =
new ViewModelProvider(this).get(DashboardViewModel.class);
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
initView(root);
return root;
private void initView(View view)
listViewContract = view.findViewById(R.id.contract_list);
listViewContract.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
listViewProduct_onItemClick(adapterView, view, i, l);
);
loadData();
contractSearchView = view.findViewById(R.id.search_bar);
contractSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
@Override
public boolean onQueryTextSubmit(String query)
DashboardFragment.this.productListAdapter.getFilter().filter(query);
return false;
@Override
public boolean onQueryTextChange(String newText)
DashboardFragment.this.productListAdapter.getFilter().filter(newText);
return false;
);
private void loadData()
List<Contract> products = new ArrayList<Contract>();
products.add(new Contract("p03", "Name 1", "4", "Mới", "HD03" ));
products.add(new Contract("p02", "Name 2", "5", "Mới", "HD02"));
productListAdapter = new ContractAdapter(getContext(), products);
listViewContract.setAdapter(productListAdapter);
private void listViewProduct_onItemClick(AdapterView<?> adapterView, View view, int i, long l)
// Product product = (Product) adapterView.getItemAtPosition(i);
// Toast.makeText(getApplicationContext(), product.getName(), Toast.LENGTH_LONG).show();
适配器
public class ContractAdapter extends BaseAdapter implements Filterable
private Context context;
private List<Contract> products;
private List<Contract> filteredData;
private ItemFilter itemFilter = new ItemFilter();
public ContractAdapter(Context context, List<Contract> products)
this.context = context;
this.products = products;
this.filteredData = products;
@NonNull
@Override
public View getView(int position, View view, ViewGroup parent)
ViewHolder viewHolder;
if (view == null)
viewHolder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.phan_anh_cardview, parent, false);
viewHolder.textViewContractName = view.findViewById(R.id.ten_hd);
viewHolder.textViewCustomerName = view.findViewById(R.id.ten_dt);
viewHolder.textViewEmployeeName = view.findViewById(R.id.ten_nv);
viewHolder.textViewId = view.findViewById(R.id.ma_hd);
viewHolder.buttonStatus = view.findViewById(R.id.report_status);
view.setTag(viewHolder);
else
viewHolder = (ViewHolder) view.getTag();
Contract product = filteredData.get(position);
viewHolder.textViewContractName.setText(product.getName());
viewHolder.textViewEmployeeName.setText(product.getNhanvien());
viewHolder.textViewCustomerName.setText(product.getCustomer());
viewHolder.textViewId.setText(product.get_id());
viewHolder.buttonStatus.setText(product.getStatus());
Log.i("view", viewHolder.textViewId.getText().toString());
return view;
public int getCount()
return filteredData.size();
public Contract getItem(int position)
return filteredData.get(position);
public long getItemId(int position)
return position;
@NonNull
@Override
public Filter getFilter()
return itemFilter;
// return super.getFilter();
private static class ViewHolder
public static TextView textViewContractName;
public static TextView textViewCustomerName;
public static TextView textViewEmployeeName;
public static TextView textViewId;
public static Button buttonStatus;
private class ItemFilter extends Filter
@Override
protected FilterResults performFiltering(CharSequence constraint)
String query = constraint.toString();
FilterResults results = new FilterResults();
List<Contract> originalData = products;
int count = originalData.size();
List<Contract> nList = new ArrayList<>();
for (Contract contract : originalData)
if(contract.get_id().contains(query) || contract.getName().contains(query) || contract.getCustomer().contains(query))
nList.add(contract);
results.values = nList;
results.count = nList.size();
return results;
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
filteredData = (List<Contract>) results.values;
notifyDataSetChanged();
fragment_dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".ui.dashboard.DashboardFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/tag_spinner_constraint"
android:layout_
android:layout_
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<SearchView
android:id="@+id/search_bar"
android:layout_
android:layout_
android:iconifiedByDefault="false"
app:layout_constraintEnd_toEndOf="@id/tag_spinner_constraint"
app:layout_constraintStart_toStartOf="@id/tag_spinner_constraint"
app:layout_constraintTop_toTopOf="@id/tag_spinner_constraint" />
<HorizontalScrollView
android:id="@+id/filter_bar"
android:layout_
android:layout_
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/search_bar">
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/empty_rooms_btn"
android:layout_
android:layout_
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:background="@drawable/rounded_button"
android:text="Phòng trống"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/floor_spinner" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/not_full_rooms_btn"
android:layout_
android:layout_
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="4dp"
android:background="@drawable/rounded_button"
android:text="Phòng ghép"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/empty_rooms_btn"
app:layout_constraintTop_toBottomOf="@+id/floor_spinner" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/full_rooms_btn"
android:layout_
android:layout_
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:background="@drawable/rounded_button"
android:text="Phòng đã đầy"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/not_full_rooms_btn"
app:layout_constraintTop_toBottomOf="@+id/floor_spinner" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/all_rooms_btn"
android:layout_
android:layout_
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:background="@drawable/rounded_button"
android:text="Tất cả"
android:textSize="12sp" />
</LinearLayout>
</HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
<ListView
android:id="@+id/contract_list"
android:layout_
android:layout_
android:layout_marginTop="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tag_spinner_constraint">
</ListView>
</androidx.constraintlayout.widget.ConstraintLayout>
phan_anh_cardview.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="8dp">
<androidx.cardview.widget.CardView
android:layout_
android:layout_
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_
android:layout_
android:layout_marginTop="5dp"
android:padding="6dp">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/report_status"
android:layout_
android:layout_
android:background="@drawable/rounded_button"
android:text="Mới"
android:textSize="10sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/ma_hd"
android:layout_
android:layout_
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/ten_hd"
android:layout_
android:layout_
android:textSize="30sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ma_hd" />
<TextView
android:id="@+id/ten_dt"
android:layout_
android:layout_
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ten_hd" />
<TextView
android:id="@+id/ten_nv"
android:layout_
android:layout_
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ten_dt" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
rounded_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/cardview_dark_background" />
<corners android:bottomRightRadius="30dp"
android:bottomLeftRadius="30dp"
android:topRightRadius="30dp"
android:topLeftRadius="30dp"/>
</shape>
【问题讨论】:
你也可以发布xml,然后我将在我的环境中调试你的代码。 我用所有必要的 xml 代码更新了我的问题。提前谢谢你! 我贴出答案,请查收。 删除了android-studio
标签。此标签用于解决与 Android Studio 产品相关的问题。这是一个通用的 Android 问题。
【参考方案1】:
我在您的代码中发现错误。 您不应该在 ViewHolder 中使用静态成员。 所以请替换 ViewHolder 代码如下。
ContractAdapter.java
..............
private static class ViewHolder
public TextView textViewContractName;
public TextView textViewCustomerName;
public TextView textViewEmployeeName;
public TextView textViewId;
public Button buttonStatus;
...............
如果是这样,它将起作用。
【讨论】:
哦!非常感谢,我从一个旧项目中重用了这段代码,但我没有注意到这一点。自从我上次从事 android 项目以来已经有一段时间了。谢谢!以上是关于listview 项目未在自定义适配器的 getview 方法中显示分配的值的主要内容,如果未能解决你的问题,请参考以下文章
在自定义 ListView 适配器中执行 findViewById() 时发生 NullPointerException
如何在自定义 ListView 适配器中处理 RadioGroup 的 onCheckedChangeListener
在自定义适配器类中发送服务器请求后,如何更改 Listview 按钮名称?
Android - 通过 getView 函数在自定义 listView 适配器内设置 ImageView 的源无法正常工作