Java 如何将JSONArray转换为ListView?
我有一个代码,它包含以下内容:Java 如何将JSONArray转换为ListView?,java,android,json,android-arrayadapter,Java,Android,Json,Android Arrayadapter,我有一个代码,它包含以下内容: 通过HttpClient到PHP文件连接到web服务 返回SQL查询的结果 返回格式为jArray(JSONArray) for(int i=0;i
for(int i=0;i
当我查看LogCat时,我看到了查询的所有“名称”,每个记录都被打印出来。我只需要将这些结果插入ListView。我怎样才能做到这一点
PS-对于ArrayAdapter,我没有单独的类。这可能是原因吗 将数据放入数组,并使用ArrayAdapter将数据绑定到列表项 见以下文章:
你可以看看这个网页,它有一个例子来做你正在寻找的事情: 基本上,在打印名称的for循环中,应该加载一个数组列表,其中包含稍后要插入到列表中的值。 请注意,该示例使用了以下内容:
ArrayList>mylist=newarraylist>()代码>
您有一个整数和一个字符串要添加到结构中,所以您可以简单地将该ID转换为字符串并解决问题。
之后,您可以使用列表适配器,而无需创建单独的类:
ListAdapter=newsimpledapter(this,mylist,R.layout.main,新字符串[]{“name”,“id”},新int[]{R.id.item_title,R.id.item_subtitle})代码>
其中“name”和“id”将是json和item_title返回的name和id值在地图中的键,item_副标题为视图添加“adapt”文本
希望我说得够清楚,看看这个例子吧,反正它很简单。如果您只想显示文本视图列表,而不需要覆盖任何内容,您可以将所有项目添加到arrayList中,然后使用arrayAdapter
在xml中放置一个名为android:list的列表视图,然后使用要使用的textView创建arrayAdapter
之后,您所要做的就是调用setListAdapter(mArrayAdapter),它应该填充您的列表
ArrayList<String> items = new ArrayList<String>();
for(int i=0; i < jArray.length() ; i++) {
json_data = jArray.getJSONObject(i);
int id=json_data.getInt("id");
String name=json_data.getString("name");
items.add(name);
Log.d(name,"Output");
}
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1, items));
setListAdapter(mArrayAdapter)
ArrayList items=new ArrayList();
for(int i=0;i
希望这有帮助 我不喜欢在JSON数组中重新映射数据
这是我的代码。。。这对我来说很简单……希望能有所帮助
package ...;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class DataListFragment extends ListFragment{
JSONArray data;
public DataListFragment(){
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i("mycode", "DataListFragment onActivityCreated");
data=((MainActivity)this.getActivity()).data;
Log.i("mycode", "data length "+data.toString());
setEmptyText("No Data Here");
final JSONArrayAdapter adapter = new JSONArrayAdapter(this.getActivity(),data);
setListAdapter(adapter);
// Start out with a progress indicator.
//setListShown(false);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Insert desired behavior here.
Log.i("mycode", "Item clicked: " + id);
}
private class JSONArrayAdapter extends BaseAdapter {
JSONArray data;
Context context;
public JSONArrayAdapter(Context context,JSONArray data) {
super();
this.context=context;
this.data=data;
}
@Override
public int getCount() {
return data.length();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
try {
return data.getJSONObject(arg0);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public boolean hasStableIds(){
return true;
}
@Override
public boolean isEmpty(){
return data==null || data.length()==0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.single_item, parent, false);
TextView textView1 = (TextView) rowView.findViewById(R.id.firstLine);
TextView textView2 = (TextView) rowView.findViewById(R.id.secondLine);
//ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
try {
JSONObject jo = (JSONObject) data.get(position);
textView1.setText(jo.getString("category_title"));
textView2.setText(jo.getString("description"));
} catch (JSONException e) {
e.printStackTrace();
}
return rowView;
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="icon"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="12sp" />
<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:textSize="16sp" />
</RelativeLayout>
包。。。;
导入android.content.Context;
导入android.os.Bundle;
导入android.support.v4.app.ListFragment;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.ListView;
导入android.widget.TextView;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
公共类DataListFragment扩展了ListFragment{
JSONArray数据;
公共DataListFragment(){
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Log.i(“mycode”、“DataListFragment onActivityCreated”);
数据=((MainActivity)this.getActivity()).data;
Log.i(“mycode”、“数据长度”+data.toString());
setEmptyText(“此处无数据”);
最终JSONArrayAdapter=新的JSONArrayAdapter(this.getActivity(),data);
setListAdapter(适配器);
//从进度指示器开始。
//SetListShowed(假);
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
//在此处插入所需的行为。
Log.i(“mycode”,“单击的项目:”+id);
}
私有类JSONArrayAdapter扩展BaseAdapter{
JSONArray数据;
语境;
公共JSONArrayAdapter(上下文上下文,JSONArray数据){
超级();
this.context=context;
这个。数据=数据;
}
@凌驾
public int getCount(){
返回data.length();
}
@凌驾
公共对象getItem(int arg0){
//TODO自动生成的方法存根
试一试{
返回data.getJSONObject(arg0);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
@凌驾
公共长getItemId(int arg0){
返回arg0;
}
@凌驾
公共布尔表ID(){
返回true;
}
@凌驾
公共布尔值为空(){
返回数据==null | | data.length()==0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
视图行视图=充气机。充气(R.layout.single_项目,父项,false);
TextView textView1=(TextView)rowView.findViewById(R.id.firstLine);
TextView textView2=(TextView)rowView.findViewById(R.id.secondLine);
//ImageView ImageView=(ImageView)rowView.findViewById(R.id.icon);
试一试{
JSONObject jo=(JSONObject)data.get(position);
textView1.setText(jo.getString(“category_title”);
textView2.setText(jo.getString(“description”);
}捕获(JSONEx)
package ...;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class DataListFragment extends ListFragment{
JSONArray data;
public DataListFragment(){
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i("mycode", "DataListFragment onActivityCreated");
data=((MainActivity)this.getActivity()).data;
Log.i("mycode", "data length "+data.toString());
setEmptyText("No Data Here");
final JSONArrayAdapter adapter = new JSONArrayAdapter(this.getActivity(),data);
setListAdapter(adapter);
// Start out with a progress indicator.
//setListShown(false);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Insert desired behavior here.
Log.i("mycode", "Item clicked: " + id);
}
private class JSONArrayAdapter extends BaseAdapter {
JSONArray data;
Context context;
public JSONArrayAdapter(Context context,JSONArray data) {
super();
this.context=context;
this.data=data;
}
@Override
public int getCount() {
return data.length();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
try {
return data.getJSONObject(arg0);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public boolean hasStableIds(){
return true;
}
@Override
public boolean isEmpty(){
return data==null || data.length()==0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.single_item, parent, false);
TextView textView1 = (TextView) rowView.findViewById(R.id.firstLine);
TextView textView2 = (TextView) rowView.findViewById(R.id.secondLine);
//ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
try {
JSONObject jo = (JSONObject) data.get(position);
textView1.setText(jo.getString("category_title"));
textView2.setText(jo.getString("description"));
} catch (JSONException e) {
e.printStackTrace();
}
return rowView;
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="icon"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="12sp" />
<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:textSize="16sp" />
</RelativeLayout>