简体   繁体   English

如何将数据从arraylist设置为listview

[英]How to set data from arraylist to listview

This is my adapter class: 这是我的适配器类:

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;
}
}

This is my Activity: 这是我的活动:

 @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;
}


}

I want to set the values from my List to text view (which is a listiview). 我想将“列表”中的值设置为文本视图(即listiview)。 but when i set it it is showing only first data in the listview. 但是当我设置它时,它仅在列表视图中显示第一个数据。 i am not understanding what is going wrong ,how to fetch all the data from List and set it to listview. 我不明白这是怎么回事,如何从列表中获取所有数据并将其设置为列表视图。 please help me to solve this.. 请帮我解决这个问题。

I guess you are getting the same value each time. 我想您每次都获得相同的价值。 Get value for the right position in your adapter. 为您的适配器中的正确位置获取价值。 Something like this: 像这样:

title1.setText(aList.get(position).get("Title"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM