2017-08-12 12 views
0

いくつかの変数を保存してからListViewAdapterに送信してカスタムの行を作成する必要があります。しかし、JSoupを使用しているので、配列から来るオブジェクトはAsyncTaskを使用するため、カスタムビューをメインクラスに戻す前にカスタムアダプタでこれらの変数を取得する必要があります。次のようにコードがある:Android:AsyncClassオブジェクト配列を持つListViewアダプタを作成する

リストビューアダプターコード:AsyncTaskを拡張

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クラス:

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

     } 
    } 

私は問題があることは、時間によって私のようだと思いますCustomAdapteronCallCompleteを実行すると、customViewが既に返されています。このような場合は、これを回避するために何ができますか?

あなたの時間と助けてくれてありがとう!

答えて

0

あなたは、コンストラクタの下に呼び出すことはありません:

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

あなたがcallbackオブジェクトを設定する必要が動作するようにリスナーオブジェクトをもらうために。また、小文字でクラスの最初の文字を開始しないでください。

+0

どのようにですか?私はonPostExecuteメソッドの中でコールバックオブジェクトを使用していますので、私はまた、ListSizeを設定するためにActivity上のコールバックを使用しているので、それ以外は何をしているのか分かりません。また、下位のケースの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

関連する問題