将自定义ListView与图像和文本放在一起

时间:2013-08-05 20:33:04

标签: java android android-adapter

我从另一个例子中获取的我的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如何通过FilesInFolderFileLastModified

1 个答案:

答案 0 :(得分: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目前正在接受HashMap,但您有一个ArrayList,因此将其构造函数更改为

 public CustomListViewAdapter(Activity a, ArrayList<String> d)