我創建了一個自定義的適配器類和一個名爲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