Kekis2014
Kekis2014

Reputation: 145

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

Upvotes: 0

Views: 221

Answers (2)

rlshaw
rlshaw

Reputation: 46

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;

}
}

Upvotes: 2

rsella
rsella

Reputation: 337

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

Upvotes: 0

Related Questions