简体   繁体   中英

How to add string builder data to listview in android?

I am getting data from PHP server and i read it as string builder. Now i want to add it to listview . How can i do it? Here is my php data. I want to display it in Listview

private String convertStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        try {
            while ((line = rd.readLine()) != null) {

                total.append(line+"\n");
                    }
        } catch (Exception e) {
            Log.v("sat", ""+e);
        }

And result is:

["A","B","C","D","E","F","G"]

Now How can i add this data to listview?

If ["A","B","C","D","E","F","G"] is of String[] values

then use simple list view adapter

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1, android.R.id.<textviewname>, values);

    // Assign adapter to ListView
     listView.setAdapter(adapter); 

Simple array adapter try this:

ArrayList<String>arr_list= new ArrayList<String>();
String str=strbuilder.toString();       

String[] str1=str.split(",");
for(int i=0;i<str1.length;i++){

    arr_list.add(str1[i]);
}

System.out.println("values=="+str1);
System.out.println("arr_list=="+arr_list);


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                      android.R.layout.simple_list_item_1,  arr_list);

            // Assign adapter to ListView
             listView.setAdapter(adapter); 

Here strbuilder is your StringBuilder.

convertStreamToStringArrayList method...

  public ArrayList<String> convertStreamToString(InputStream is) {
                ArrayList<String> data=new ArrayList<String>();
                String line;
                BufferedReader info = new BufferedReader(new InputStreamReader(is));
                try {
                    while ((line = info.readLine()) != null) {

                            data.add(line);
                            }
                    return data;
                } catch (Exception e) {
                    Log.v("sat", ""+e);
                }
                return null;
               }

and you can use this method like:

 listView.setAdapter(new ArrayAdapter<String>(this,
               android.R.layout.simple_list_item_1,  convertStreamToString(YourStream)));

Or

ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,convertStreamToString(YourStream));

listView.setAdapter(adapter);

or

ArrayList<String> data=convertStreamToString(YourStream);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
listView.setAdapter(adapter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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