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

}

2
  • Create an ArrayList for your data then pass it to your adapter. Without seeing your adapter code, it is hard to know Commented Jul 15, 2016 at 16:43
  • Sorry- forgot to add, just added. Commented Jul 15, 2016 at 16:49

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

you're missing something like favoriteIte.findViewById(R.id.someTextView).setTextView(favoriteItem);
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
Thank you quidproquo! This worked for me, however the only thing i needed to add was the declaration of 'new' arrayList. See below: ArrayList<String> favlist = new ArrayList<>(Arrays.asList(favorites.split(",")));
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.