0

我从另一个示例中获取的我的 CustomListViewAdapter 类并试图将它放在一起,但我被卡住了:

package com.test.testing;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListViewAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;

    public CustomListViewAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.customlist, null);

        TextView fileName = (TextView)vi.findViewById(R.id.tvFileName); // filename
        TextView lastMod = (TextView)vi.findViewById(R.id.tvLastMod); // artist name
        ImageView thumb_image = (ImageView)vi.findViewById(R.id.list_image); // thumb image

        HashMap<String, String> song = new HashMap<String, String>();
        song = data.get(position);

        // Setting all values in listview
        fileName.setText(song.get(PreviousTrip.FilesInFolder)); //set the name for FILENAME?
        lastMod.setText(song.get(PreviousTrip.FileLastModified)); //set the file modified date for FileLastModified?
        thumb_image.setImageResource(R.drawable.ic_launcher); //keep the same image for all files?
        return vi;
    }
}

我的 PrevTrip 类调用 CustomListViewAdapter 类:

public class PreviousTrip extends Fragment {
    ArrayList<String> FilesInFolder, FileLastModified, FileSize;
    File folder;
    ListView lv;
    @Override
    public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
        final RelativeLayout mFrame3 =  (RelativeLayout) inflater.inflate( R.layout.ptrip, container, false );
        folder = new File(Environment.getExternalStorageDirectory() + "/tc");
        FilesInFolder = GetFiles(folder.getAbsolutePath());
        FileLastModified = GetFileLM(folder.getAbsolutePath());
        lv = (ListView) mFrame3.findViewById(R.id.lvFiles);

        lv.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, FilesInFolder));
    }

    public ArrayList<String> GetFiles(String DirectoryPath) {
    ArrayList<String> MyFiles = new ArrayList<String>();
    File f = new File(DirectoryPath);

    //f.mkdirs();
    File[] files = f.listFiles();
    if (files.length == 0)
        return null;
    else {
        for (int i=0; i<files.length; i++)  {
            if (files[i].getName().endsWith(".tol")) {
                long lastTime = files[i].lastModified();
                String fileName = files[i].getName().substring(0, files[i].getName().lastIndexOf("."));
                MyFiles.add(fileName);
            }
        }
    }

    return MyFiles;
    }

    public ArrayList<String> GetFileLM(String DirectoryPath) {
    ArrayList<String> MyFiles = new ArrayList<String>();
    File f = new File(DirectoryPath);

    //f.mkdirs();
    File[] files = f.listFiles();
    if (files.length == 0)
        return null;
    else {
        for (int i=0; i<files.length; i++)  {
            if (files[i].getName().endsWith(".tol")) {
                long lastTime = files[i].lastModified();
                String dateString = DateFormat.format("MM/dd/yyyy", new Date(lastTime)).toString();
                MyFiles.add(dateString);
            }
        }
    }

    return MyFiles;
    }
}

根据我的应用程序,我对这些行有疑问,这没有意义

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    fileName.setText(song.get(PreviousTrip.FilesInFolder)); //set the name for FILENAME?
    lastMod.setText(song.get(PreviousTrip.FileLastModified)); //set the file modified date for FileLastModified?
    thumb_image.setImageResource(R.drawable.ic_launcher); //keep the same image for all files?

我想更换:

lv = (ListView) mFrame3.findViewById(R.id.lvFiles);

lv.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, FilesInFolder));

据我了解:

lv=(ListView)findViewById(R.id.lvFiles);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);        
list.setAdapter(adapter);

但是,而不是songsList如何通过FilesInFolderand FileLastModified

4

1 回答 1

1

我可能是错的,因为我不确定你遇到的确切问题,但我认为你想String从你那里得到一个具体的问题,ArrayList所以你会想要类似的东西

fileName.setText(song.get(PreviousTrip.FilesInFolder.get(position)));  // use the position in your Adapter to get that location in your ArrayList
lastMod.setText(song.get(PreviousTrip.FileLastModified.get(position)));

看起来Adapter当前正在接受 aHashMap但您有 aArrayList所以将其构造函数更改为

 public CustomListViewAdapter(Activity a, ArrayList<String> d) 
于 2013-08-05T20:40:16.973 回答