1

So i have a string array that I have. I am trying to pass each individual item from that string array to the custom adapter. I'm having trouble figuring out how to use the string that i pass in the custom adapter?

String[] that I pass

 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);
                }

Adapter.class

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;
}

}

AndroidDev21921
  • 695
  • 1
  • 8
  • 21

2 Answers2

1

It would seem like you are only passing strings in, so I am not sure why you are using an ArrayAdapter<favoriteList>.

If you change your adapter class to this:

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
}

Then when you pass the string, pass it like so:

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
Bill
  • 4,506
  • 2
  • 18
  • 29
  • you're missing something like favoriteIte.findViewById(R.id.someTextView).setTextView(favoriteItem); – piotrpo Jul 15 '16 at 17:18
  • op didn't specify what he wanted to do with the string, so I only demonstrated how to pass it and store it in a variable – Bill Jul 15 '16 at 17:21
  • Thank you quidproquo! This worked for me, however the only thing i needed to add was the declaration of 'new' arrayList. See below: ArrayList favlist = new ArrayList<>(Arrays.asList(favorites.split(","))); – AndroidDev21921 Jul 15 '16 at 18:57
0

I don't know how you wrote your 'FavoritesAdapter' class but having a static method 'add' and passing a string to it to build a list feels very wrong.

You can have a lot at this link https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView to see how to create a custom array adapter to ListView

Michael Dugah
  • 59
  • 1
  • 9