gpt4 book ai didi

android - 使用带有自定义适配器的字符串数组到 ListView ?

转载 作者:行者123 更新时间:2023-11-30 01:11:27 24 4
gpt4 key购买 nike

所以我有一个字符串数组。我试图将每个单独的项目从该字符串数组传递到自定义适配器。我无法弄清楚如何使用我在自定义适配器中传递的字符串?

我传递的字符串[]

 String favorites = String.valueOf(FavList.get(0).get("favorites_list"));

String[] separated = favorites.split(",");

for (String s: separated) {
//Do your stuff here
FavoritesAdapter.add(s);
}

适配器类

public class FavoritesAdapter extends ArrayAdapter<favoriteList> {
private final Context mContext;
private List<favoriteList> favlist;
private TextView favorites;
private TextView favDetail;

public FavoritesAdapter(Context context, ArrayList<favoriteList> objects) {
super(context, R.layout.favorites_listview_single, objects);
this.mContext = context;
this.favlist = objects;
}


public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
convertView = mLayoutInflater.inflate(R.layout.favorites_listview_single, null);
}


// Here is where i cant figure out how to get the string that I passed.






return convertView;
}

最佳答案

看起来你只是传递字符串,所以我不确定你为什么使用 ArrayAdapter<favoriteList> .

如果您将适配器类更改为此:

public class FavoritesAdapter extends ArrayAdapter<String> {
private final Context mContext;
private ArrayList<String> favlist;
private TextView favorites;
private TextView favDetail;

public FavoritesAdapter(Context context, ArrayList<String> objects) {
super(context, R.layout.favorites_listview_single, objects);
this.mContext = context;
this.favlist = objects;
}

public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
convertView = mLayoutInflater.inflate(R.layout.favorites_listview_single, null);
}

String favoriteItem = favlist.get(position) //get the string you passed


return convertView;
}

//...more code
}

然后当你传递字符串时,像这样传递它:

String favorites = String.valueOf(FavList.get(0).get("favorites_list"));
ArrayList<String> favlist = (ArrayList<String>)Arrays.asList(favorites.split(","));

FavoritesAdapter adapter = new FavoritesAdapter(getApplicationContext(), favlist);
listView.setAdapter(adapter); //where listView is the view you declared previously

关于android - 使用带有自定义适配器的字符串数组到 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38401201/

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