如何使用两个数组填充自定义列表。

How to populate a custom list with two arrays.

我创建了一个自定义适配器 class 和一个名为 Bean 的 getter 和 setter class。这是为了制作一个包含文本视图和图像的列表视图。

如何填充 myList 以便它在我设置适配器时可用,从而将数组中的相应文本和图像显示到我的 listView 中?

我已经为我的适配器和 bean class 以及我的 Main Activity Class' Async Task 提供了代码,但问题出在我的 Async 的 onPostExecute 方法中class。

澄清一下。此代码尚未经过测试,因此未返回任何错误。我的问题是如何使用来自字符串数组 "descriptionArray" 和 "photoArray".

的信息在 onPostExecute 方法中填充 myList

我的主要 Activity 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);


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

}

适配器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;

}
}

豆子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 个数组并将字符串添加到 Bean 对象来填充您的列表。

示例:

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

然后调用异步中的函数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);
    }
}

}

更新:

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;

}
}

根据我对你的问题的了解,你不知道如何填充列表。

嗯,这很容易。假设 descriptionArray 包含文本,photoArray 包含网址。

需要的代码是:

首先,Beanclass添加一个构造函数为了方便:

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

它只是添加了一个非空构造函数,因此我们可以在创建实例时直接初始化 class 字段。

其次,调用ArrayList的方法add。像这样:

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

显然你需要把它放在一个 for 循环中(对于数组的每一项你插入一个新的 Bean)。所以它会是这样的:

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

这将为 descriptionArrayphotoArray 中的每一对创建一个新的 Bean

因此,您需要检查两个数组的大小是否相同。 为此,您必须将 for 循环放在 if:

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

也许在发帖前花点时间Google研究一下这个话题;)

希望对您有所帮助