栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从此链接填充到android Listview的JSON

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从此链接填充到android Listview的JSON

继续上课:

public class MainActivity extends Activity {    private static String url="https://www.dropbox.com/s/rhk01nqlyj5gixl/jsonparsing.txt?dl=1";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //Create a JSON parser Instance ----- Used JSON parser from Android        JSonParser jParser=new JSonParser();        //Getting JSON string from URL ------ Used JSON Array from Android        JSonArray json=jParser.getJSonFromUrl(url);        List<WhateverObject> yourData = new ArrayList<WhateverObject>();        try { for(int i=0;i<json.length();i++) {     JSonObject c=json.getJSonObject(i);// Used JSON Object from Android     //Storing each Json in a string variable     int AGE=c.getInt("age");     String NAME=c.getString("name");     String CITY=c.getString("city");     String GENDER=c.getString("Gender");     String BIRTHDATE=c.getString("birthdate");     yourData.add(new WhateverObject(NAME, CITY, GENDER, BIRTHDATE)); }        } catch (JSonException e) { // TODO Auto-generated catch block e.printStackTrace();        }        ListView yourListView = (ListView) findViewById(R.id.itemListView);        ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, yourData);        yourListView.setAdapter(customAdapter);    }}

调整器:

public class ListAdapter extends ArrayAdapter<Item> {    public ListAdapter(Context context, int textViewResourceId) {        super(context, textViewResourceId);        // TODO Auto-generated constructor stub    }    private List<Item> items;    public ListAdapter(Context context, int resource, List<Item> items) {        super(context, resource, items);        this.items = items;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        View v = convertView;        TextView tt = null;        TextView tt1 = null;        TextView tt2 = null;        TextView tt3 = null;        TextView tt4 = null;        if (v == null) { LayoutInflater vi; vi = LayoutInflater.from(getContext()); v = vi.inflate(R.layout.itemlistrow, null); tt = (TextView) v.findViewById(R.id.age); tt1 = (TextView) v.findViewById(R.id.name); tt2 = (TextView) v.findViewById(R.id.city); tt3 = (TextView) v.findViewById(R.id.gender); tt4 = (TextView) v.findViewById(R.id.birthdate);        }        Item p = items.get(position);        if (p != null) { if (tt != null) {     tt.setText(""+p.getAge()); } if (tt1 != null) {     tt1.setText(""+p.getName()); } if (tt2 != null) {     tt2.setText(""+p.getCity()); } if (tt3 != null) {     tt3.setText(""+p.getGender()); } if (tt4 != null) {     tt4.setText(""+p.getBirthdate()); }        }        return v;    }}

对象保存数据:

public class WhateverObject{    private int age;    private String name;    private String city;    private String gender;    private String birthdate;    public WhateverObject(int age, String name, String city, String gender, String birthdate){        this.age = age;        this.name = name;        this.city = city;        this.gender = gender;        this.birthdate = birthdate;    }    public int getAge(){        return this.age;    }    public String getName(){        return this.name;    }    public String getCity(){        return this.city;    }    public String getGender(){        return this.gender;    }    public String getBirthdate(){        return this.birthdate;    }}

列表项的xml(保存在

itemlistrow
名称下):

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_height="wrap_content" android:orientation="vertical"  android:layout_width="fill_parent">    <TableRow android:layout_width="fill_parent"   android:id="@+id/TableRow01"   android:layout_height="wrap_content">        <TextView     android:id="@+id/age"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="age" android:textStyle="bold"     android:gravity="left"     android:layout_weight="1"     android:typeface="monospace"     android:height="40sp"/>    </TableRow>    <TableRow android:layout_height="wrap_content"   android:layout_width="fill_parent">        <TextView     android:id="@+id/name"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="name"     android:layout_weight="1"     android:height="20sp"/>    </TableRow>    <TableRow android:layout_height="wrap_content"   android:layout_width="fill_parent">        <TextView     android:id="@+id/city"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="city"     android:layout_weight="1"     android:height="20sp"/>    </TableRow>    <TableRow android:layout_height="wrap_content"   android:layout_width="fill_parent">        <TextView     android:id="@+id/gender"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="city"     android:layout_weight="1"     android:height="20sp"/>    </TableRow>    <TableRow android:layout_height="wrap_content"   android:layout_width="fill_parent">        <TextView     android:id="@+id/birthdate"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="city"     android:layout_weight="1"     android:height="20sp"/>    </TableRow></TableLayout>

比较简单的例子:

public class MainActivity extends Activity {    private static String url="https://www.dropbox.com/s/rhk01nqlyj5gixl/jsonparsing.txt?dl=1";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //Create a JSON parser Instance ----- Used JSON parser from Android        JSonParser jParser=new JSonParser();        //Getting JSON string from URL ------ Used JSON Array from Android        JSonArray json=jParser.getJSonFromUrl(url);        List<Map<String, String>> personList = new ArrayList<Map<String,String>>();        try { for(int i=0;i<json.length();i++) {     JSonObject c=json.getJSonObject(i);// Used JSON Object from Android     //Storing each Json in a string variable     int AGE=c.getInt("age");     String NAME=c.getString("name");     String CITY=c.getString("city");     String GENDER=c.getString("Gender");     String BIRTHDATE=c.getString("birthdate");     personList.add(createPerson(AGE, NAME, CITY, GENDER, BIRTHDATE)); }        } catch (JSonException e) { // TODO Auto-generated catch block e.printStackTrace();        }        ListView yourListView = (ListView) findViewById(R.id.itemListView);        simpleAdpt = new SimpleAdapter(this, personList, android.R.layout.simple_list_item_1, new String[] {"person"}, new int[] {android.R.id.text1});        yourListView.setAdapter(simpleAdpt);    }    private HashMap<String, String> createPerson(int age, String name, String city, String gender, String birthdate) {        HashMap<String, String> person = new HashMap<String, String>();        person.put("person", name+" | "+age + " | "+city + " | "+gender + " | "+birthdate);        return person;    }}

感谢http://www.javapregeeks.com/2013/06/android-listview-tutorial-and-basic-
example.html



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/616901.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号