2016-05-27 22 views
0

Ich benutze Listview mit dynamischen Artikeln.Es speichert in falscher Ort (Das bedeutet 1. Artikel zeigt in dritten Artikel, zweiter Artikel zeigt in 5. Artikel, etc.) .Ich weiß nicht, wie zu lösen dieses. Ich habe den relevanten Code hinzugefügt. Bitte überprüfen Sie es.Listview Artikel wurden an falscher Stelle hinzugefügt

EDIT:

UpcomingGoalAdapter.java:

public class UpcomingGoalAdapter extends ArrayAdapter<UpcomingGoalItems> { 
    private Context context; 
    @SuppressWarnings("unused") 
    private List<UpcomingGoalItems> items; 
    String eventIdForVol; 
    private UpcomingGoalAdapter adapter; 
    String userIdStr, tokenStr; 

    public UpcomingGoalAdapter(Context context, int resource, List<UpcomingGoalItems> objects) { 
     super(context, resource, objects); 
     this.context = context; 
     this.items = objects; 
     this.adapter = this; 
    } 

    @SuppressLint("InflateParams") 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 

     final UpcomingGoalItems rowItem = getItem(position); 

     LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.adapt_upcoming_goal, null); 
      holder = new ViewHolder(); 

      holder.tvInterest = (TextView) convertView.findViewById(R.id.tv_interest_goal_adapt); 
      holder.ivEdit = (ImageView) convertView.findViewById(R.id.iv_edit_goal_adapt); 
      holder.tvCount = (TextView) convertView.findViewById(R.id.tv_count_goal_adapt); 
      holder.ivDelete = (ImageView) convertView.findViewById(R.id.iv_delete_goal_adapt); 


      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 



     if((rowItem.getInterest().equals(null) || rowItem.getInterest().equals("")) 
       && (rowItem.getLocation().equals(null) || rowItem.getLocation().equals("")) 
       && (rowItem.getFromDate().equals(null) || rowItem.getFromDate().equals("")) 
       && (rowItem.getToDate().equals(null) || rowItem.getToDate().equals(""))) { 

      holder.tvInterest.setText("-"); 

     } else { 

      holder.tvInterest.setText("Looking for "+rowItem.getInterest() + " in " + rowItem.getSplitLocation() + " On (" 
        + rowItem.getFromDate() + " - " + rowItem.getToDate()+") "); 
     } 


     holder.tvCount.setText(rowItem.getCount()); 

     return convertView; 
    } 

    private class ViewHolder { 
     TextView tvInterest; 
     ImageView ivEdit; 
     TextView tvCount; 
     ImageView ivDelete; 
    } 

UpcomingGoalActivity.java:

 ArrayList<UpcomingGoalItems> itemsaArrayList; 
     UpcomingGoalAdapter itemsAdapter; 
     ListView listView; 

     itemsaArrayList = new ArrayList<UpcomingGoalItems>(); 
      itemsAdapter = new UpcomingGoalAdapter(UpcomingGoalActivity.this, R.layout.adapt_upcoming_goal, itemsaArrayList); 

      listView = (ListView) findViewById(R.id.lv_interest_goal_search); 

     listView.setAdapter(itemsAdapter); 

     hitGoalApi(); 


    private void hitGoalApi(){ 


     String myGoalsUrl = PK_MY_GOALS; 
     Log.e("myGoalsUrl", myGoalsUrl); 

     StringRequest request = new StringRequest(Request.Method.GET, myGoalsUrl, new Response.Listener<String>() { 

      @Override 
      public void onResponse(String response) { 

       dialog.dismiss(); 

       if(response != null && !response.startsWith("<HTML>")){ 

        Log.e("MyGoalsRes", response); 

        try { 

         JSONObject jsonObject = new JSONObject(response); 

         JSONArray jsonArrData = jsonObject.getJSONArray("data"); 

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

          JSONObject getDataJsonObj = jsonArrData.getJSONObject(i); 

          LOCATION = getDataJsonObj.getString("location"); 

          String[] placeArray = LOCATION.split("\\s*,\\s*"); 

          Log.e("placeArray", ""+ Arrays.toString(placeArray)); 

          String placeStr = placeArray[0]; 

          FROM_DATE = getDataJsonObj.getString("fromdate"); 

          TO_DATE = getDataJsonObj.getString("todate"); 

          GOAL_ID = getDataJsonObj.getString("g_id"); 

          getInterest = getDataJsonObj.getString("users_interest_goals"); 

          hitCountApi(GOAL_ID, placeStr, FROM_DATE, TO_DATE, getInterest); 



         } 

        } 

        catch (JSONException e){ 

         e.printStackTrace(); 
        } 

       }else{ 

        toastShort(getApplicationContext(), "Check Internet"); 
       } 

      } 
     } 


    private void hitCountApi(final String goalId, final String splitLoc, final String fromDate, 
          final String toDate, final String getInteresn) { 


     String countUrl = PK_GOAL_SEARCH + goalId + ".json"; 
     Log.e("countUrl", countUrl); 

     StringRequest request = new StringRequest(Request.Method.GET, countUrl, new Response.Listener<String>() { 

      @Override 
      public void onResponse(String response) { 

       dialog.dismiss(); 

       if (response != null && !response.startsWith("<HTML>")) { 

        Log.e("CountRes", response); 

        try { 

         JSONObject jsonObject = new JSONObject(response); 

         JSONObject metaJsonObj = jsonObject.getJSONObject("_metadata"); 

         String totalRecStr = metaJsonObj.getString("total_records"); 

         Log.e("totalRecStr", "" + totalRecStr); 

         UpcomingGoalItems items = new UpcomingGoalItems(); 

         items.setSplitLocation(splitLoc); 
         items.setFromDate(fromDate); 
         items.setToDate(toDate); 
         items.setGoalId(goalId); 
         items.setInterest(getInterest); 
         items.setCount(totalRecStr);     ; 

         itemsaArrayList.add(items); 
         itemsAdapter.notifyDataSetChanged(); 


        } catch (JSONException e) { 

         e.printStackTrace(); 
        } 

       } else { 

        toastShort(getApplicationContext(), "Check Internet"); 
       } 

      } 
     }) 

in der Größenordnung Volley Antwort Basierend Ich habe das Recht zeigen, ID für Listview.It ta kes falsche Reihenfolge.

Lassen Sie mich wissen, warum es an der falschen Stelle bestellt und wie man das löst.Vielen Dank.

Antwort

0

TLDR: Verwenden Sie Arraylist statt List:

private ArrayList<UpcomingGoalItems> items; 

Die ArrayAdapters Ich benutze neigen wie folgt aussehen:

(der Arraylist < String> Namen ist eine Arraylist aus den Namen der Arraylist erstellt < Aufgabe>)

public class ArrayAdapterTask extends ArrayAdapter<String> { 

    private final Context context; 
    private int layoutId; 
    private ArrayList<Task> tasks; 

    public ArrayAdapterTask(Context context, ArrayList<String> names, ArrayList<Task> tasks, int layoutId) { 
     super(context, layoutId, names); 

     this.context = context; 
     this.tasks = null; 
     this.tasks = tasks; 
     this.layoutId = layoutId; 
    } 

    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     // Do stuff with the tasks.get(position) 
    } 
} 
Verwandte Themen