2016-07-08 49 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 
} 

也許時間做發佈前的話題有點谷歌的研究;)

希望它可以幫助一點點