2016-07-08 46 views
0

我创建了一个自定义的适配器类和一个名为Bean的getter和setter类。这是制作一个包含textView和图像的列表视图。如何用两个数组填充自定义列表。

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

我已经提供了我的适配器和bean类的代码以及我的主要活动类的异步任务,但是问题出在我的Async类的onPostExecute方法中。

只是为了澄清。此代码尚未经过测试,因此未返回任何错误。我的问题是如何使用字符串数组“descriptionArray”和“photoArray”中的信息在onPostExecute方法中填充myList。

我的主要活动类异步任务

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

} 

适配器类别

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类

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

什么问题?它是否会抛出异常?请更具体地对您的问题 – rsella

+0

@rsella这不是一个例外。我根本不知道如何用字符串数组中的信息填充myList 。 – Kekis2014

回答

2

您可以通过迭代的2个阵列填充您的列表,并添加豆的对象的字符串。

例子:

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

然后调用该函数在异步类

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; 

} 
} 
+1

尽管填充列表的最佳方式是使用ie:GSON,Jackson将Web响应序列化为列表。 – rlshaw

+0

这正是我最终做的,它使用文本部分,所以谢谢你!我假设我必须在我的适配器类中做错了什么,因为我的图像仍然没有显示。只有文字查看。 – Kekis2014

+0

你知道你的适配器是怎么回事吗? – rlshaw

0

从我理解你的问题,你不知道如何填充列表。

那么,这是一件容易的事。假设descriptionArray包含文本,photoArray包含网址。

所需的代码是:

首先,构造添加到Bean类方便:

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

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

二,调用方法addArrayList。就像这样:

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

显然,你需要把它放在一个循环(对于阵列的每一个项目,你插入一个新的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 
} 

也许时间做发布前的话题有点谷歌的研究;)

希望它可以帮助一点点