我从PHP服务器获取数据,并将其作为字符串构建器读取。现在我想将它添加到listview。我该怎么做?这是我的PHP数据。我想在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);
}
结果是:
["A","B","C","D","E","F","G"]
现在如何将此数据添加到listview?
答案 0 :(得分:0)
如果["A","B","C","D","E","F","G"]
属于String[] values
然后使用简单的列表视图适配器
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);
答案 1 :(得分:0)
简单的数组适配器试试这个:
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);
这里的strbuilder是你的StringBuilder。
答案 2 :(得分:0)
convertStreamToStringArrayList 方法...
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;
}
您可以使用以下方法:
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, convertStreamToString(YourStream)));
或强>
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,convertStreamToString(YourStream));
listView.setAdapter(adapter);
或强>
ArrayList<String> data=convertStreamToString(YourStream);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
listView.setAdapter(adapter);