Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

java - How to populate list view using array in a custom adapter using model class?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The lines,

viewHolder.txtTitle.setText(dataModel.getFicheros());
viewHolder.txtFecha.setText(dataMlodel.getFecha()); // typo error, it's dataModel.getFecha()

present in your CustomAdapter calls getFicheros() and getFecha() from your DataModel class.

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).

Hence you are receiving 64: error: no suitable method found for setText(String[]).

Same issue exist with your getFecha() method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...