我正在创建一个使用本地json文件显示某些信息的应用程序。所以我解析了数据和显示数据的文本视图。但我真正想要的是解析后的数据必须显示在{ {1}}。怎么做?
这是我的代码。
Listview
这是我的customList适配器
// Reading text file from assets folder
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(
"jsonshoutdata.txt")));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); // stop reading
} catch (IOException e) {
e.printStackTrace();
}
}
myjsonstring = sb.toString();
// Try to parse JSON
try {
urlist = new ArrayList<HashMap<String, String>>();
// Creating JSONObject from String
JSONObject jsonObjMain = new JSONObject(myjsonstring);
// Creating JSONArray from JSONObject
JSONArray jsonArray = jsonObjMain.getJSONArray("message");
// JSONArray has four JSONObject
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
String message = jsonObj.getString("msg");
HashMap<String, String> map = new HashMap<String, String>();
map.put("msg", msg );
urlist.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是我的适配器类
CustomList adapter = new CustomList(MainActivity.this,urlist);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "TEST.........", Toast.LENGTH_SHORT).show();
}
});
答案 0 :(得分:4)
创建 customList 类(dont use hashmap)
更改自定义列表代码(这是您的代码)
public class CustomList extends ArrayAdapter<String>
{
public final ArrayList<HashMap<String, String>> urlist;
private Activity context;
public CustomList(Activity context,ArrayList<HashMap<String, String>> urlist)
{
super(context, R.layout.list_single);
this.context = context;
this.urlist=urlist;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
txtTitle.setText((CharSequence) urlist);
return rowView;
}
}
以强>
public class customList extends ArrayAdapter<String>{
Context context;
int layoutResourceId;
ArrayList<String> data = null;
public customList(Context context, int layoutResourceId, ArrayList<String> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CustomHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CustomHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (CustomHolder)row.getTag();
}
String s = data.get(postition);
holder.txtTitle.setText(s);
return row;
}
static class CustomHolder
{
TextView txtTitle;
}
}
并更改您的json解析代码并解析您的json,如
// JSONArray has four JSONObject
ArrayList<String> messages = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
String message = jsonObj.getString("msg");
messages.add(message);
}
并最终替换您的此代码
CustomList adapter = new CustomList(MainActivity.this,urlist);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "TEST.........", Toast.LENGTH_SHORT).show();
}
});
使用此
customList adapter = new customList(MainActivity.this,R.layout.list_single,messages); //messagses is your arraylist in which you added string by parsing your json (see before this code mean upward)
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "TEST.........", Toast.LENGTH_SHORT).show();
}
});
解决方案2
由于您只显示单个文字,因此您无需创建自己的自定义适配器,而只需执行此操作即可。
ArrayList<String> messages = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
String message = jsonObj.getString("msg");
messages.add(message);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, messages);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "TEST.........", Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
使用此功能。
try {
urlist = new ArrayList<HashMap<String, String>>();
JSONObject jsonObjMain = new JSONObject(myjsonstring);
// Creating JSONArray from JSONObject
JSONArray jsonArray = jsonObjMain.getJSONArray("message");
// JSONArray has four JSONObject
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
String message = jsonObj.getString("msg");
HashMap<String, String> map = new HashMap<String, String>();
map.put("msg", message );
urlist.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这里全局在onCreate()
之前声明 ArrayList<HashMap<String, String>> urlist;
现在,您必须将urList
arraylist作为参数传递到CustomAdapter
ListView
,然后将该适配器设置为ListView
。