我创建了一个自定义的适配器类和一个名为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;
}
}
什么问题?它是否会抛出异常?请更具体地对您的问题 – rsella
@rsella这不是一个例外。我根本不知道如何用字符串数组中的信息填充myList。 –
Kekis2014