我在android中创建一个列表。那就是我从服务器获取列表并将其添加到ArrayList。之后,我创建了一个适配器来显示列表,但问题是我在列表中有两个值,但ListView只显示了一个项目。我检查了很多。我检查了适配器。它共有2个项目,但只显示1.为什么会出现这个问题。请帮忙。
//从服务器提取列表
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);
}
// listview的适配器
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;
}
}
答案 0 :(得分:0)
您可以尝试下面的
从服务器提取列表
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);
您应该使用类似
的模型public class CamaraModel {
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
创建布局xml名称test_row.xml&amp;粘贴
<?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>
最后你使用像
这样的适配器 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;
}
}