9

i die https://github.com/Diolor/Swipecards müssen gesetzt verwenden und es funktioniert perfekt, wenn Daten statisch ist, aber es funktioniert nicht mit AsyncTaskNicht in der Lage SwipeFlingAdapterView von Array-Adapter

Jetzt i den Adapter mit json Daten binden möchten. aber es funktioniert nicht.

public class HomeFragment extends Fragment { 

    private Context context; 

    private ArrayList<Productp> al; 
    private ArrayAdapter<Productp> arrayAdapter; 

    SwipeFlingAdapterView flingContainer; 

    ImageView img_product; 

    private ProgressDialog pDialog; 

    JSONParser jParser = new JSONParser(); 
    private static String url_all_products = "http://www.example.com/prod.php"; 

    // JSON Node names 
    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_PRODUCTS = "products"; 
    private static final String TAG_IMAGE = "url"; 

    JSONArray products = null; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_home, container, 
       false); 
     context = getActivity(); 

     initUI(rootView); 
     return rootView; 
    } 

    private void initUI(View view) { 

     flingContainer = (SwipeFlingAdapterView) view.findViewById(R.id.frame); 


     new Loaditems().execute(); 


    } 

    class Loaditems extends AsyncTask<String, String, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

      pDialog = new ProgressDialog(context); 
      pDialog.setMessage("Loading products. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 

     } 

     protected String doInBackground(String... args) { 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 

      params.add(new BasicNameValuePair("range", "1")); 

      JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", 
        params); 

      try { 
       // Checking for SUCCESS TAG 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        products = json.getJSONArray(TAG_PRODUCTS); 

        al = new ArrayList<Productp>(); 

        for (int i = 0; i < products.length(); i++) { 

         JSONObject c = products.getJSONObject(i); 

         Productp pp = new Productp(); 

         pp.portal = c.getString(TAG_IMAGE); 

         al.add(pp); 
        } 
       } else { 

       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 

      } 

      return null; 
     } 

     protected void onPostExecute(String file_url) { 
      pDialog.dismiss(); 

      arrayAdapter = new ProductAdpater(context, al); 
      flingContainer.setAdapter(arrayAdapter); 

     } 
    } 

Adapter Methode:

private class ProductAdpater extends ArrayAdapter<Productp> { 

     Context mContext; 

     public ProductAdpater(Context context, List<Productp> items) { 
      super(context, R.layout.row_homeview, items); 

      try { 
       this.mContext = context; 

      } catch (Exception e) { 

       e.printStackTrace(); 
      } 

     } 

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return al.size(); 
     } 

     @Override 
     public long getItemId(int position) { 
      // TODO Auto-generated method stub 
      return position; 
     } 

     public View getView(final int position, View convertView, 
       ViewGroup parent) { 

      View view = convertView; 
      ViewHolder holder = null; 

      LayoutInflater mInflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      if (view == null) { 
       view = mInflater.inflate(R.layout.row_homeview, parent, false); 
       holder = new ViewHolder(); 
       holder.imgIcon = (ImageView) view 
         .findViewById(R.id.addviewimage); 

       view.setTag(holder); 
      } else 
       holder = (ViewHolder) view.getTag(); 
      UrlImageViewHelper.setUrlDrawable(holder.imgIcon, 
        al.get(position).portal); 
      return view; 
     } 

     class ViewHolder { 

      ImageView imgIcon; 

     } 

    } 

    } 

productp Klasse:

public class Productp { 


    public String portal=""; 
} 

Wenn ich den obigen Code auszuführen. Es wird nichts angezeigt.

Auch dieser Code wird erfolgreich ausgeführt, bindet die Daten jedoch nicht an den Adapter.

In logcat wird kein Fehler angezeigt.

Ich habe auch debug den Code.

the execution exit from 

public ProductAdpater(Context mcontext, ArrayList<Productp> items) { 
      super(mcontext, R.layout.row_homeview, items); 

      try { 
       context = mcontext; 
       // this.list = items; 
      } catch (Exception e) { 
       // TODO: handle exception 
       e.printStackTrace(); 
      } 

     } 

Es es auszuführen nicht die getView Methode

public View getView(final int position, View convertView, 
      ViewGroup parent) {} 

, wie ich das gelöst ist?

+0

Versuchen erstreckt 'BaseAdapter' einmal statt' ArrayAdapter' –

+0

Versuchte das schon. Aber immer noch nicht in der Lage, Daten zu binden. –

+0

Okay. Und bist du sicher, dass das "items" -Array nicht leer ist und mindestens die Größe> 1 hat? –

Antwort

5

Ich habe das gelöst.

Ich habe gerade die - arrayAdapter.notifyDataSetChanged(); onPostExecute.

Mein neuer aktualisierten Code auf OnPostExecute:

protected void onPostExecute(String file_url) { 
      pDialog.dismiss(); 

      arrayAdapter = new ProductAdpater(context, al); 
      flingContainer.setAdapter(arrayAdapter); 
      arrayAdapter.notifyDataSetChanged(); 
     } 

Es ist geschehen.

6

Sie müssen nur die aktualisierten Daten an den Adapter

unter Verwendung

notifyDataSetChanged() festgelegt benachrichtigen;

notifyDataSetInvalidated()

+0

ich habe es bereits vor 6 Stunden gelöst. –

+0

Ja ist es in Ordnung, ich gebe Ihnen nur die Erklärung für den Aufruf notifyDataSetChanged(); notifyDataSetInvalidated() –