2017-08-12 59 views
0

我需要存儲幾個不同的變量,然後將它們作爲數組發送到ListViewAdapter,以便我可以創建自定義行。但是,由於來自Array的對象使用AsyncTask,因爲我使用的是JSoup並訪問Internet,所以在將自定義視圖返回到主類之前,我需要一種方法在自定義適配器中檢索這些變量。代碼如下:Android:使用AsyncClass對象數組創建ListView適配器

的ListView適配器代碼:

public class CustomAdapter extends ArrayAdapter<getProductAttributes> implements OnCallCompleteCallBack{ 
String title; 
String price; 
Bitmap image; 

    //ArrayAdapter needs constructor, second parameter gets the layout for the list, third parameter is the array itself. "Context" always means background information. 
    CustomAdapter(Context context, getProductAttributes[]items){ 
     super(context,R.layout.custom_row, items); 
    } 

    public void onCallComplete(int listSize, String title, String price, String imageSRC, String productURL){ 
     this.title=title; 
     this.price = price; 
     //Get Bitmap for image 
     Bitmap mIcon11 = null; 
      try { 
       InputStream in = new java.net.URL(imageSRC).openStream(); 
       mIcon11 = BitmapFactory.decodeStream(in); 
      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
     this.image=mIcon11; 
    } 

    @NonNull 
    @Override 
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 
     LayoutInflater inflater = LayoutInflater.from(getContext()); 
     //One custom view is going to be equal to one row 
     View customView = inflater.inflate(R.layout.custom_row,parent,false); 
     ImageView productImage = (ImageView)customView.findViewById(R.id.imageView); 
     TextView itemTitle = (TextView)customView.findViewById(R.id.itemTitle); 
     TextView itemPrice = (TextView)customView.findViewById(R.id.itemPrice); 

     productImage.setImageBitmap(image); 
     itemTitle.setText(title); 
     itemPrice.setText(price); 
     return customView; 

    } 


} 

getProductAttributes類,它擴展的AsyncTask:

public class getProductAttributes extends AsyncTask<Object, Object, Void> { 
    OnCallCompleteCallBack callback; 
    String url; 
    int index; 
    String productURL; 
    private String price; 
    private String title; 
    private String imageSRC; 
    ImageView productView; 
    int listSize; 
    int result; 

public getProductAttributes(String url, int index, OnCallCompleteCallBack callback) { 
    this.url = url; 
    this.index = index; 
    this.callback = callback; 
} 

protected Void doInBackground(Object... voids) { 
    try{ 
     (code that gets all of the attributes, I'm sure that this code is working fine) 
     }catch(Exception e){} 

    return null; 
    } 

protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     if (callback != null) { 
      callback.onCallComplete(listSize,title,price,imageSRC,productURL); 

     } 
    } 

我覺得現在的問題似乎是,到時候我在CustomAdapter中運行onCallComplete,customView已經返回。如果是這種情況,我能做些什麼來規避這種情況?

感謝您的時間和幫助!

回答

0

你永遠不會調用構造函數如下:

public getProductAttributes(String url, int index, OnCallCompleteCallBack callback) { 
    this.url = url; 
    this.index = index; 
    this.callback = callback; 
} 

爲了讓你的聽衆對象工作,你必須設置callback對象。也不要以小寫開始你的班級的第一個字母。

+0

是怎麼回事?我在onPostExecute方法中使用回調對象,所以我不知道我應該做什麼,因爲我也使用Activity的回調來設置listSize,並且工作正常。此外,謝謝指出較低的getProductAttributes,我甚至沒有注意到它。 – coolyfrost

+0

您的CustomAdapter類正在實現接口:OnCallCompleteCallBack,然後CustomAdapter正在實現OnCallCompleteCallBack接口的方法,並充當偵聽器。現在,在您的getProductAttributes類中,您正在使用回調OnCallCompleteCallBack對象(回調)。這個對象(回調)需要由正在監聽的類初始化,在這種情況下是CustomAdapter。 – HaroldSer

+0

告訴我你在哪裏設置類「getProductAttributes」的構造函數。目前,對象回調來自:「callback.onCallComplete(listSize,title,price,imageSRC,productURL);」有一個來自設置getProductAttributes的構造函數的類的引用。您的CustomAdapter類無法偵聽「callback.onCallComplete(listSize,title,price,imageSRC,productURL);」 – HaroldSer