2016-05-23 69 views
0

夥計們我正在開發一個android項目。我想在列表中顯示5個項目,但我只有最後一個數組項目,基本上這個數組來自JSONarray。我怎麼解決這個問題?這是我的主要活動類Listadapter僅顯示android中數組的最後一個值?

 try { 
      Log.d("hy one ","two"); 
      //JSONObject jobj=new JSONObject(data1); 
      JSONArray jr=new JSONArray(data1);    
      for (int i=0;i<jr.length();i++) { 
       jsonObject = jr.getJSONObject(i); 
       like = jsonObject.getString(like_count); 
      } 
      JSONArray jsonArray3=jsonObject.getJSONArray("comments"); 
      HashMap<String, String> arr; 
      for(int j=0;j<jsonArray3.length();j++){ 
      arr = new HashMap<>(); 
      JSONObject jsonObject3 = jsonArray3.getJSONObject(j); 
      name11=jsonObject3.getString(name1); 
      Log.d("name value",name11); 

      arr.put("name",name11); 
      arrayList.add(arr); 
     } 
     Log.d("arraylist", String.valueOf(arrayList)); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 
@Override 
protected void onPostExecute(String s) { 
    super.onPostExecute(s); 
    progressDialog.dismiss(); 
    /*list_adapter=new list_adapter(Dashboard.this); 
    listcomment.setAdapter(listAdapter);*/ 
    adapter= new Dashboard_Adapter(Dashboard.this,arrayList); 
    // Log.d("adapter", String.valueOf(adapter)); 

    listview.setAdapter(adapter); 
} 

這是我的適配器

public class Dashboard_Adapter extends BaseAdapter { 
    ViewHolder viewHolder; 
    SharedPreferences sharedPreferences; 
    private LayoutInflater layoutInflater = null; 
    // ArrayList<String> arr; 
    private Activity activity; 
    public Context context; 
    HashMap<String, String> arr; 
    ArrayList<HashMap<String, String>> arrayList; 
    public Dashboard_Adapter(Context dashbord, ArrayList<HashMap<String, String>> arrayList) { 
     activity = (Activity) dashbord; 
     this.arrayList=arrayList; 
     layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     sharedPreferences = activity.getSharedPreferences(Login.My_Prefs, Context.MODE_PRIVATE); 
    } 

    @Override 
    public int getCount() { 
     return arrayList.size(); 
     // Log.d("arraysize",arrayList.size()); 

    } 

    @Override 
    public Object getItem(int position) { 
     return arrayList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

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

     View view = convertView; 
     final ViewHolder viewHolder; 
     if (convertView == null) { 
      view = layoutInflater.inflate(R.layout.omments, parent, false); 
      viewHolder = new ViewHolder(); 
      viewHolder.Name = (TextView) view.findViewById(R.id.txtTags23); 

      //comment 
      // viewHolder.btnPromote = (Button) view.findViewById(R.id.btnFeedResponce); 

      view.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) view.getTag(); 
     } 
     arr = new HashMap<>(); 
     arr = arrayList.get(position); 

     viewHolder.Name.setText(Dashboard.name11); 




     return view; 
     // Glide.with(context).load(images).placeholder(R.drawable.home).into(imageView); 
    } 

    public static class ViewHolder { 
     TextView Name; 


    } 

} 

我認爲問題出在適配器。

在此先感謝:)

+1

你有沒有將其送入適配器上onPostExecute前檢查使用日誌ArrayList的大小()? –

+0

將所有解析代碼移到'for(int i = 0; i

+1

是的,我檢查過。所有值都存在於arraylist中,但未顯示最終結果 –

回答

0
JsonObject jsonObject = New JsonObject(); 

在for循環

相關問題