gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:16:41 25 4
gpt4 key购买 nike

我的 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

最佳答案

我可能是错的,因为我不确定您遇到的确切问题,但我认为您想从 ArrayList 中获取特定的 String 所以您会想要类似的东西

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) 

关于java - 将自定义 ListView 与图像和文本放在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18067191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com