2017-09-13 65 views
-1

我正在android中創建一個列表。這是我從服務器獲取列表,並將其添加到一個ArrayList.After,我創建了一個適配器來顯示列表,但問題是,我有總共兩個值列表中,但ListView只顯示我1項。我檢查了很多。我查了適配器。它總共有2個項目,但只顯示1.爲什麼這個問題。請幫忙。從服務器無法在ListView中獲取所有值android

//擷取列表

JSONArray askArray = response.getJSONArray("data"); 

         if (askArray.length() > 0) { 

          final ArrayList<HashMap<String, String>> cameraList = new ArrayList<HashMap<String, String>>(); 
          for (int i = 0; i < askArray.length(); i++) { 
           JSONObject cameraObject = askArray.getJSONObject(i); 
           //Log.e("TAG", "onResponse: "+cameraObject.getString("description")); 
           HashMap<String, String> store = new HashMap<String, String>(); 
           store.put(KEY_DESCRIPTION, cameraObject.getString("description")); 
           cameraList.add(store); 
          } 

//適配器列表視圖的

public class AdapterTermsNConditions extends BaseAdapter { 


private Activity activity; 
private ArrayList<HashMap<String, String>> data; 
private static LayoutInflater inflater = null; 
private Context context; 
AskFormFragment askFormFragment; 
HashMap<String, String> officeList1; 


public AdapterTermsNConditions(Activity a, ArrayList<HashMap<String, String>> d, AskFormFragment askFormFragment) { 
    activity = a; 
    data = d; 
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    this.askFormFragment = askFormFragment; 

} 

public int getCount() { 
    return data.size(); 
} 

public Object getItem(int position) { 
    return position; 
} 

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

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

    View itemView = inflater.inflate(R.layout.item_terms_conditions, parent, false); 

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

    TextView txtTerms = (TextView) itemView.findViewById(R.id.txtTerms); 


    officeList1 = new HashMap<String, String>(); 
    officeList1 = data.get(position); 
    Log.e("termsList", officeList1.toString() + "" +position); 


    txtTerms.setText(officeList1.get(AskFormFragment.KEY_DESCRIPTION)); 

    Log.e("TAG", "terms_size: "+data.toString()); 

    return itemView; 
} 


} 
+2

' //你沒有顯示listview的適配器適配器代碼,只有1種方法。 –

+1

分享你的整個listview'類的適配器與問題 –

+0

檢查更新的代碼 –

回答

0

你可以嘗試像波紋管

  1. 從服務器獲取列表

    List<CamaraModel> listValue= new ArrayList<>(); 
    
        Array askArray = response.getJSONArray("data"); 
        if (askArray.length() > 0) { 
    
    
         for (int i = 0; i < askArray.length(); i++) { 
         JSONObject cameraObject = askArray.getJSONObject(i); 
         //Log.e("TAG", "onResponse: "+cameraObject.getString("description")); 
         CamaraModel model = new CamaraModel(); 
         model.setDescription(cameraObject.getString("description")); 
         listValue.add(model); 
        } 
    } 
    
    CustomListViewAdapter adapter = new CustomListViewAdapter(AccountBalanceInfo.this, listValue); 
    
    listView.setAdapter(adapter); 
    
  2. 您應該使用模型像

    public class CamaraModel { 
    private String description; 
    
    public String getDescription() { 
    return description; 
    } 
    
        public void setDescription(String description) { 
        this.description = description; 
        } 
    } 
    
  3. 創建佈局XML名稱test_row.xml &貼

    <?xml version="1.0" encoding="utf-8"?> 
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 
    
        <TextView 
         android:id="@+id/tv_descripton" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"   
        android:textStyle="bold" 
        android:paddingLeft="2dp" 
        android:textSize="16sp" 
        android:paddingBottom="5dp" 
        android:text="description" 
        android:paddingTop="5dp"/>  
    
        </LinearLayout> 
    
  4. 最後,您使用的適配器像

     public class CustomListViewAdapter extends ArrayAdapter<CamaraModel> { 
    
         // View lookup cache 
    
        private class ViewHolder { 
    
         TextView description; 
    
    
        } 
    
    
    
        public CustomListViewAdapter(Context context, List<CamaraModel> loandAmomount) { 
    
         super(context, R.layout.test_row, loandAmomount); 
    
        } 
    
    
    
    @Override 
    
    public View getView(int position, View convertView, ViewGroup parent) { 
    
        // Get the data item for this position 
    
        CamaraModel model = getItem(position); 
    
        CustomListViewAdapter.ViewHolder viewHolder; // view lookup cache stored in tag 
    
        if (convertView == null) { 
    
         viewHolder = new CustomListViewAdapter.ViewHolder(); 
    
         LayoutInflater inflater = LayoutInflater.from(getContext()); 
    
         convertView = inflater.inflate(R.layout.test_row, parent, false); 
    
         viewHolder.description = (TextView) convertView.findViewById(R.id.description); 
    
    
         convertView.setTag(viewHolder); 
    
        } else { 
    
         viewHolder = (CustomListViewAdapter.ViewHolder) convertView.getTag(); 
    
        } 
    
        // Populate the data into the template view using the data object 
    
        viewHolder.description.setText(model.getDescription()); 
    
    
        // Return the completed view to render on screen 
    
        if (position%2== 0) { 
         convertView.setBackgroundColor(Color.parseColor("#F5EEE5")); 
        }else{ 
    
         convertView.setBackgroundColor(Color.parseColor("#FFFFFF")); 
        } 
    
        return convertView; 
    
        } 
    
    } 
    
相關問題