如何在使用模型类的自定义适配器中使用数组填充列表视图? [英] How to populate list view using array in a custom adapter using model class?
问题描述
我正在尝试修改此代码教程: https://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial
i was trying to adapt this code tutorial: https://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial
对于我的列表视图,本教程使用自定义适配器和模式类.在本教程中,他们使用字符串填充行,我需要使用字符串数组填充行,此字符串数组包含特定文件夹内文件的字符串. 已经尝试转换字符串数组中的所有字符串,但出现此错误:
for my list view, the tutorial use a custom adapter and a modal class. In the tutorial they are using strings to populate the rows, i need to populate the row using a string array, this string array contains the string of the files inside a specific folder. Already try to convert all the strings in string array but i get this error:
64:错误:找不到适用于setText(String [])的合适方法
64: error: no suitable method found for setText(String[])
viewHolder.txtTitle.setText(dataModel.getFicheros());
^
method TextView.setText(CharSequence) is not applicable
(argument mismatch; String[] cannot be converted to CharSequence)
method TextView.setText(int) is not applicable
(argument mismatch; String[] cannot be converted to int)
CustomAdapter.java:65:错误:找不到适合的方法 setText(String [])
CustomAdapter.java:65: error: no suitable method found for setText(String[])
viewHolder.txtFecha.setText(dataModel.getFecha());
^
method TextView.setText(CharSequence) is not applicable
(argument mismatch; String[] cannot be converted to CharSequence)
method TextView.setText(int) is not applicable
(argument mismatch; String[] cannot be converted to int)
如何使用数组填充它们?
How can i populate them using an array?
代码:
Mainactivy.java
Mainactivy.java
final File carpeta = new
String[] fecha = new String[] { "a", "b", "c" , "b", "c", "b", "c", "b", "c", "b", "c", "b", "c", "b", "c","b", "c" , "b", "c", "b", "c", "b", "c", "b", "c", "b", "c", "b", "c","b", "c" , "b", "c", "b", "c", "b", "c", "b", "c", "b", "c", "b", "c"};
File("/storage/emulated/0/Android/data/cc.openframeworks.androidMultiOFActivitiesExample/files/xml"); 列表列表= new ArrayList();
File("/storage/emulated/0/Android/data/cc.openframeworks.androidMultiOFActivitiesExample/files/xml"); List list = new ArrayList();
ficheros = list.toArray(new String[list.size()]);
listarFicherosPorCarpeta(carpeta);
listView=(ListView)findViewById(R.id.listview);
dataModels= new ArrayList<>();
dataModels.add(new DataModel(ficheros,fecha));
dataModels.add(new DataModel(ficheros,fecha));
adapter= new CustomAdapter(dataModels,getApplicationContext());
listView.setAdapter(adapter);
public void listarFicherosPorCarpeta(final File carpeta) {
for (final File ficheroEntrada: carpeta.listFiles()) {
if (ficheroEntrada.isDirectory()) {
listarFicherosPorCarpeta(ficheroEntrada);
} else {
System.out.println(ficheroEntrada.getName());
list.add(ficheroEntrada.getName());
}
}
// listAdapter.notifyDataSetChanged();
}
CustomAdapter.java
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<DataModel> {
private ArrayList<DataModel> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView txtTitle;
TextView txtFecha;
}
public CustomAdapter(ArrayList<DataModel> data, Context context) {
super(context, R.layout.rowlayout, data);
this.dataSet = data;
this.mContext=context;
}
private int lastPosition = -1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
DataModel 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.rowlayout, parent, false);
viewHolder.txtTitle = (TextView) convertView.findViewById(R.id.listtext);
viewHolder.txtFecha = (TextView) convertView.findViewById(R.id.fechatxt);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
result.startAnimation(animation);
lastPosition = position;
viewHolder.txtTitle.setText(dataModel.getFicheros());
viewHolder.txtFecha.setText(dataMlodel.getFecha());
// Return the completed view to render on screen
return convertView;
}
}
Datamodel.java
Datamodel.java
package cc.openframeworks.androidMultiOFActivitiesExample;
public class DataModel {
String[] ficheros;
String[] fecha;
public DataModel(String[] ficheros, String[] fecha) {
this.ficheros = ficheros;
this.fecha = fecha;
}
public String[] getFicheros() {
return ficheros;
}
public String[] getFecha() {
return fecha;
}
}
推荐答案
行,
viewHolder.txtTitle.setText(dataModel.getFicheros());
viewHolder.txtFecha.setText(dataMlodel.getFecha()); // typo error, it's dataModel.getFecha()
CustomAdapter
中出现的
从DataModel
类中调用getFicheros()
和getFecha()
.
present in your CustomAdapter
calls getFicheros()
and getFecha()
from your DataModel
class.
现在的问题是getFicheros()
返回一个String[]
,不幸的是,没有一个单独的 Android TextView
setText()
方法被设计为将String[]
作为参数使用( setText()
方法的5个 重载 版本.
Now the issue is getFicheros()
returns a String[]
, unfortunately not a single Android TextView
setText()
method is designed to work with String[]
as a parameter ( there are in all 5 overloaded versions of setText()
method).
因此您将收到 64: error: no suitable method found for setText(String[])
.
Hence you are receiving 64: error: no suitable method found for setText(String[])
.
您的getFecha()
方法存在相同的问题.
Same issue exist with your getFecha()
method.
这篇关于如何在使用模型类的自定义适配器中使用数组填充列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!