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: 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: 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?
Code:
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");
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
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
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;
}
}
See Question&Answers more detail:
os