0
這是我的適配器類:如何從ArrayList中的數據集列表視圖
public class MyAdapter extends ArrayAdapter {
private Context context;
private LayoutInflater inflater;
private List<HashMap<String, String>> aList=new ArrayList<HashMap<String,String>>();
int[] to;
public MyAdapter(Context context, List<HashMap<String, String>> aList) {
super(context, R.layout.layoutarray, aList);
this.context = context;
this.aList = aList;
inflater = LayoutInflater.from(context);
}
HashMap<String, String> m;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = inflater.inflate(R.layout.layoutarray, parent, false);
}
TextView title1=(TextView)convertView.findViewById(R.id.posttitle);
TextView description1=(TextView)convertView.findViewById(R.id.postdesc);
//TextView image1=(ImageView)rootView.findViewById(R.id.postimage);
TextView date=(TextView)convertView.findViewById(R.id.postdate);
ImageView image1=(ImageView)convertView.findViewById(R.id.postimage);
title1.setText(aList.get(aList.get(0).get("Title")));
// Picasso.with(context).load(m.get("Image")).into(image1);
return convertView;
}
}
這是我的活動:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
final View rootView = inflater.inflate(R.layout.aa, container, false);
Firebase.setAndroidContext(getActivity());
Firebase ref = new Firebase(FIREBASE_URL);
final List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
final ListView listView = (ListView) rootView.findViewById(R.id.listView);
final MyAdapter adapter = new MyAdapter(getActivity(),aList);
listView.setAdapter(adapter);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
for (DataSnapshot single : child.getChildren()) {
Map<String, Object> map = (Map<String, Object>) single.getValue();
namerc = (String) map.get("Namerc");
image = (String) map.get("Imagerc");
description = (String) map.get("Description");
title=(String) map.get("Title");
if (namerc!=null && description!=null && title!=null) {
String[] title1={title};
String[] desc1={description};
String[] name1={namerc};
String[] image1={image};
Toast.makeText(getActivity(), description, Toast.LENGTH_SHORT).show();
HashMap<String,String>data=new HashMap<String, String>();
for (int i=0; i<title1.length;i++) {
data.put("Title", title1[i]);
data.put("Desc",desc1[i]);
data.put("Name",name1[i]);
data.put("Image",image1[i]);
}
aList.add(0,data);
adapter.notifyDataSetChanged();
}
}
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
return rootView;
}
}
我想從我的列表文本視圖設置的值(其是一個listiview)。 但當我設置它只顯示列表視圖中的第一個數據。我不明白髮生了什麼問題,如何從列表中獲取所有數據並將其設置爲列表視圖。 請幫我解決這個問題..
可能是你的rootview您正在膨脹具有高度match_parent而非WRAP_CONTENT – Manifest
使用'title1.setText(aList.get(aList.get(位置)獲得( 「標題」)) );'。建議你切換到recyclerview。 – Raghunandan
Thanks Ragunadhan它解決了我的問題,只是編輯yur這樣回答。 title1.setText(aList.get(位置)獲得( 「標題」)); –