Android开发之将json数据解析后填充到ListView

来源:互联网 发布:oracle数据库全库备份 编辑:程序博客网 时间:2024/06/05 18:37

Android开发之将json数据解析后填充到ListView是大多数移动开发人员会遇到的问题,这里就给记录一下我遇到时我是怎么做的。。

新建一个Test类,代码如下:

public class Test {

    public String getVegetable() {
        return Vegetable;
    }
    public void setVegetable(String vegetable) {
        Vegetable = vegetable;
    }

    public String getNumber() {
        return Number;
    }
    public void setNumber(String number) {
        Number = number;
    }
    private String Vegetable;
    private String Number;
    
}
再建一个类用来解析和填充数据代码如下:
public class TestActivity extends Activity {
    ListView myListView;
    // ArrayList list ;
    ArrayList<Test> list;
    ProgressBar progressBar;
    AllViewHolder holder;
    Test test;
    MyListViewAdapter adapter;
    private String josnData = "[{\"Vegetable\":\"dfsdsdf\",\"Number\":\"sfsdfsdf\"}," +
            "{\"Vegetable\":\"dfsdsdf\",\"Number\":\"sfsdfsdf\"}," +
            "{\"Vegetable\":\"dsgsdg\",\"Number\":\"sfsdfsdf\"}," +
            "{\"Vegetable\":\"gsgsdg\",\"Number\":\"sfsdfsdf\"}]";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar)findViewById(R.id.progess);
        // initData();
        refresh();
    }
    public void refresh(){
        try {
            Thread.sleep(5*1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        progressBar.setVisibility(View.GONE);
        praseFromJosn(josnData);
        myListView = (ListView) findViewById(R.id.listview);

        adapter = new MyListViewAdapter(TestActivity.this);
        myListView.setAdapter(adapter);
        //给ListView注册一个上下文菜单
        registerForContextMenu(myListView);
        
    }

    

    class AllViewHolder {
        TextView Vegetable;
        TextView Number;
    }

    class MyListViewAdapter extends BaseAdapter {
        private Context context;
        LayoutInflater myInflater;

        public MyListViewAdapter(Context context) {
            this.context = context;
            myInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            test = list.get(position);
            if (convertView == null) {
                convertView = myInflater.inflate(R.layout.activity_vegetable, null);
                holder = new AllViewHolder();

                holder.Number = (TextView) convertView
                        .findViewById(R.id.Number);
                holder.Vegetable = (TextView) convertView.findViewById(R.id.Vegetable);
                
                convertView.setTag(holder);

            } else {

                holder = (AllViewHolder) convertView.getTag();
            }
            holder.Number.setText(test.getNumber());
            holder.Vegetable.setText(test.getVegetable());
            return convertView;
        }

    }

    public List praseFromJosn(String jsonData) {
        Type ListType = new TypeToken<ArrayList<Test>>() {
        }.getType();
        Gson gson = new Gson();
        list = gson.fromJson(jsonData, ListType);

        return list;

    }
}

当然,xml文件分为两部分,一部分有listview,另一部分是填充到listview的item,代码如下:

main_activity.xml代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
      <ProgressBar
        android:id="@+id/progess"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
       <ListView
           android:id="@+id/listview"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"></ListView>
</LinearLayout>

activity_vegetable.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#eeeeee" >

        <ImageView
            android:layout_width="70dp"
            android:layout_height="55dp"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:background="#8bc34a"/>

        <RelativeLayout
            android:layout_width="120dp"
            android:layout_height="55dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp">

            <TextView
                android:id="@+id/Vegetable"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="菜名菜名菜名"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/Price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="5dp"
                android:text="¥9.5"
                android:textSize="16sp" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            >
            <TextView
            android:id="@+id/Number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="10dp"
            android:text="x1"
            android:textSize="16sp" />

        </RelativeLayout>

        

    </LinearLayout>

</LinearLayout>

当然,解析数据是要导入Gson的jar包,,,,

0 0
原创粉丝点击