Kekis2014 Kekis2014 - 4 months ago 8
Java Question

How to populate a custom list with two arrays.

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

Answer

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

}

Comments