0

I created a custom adapter class and a getter and setter class called Bean. This is to make a list view that has a textView and an image.

How can I populate myList so that it is usable when i'm setting the adapter and thus displays the corresponding text and image from my arrays into my listView?

I have provided the code for my adapter and bean class as well as my Main Activity Class' Async Task , but the problem is in my onPostExecute method from my Async class.

Just To Clarify. This code has NOT been tested and therefore has not returned any errors. My question is how do I populate myList in the onPostExecute method with the information from the String arrays "descriptionArray" and "photoArray".

My Main Activity Class' Async Task

    private class MyTask extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {

        String content = HttpManager.getData(params[0]);
        return content;
    }




//-----------------------THIS IS WHERE THE ISSUE IS HAPPENING---------------------------
    @Override
    protected void onPostExecute(String result) {
        hideDialog();
        String parseResult = InfoJSONResultParser.parseFeed(result);

        importerArray = OrderInformationParser.orderParser(result);

        if (parseResult.equals("ok")) {
            //Returns the Array with the JSON info already parsed.
            List<Bean> myList = new ArrayList<>(); //<---***How to populate this***




//***With  the information from these two String arrays.***
            String[] descriptionArray = OrderInformationParser.orderParser(result);
            String[] photoArray = PhotoParser.photoParser(result);


            //This creates and executes the list
            list = (ListView)findViewById(R.id.orderListView);


            //***So i can then transfer over to this adapter***
            MyAdapter adapter = new MyAdapter(MainActivity.this, myList);
            list.setAdapter(adapter);


        } else {
            findViewById(R.id.nullOrders).setVisibility(View.VISIBLE);
        }
    }

}

Adapter Class

public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<Bean> mList;

public MyAdapter(Context context,List<Bean> list){
    mContext=context;
    mList=list;
}

@Override
public int getCount() {
    return mList.size();
}

@Override
public Object getItem(int position) {
    return mList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    //use converview recycle
    if(convertView==null){
        holder=new ViewHolder();
        convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false);
        holder.textView= (TextView) convertView.findViewById(R.id.textView2);
        holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    //set text and url
    holder.textView.setText(mList.get(position).getText());
    Picasso.with(mContext).load(mList.get(position).getUrl()).into(holder.imageView);

    return convertView;
}

class ViewHolder{
    TextView textView;
    ImageView imageView;

}
}

Bean Class

public class Bean {
String text;
String url;

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}
}
2
  • What's the problem? Does it throw an exception? Please be more specific on your issue Commented Jul 8, 2016 at 17:54
  • @rsella It's not throwing an exception. I simply don't know how to populate myList<Bean> with the information from the String arrays. Commented Jul 8, 2016 at 18:01

2 Answers 2

2

You can populate your List by iterating your 2 Arrays and add the Strings to the Bean Objects.

Example:

 private List<Bean> populateBeanList(List<Bean> myList, String[] descArray, String[] photoArray){

     for(int i=0; i< descArray.length; i++){

      Bean bean = new Bean();
      bean.setText(descArray[i]);
      bean.setUrl(photoArray[i]);
      myList.Add(bean);
     }

   return myList;
} 

Then call the function in your Async Class

    private class MyTask extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {

        String content = HttpManager.getData(params[0]);
        return content;
    }




//-----------------------THIS IS WHERE THE ISSUE IS HAPPENING---------------------------
    @Override
    protected void onPostExecute(String result) {
        hideDialog();
        String parseResult = InfoJSONResultParser.parseFeed(result);

        importerArray = OrderInformationParser.orderParser(result);

        if (parseResult.equals("ok")) {
            //Returns the Array with the JSON info already parsed.
            List<Bean> myList = new ArrayList<>(); //<---***How to populate this***




//***With  the information from these two String arrays.***
            String[] descriptionArray = OrderInformationParser.orderParser(result);
            String[] photoArray = PhotoParser.photoParser(result);

        myList = populateBeanList(myList,descriptionArray, photoArray);

        //This creates and executes the list
        list = (ListView)findViewById(R.id.orderListView);


        //***So i can then transfer over to this adapter***
        MyAdapter adapter = new MyAdapter(MainActivity.this, myList);
        list.setAdapter(adapter);


    } else {
        findViewById(R.id.nullOrders).setVisibility(View.VISIBLE);
    }
}

}

Update:

public class MyAdapter extends BaseAdapter {
private Activity activity;
private List<Bean> mList;
private static LayoutInflater inflater;

public MyAdapter(Activity act,List<Bean> list){
    activity=act;
    mList=list;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return mList.size();
}

@Override
public Object getItem(int position) {
    return mList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    //use converview recycle
    if(convertView==null){
        convertView = inflater.inflate(R.layout.content_orders, null);
        holder=new ViewHolder();
        holder.textView= (TextView) convertView.findViewById(R.id.textView2);
        holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    //set text and url
    holder.textView.setText(mList.get(position).getText());
    Picasso.with(activity).load(mList.get(position).getUrl()).into(holder.imageView);

    return convertView;
}

class ViewHolder{
    TextView textView;
    ImageView imageView;

}
}
Sign up to request clarification or add additional context in comments.

5 Comments

Although the best way to populate the list would be to serialize the Web response into a List<Bean> using ie: GSON, Jackson.
This is exactly what i ended up doing and it worked using the text portion so thank you! I assume i must be doing something wrong in my adapter class because my image still isn't displaying. Only the text View.
Did you figure out what's going on with your Adapter?
I have not. I've been working at it for a while now but even using the debugger it's hard to figure out exactly what's going on. I'm positive it's in the adapter. I might have to stray away from Picasso...perhaps that's just not the best thing to use with a listview.
I figured it out! It was not even in my adapter, it was a silly mistake in how i was setting up my image urls. Thanks for the help!
0

From what i understood of your issue, you don't know how to populate the list.

Well, that's an easy task. Let's assume that descriptionArray contains the texts and photoArray contains the urls.

The needed code is that:

First, add a constructor to the Bean class for convenience:

public Bean(String text, String url) {
    this.text = text;
    this.url = url;
}

It simply add a non-empty constructor so we can initialize class fields directly on instance creation.

Second, call the method add of ArrayList. Like this:

myList.add(new Bean(text, url))

Obviously you need to put it in a for loop (for every item of arrays you insert a new Bean). So it will be like this:

for (int i=0; i<descriptionArray.lenght; i++) {
    myList.add(new Bean(descriptionArray[i], photoArray[i]);
}

This will create a new Bean for every pair in descriptionArray and photoArray.

As such, you'll need to check if the two arrays have the same size. For doing this you must put the for loop in an if:

if (descriptionArray.lenght == photoArray.lenght) {
    //execute for loop
}
else {
    //Arrays have different size. Wtf? Manage the error
}

Maybe time do a little Google research on the topic before posting ;)

Hope it helps a little

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.