如何使用 BaseAdapter 刷新带有文本和图像的列表视图?
Posted
技术标签:
【中文标题】如何使用 BaseAdapter 刷新带有文本和图像的列表视图?【英文标题】:How do I refresh a listview with text and image using BaseAdapter? 【发布时间】:2021-09-28 14:32:34 【问题描述】:我需要一些帮助来刷新我的列表视图数据。
我按照this 教程制作了一个列表视图。
这里是:
ListAdapter.java:
package com.sanktips.customlistviewexample;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class ListAdapter extends BaseAdapter
Context context;
private final String [] values;
private final String [] numbers;
private final int [] images;
public ListAdapter(Context context, String [] values, String [] numbers, int [] images)
//super(context, R.layout.single_list_app_item, utilsArrayList);
this.context = context;
this.values = values;
this.numbers = numbers;
this.images = images;
@Override
public int getCount()
return values.length;
@Override
public Object getItem(int i)
return i;
@Override
public long getItemId(int i)
return i;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
ViewHolder viewHolder;
final View result;
if (convertView == null)
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.single_list_item, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.aNametxt);
viewHolder.txtVersion = (TextView) convertView.findViewById(R.id.aVersiontxt);
viewHolder.icon = (ImageView) convertView.findViewById(R.id.appIconIV);
result=convertView;
convertView.setTag(viewHolder);
else
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
viewHolder.txtName.setText(values[position]);
viewHolder.txtVersion.setText("Version: "+numbers[position]);
viewHolder.icon.setImageResource(images[position]);
return convertView;
private static class ViewHolder
TextView txtName;
TextView txtVersion;
ImageView icon;
MainActivity.java 文件:
package com.sanktips.customlistviewexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
ListView lView;
ListAdapter lAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] images = R.drawable.alpha, R.drawable.beta, R.drawable.cupcake, R.drawable.donut, R.drawable.eclair, R.drawable.froyo, R.drawable.gingerbread, R.drawable.honeycomb, R.drawable.icecreamsandwich, R.drawable.jellybean, R.drawable.kitkat, R.drawable.lollipop, R.drawable.marshmallow, R.drawable.nougat;
String[] version = "Android Alpha", "Android Beta", "Android Cupcake", "Android Donut", "Android Eclair", "Android Froyo", "Android Gingerbread", "Android Honeycomb", "Android Ice Cream Sandwich", "Android JellyBean", "Android Kitkat", "Android Lollipop", "Android Marshmallow", "Android Nougat";
String[] versionNumber = "1.0", "1.1", "1.5", "1.6", "2.0", "2.2", "2.3", "3.0", "4.0", "4.1", "4.4", "5.0", "6.0", "7.0";
lView = (ListView) findViewById(R.id.androidList);
lAdapter = new ListAdapter(MainActivity.this, version, versionNumber, images);
lView.setAdapter(lAdapter);
lView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
Toast.makeText(MainActivity.this, version[i]+" "+versionNumber[i], Toast.LENGTH_SHORT).show();
);
由于某种原因,当我在列表适配器上调用 notifySetDataChanged
时,它不起作用。
我也尝试重新创建适配器,但它没有更新。
有人可以帮我如何正确刷新列表视图吗?
【问题讨论】:
您在哪里刷新代码中的适配器?您是否更新了数组中的任何值以调用该刷新? 【参考方案1】:当您调用notifyDataSetChanged()
时,您必须提供一个新的数据源。如果您只调用notifyDataSetChanged()
而没有任何新数据,您的列表将不会刷新。因此,在调用notifyDataSetChanged()
之前,请尝试更新您在构造函数中传递的数组数据。但是在适配器内部添加更新功能是一个好习惯。此外,您可以使用 ArrayList 代替简单的数组。
【讨论】:
以上是关于如何使用 BaseAdapter 刷新带有文本和图像的列表视图?的主要内容,如果未能解决你的问题,请参考以下文章
如何在android中使用baseadapter刷新自定义列表视图
带有 BaseAdapter 的 Android ListView 内的 ImageButton