关闭

Json解析网络数据+listview展示跳转

标签: androidjson
268人阅读 评论(0) 收藏 举报
分类:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tv_recommend"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_weight="1"
                android:text="推荐"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/tv_history"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_weight="1"
                android:text="野史"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/tv_seraglio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_weight="1"
                android:text="后宫"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/tv_decode"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_weight="1"
                android:text="解密"
                android:textSize="20sp" />
        </LinearLayout>
    </HorizontalScrollView>

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>

</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp" />

    <LinearLayout
        android:id="@+id/sourceandtime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/source"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/sourceandtime"
        android:textSize="18sp" />

</LinearLayout>
<?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="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:textSize="22sp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

</LinearLayout>
<?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="match_parent"
    android:orientation="vertical" >
    <ListView 
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ListView>

</LinearLayout>
public class Data {
    public List<News> array;
    public class News implements Serializable{

        public String title;
        public String content;
        public String type;
        public String source;
        public String time;

    }
}
public abstract class BaseFragment extends Fragment {

    private ListView lv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.base_fragment, null);
        lv = (ListView) view.findViewById(R.id.lv);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String path = "http://172.17.29.120/localuser/ljy/xuanxuan/datas.json";
        MyAsyncTask asyncTask = new MyAsyncTask();
        // 请求网络数据
        asyncTask.execute(path);
    }

    // 自定义异步类
    class MyAsyncTask extends AsyncTask<String, String, Data> {

        @Override
        protected Data doInBackground(String... params) {
            Data data = getData(params[0]);
            return data;
        }

        private Data getData(String string) {
            try {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet get = new HttpGet(string);
                System.out.println("2=========");
                HttpResponse response = client.execute(get);
                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity entity = response.getEntity();
                    InputStream is = entity.getContent();
                    InputStreamReader isr = new InputStreamReader(is);

                    Gson gson = new Gson();
                    Data data = gson.fromJson(isr, Data.class);
                    System.out.println("data===" + data.toString());
                    System.out.println(data.array.get(0).time
                            + data.array.get(0).title + data.array.get(0).type);
                    return data;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Data data) {
            super.onPostExecute(data);

            List<News> array = data.array;
            final List<News> list = new ArrayList<Data.News>();
            int type = getType();
            switch (type) {
            case 0:
                list.clear();
                for (News news : array) {
                    if (news.type.equals("推荐")) {
                        list.add(news);
                    }
                }
                break;
            case 1:
                list.clear();
                for (News news : array) {
                    if (news.type.equals("野史")) {
                        list.add(news);
                    }
                }
                break;
            case 2:
                list.clear();
                for (News news : array) {
                    if (news.type.equals("后宫")) {
                        list.add(news);
                    }
                }
                break;
            case 3:
                list.clear();
                for (News news : array) {
                    if (news.type.equals("解密")) {
                        list.add(news);
                    }
                }
                break;

            default:
                break;
            }
            lv.setAdapter(new MyAdapter(getActivity(), list));
            //为listview设置监听
            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    Intent intent=new Intent(getActivity(),DetailActivity.class);
                    intent.putExtra("news", list.get(arg2));
                    startActivity(intent);
                }

            });
        }

    }

    public abstract int getType();
}
public class MyFragment extends BaseFragment{

    private int type;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type=getArguments().getInt("type",0);
    }
    public static Fragment getInstance(int type){
        MyFragment frag = new MyFragment();
        Bundle bd = new Bundle();
        bd.putInt("type", type);
        frag.setArguments(bd);
        return frag;
    }
    @Override
    public int getType() {
        return type;
    }



}
public class MyAdapter extends BaseAdapter {
    Context context;
    List<News> list;



    public MyAdapter(Context context, List<News> list) {
        super();
        this.context = context;
        this.list = list;
    }

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

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            holder=new ViewHolder();
            convertView=View.inflate(context,R.layout.lv_item,null);
            TextView title = (TextView) convertView.findViewById(R.id.title);
            holder.title=title;
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setText(list.get(position).title);
        return convertView;
    }

    class ViewHolder{
        TextView title;
    }
}
public class MainActivity extends FragmentActivity implements OnClickListener {

    private ViewPager vp;
    private TextView tv_recommend;
    private TextView tv_history;
    private TextView tv_seraglio;
    private TextView tv_decode;
    private List<TextView> tvList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vp = (ViewPager) findViewById(R.id.vp_main);
        tv_recommend = (TextView) findViewById(R.id.tv_recommend);
        tv_history = (TextView) findViewById(R.id.tv_history);
        tv_seraglio = (TextView) findViewById(R.id.tv_seraglio);
        tv_decode = (TextView) findViewById(R.id.tv_decode);

        tvList = new ArrayList<TextView>();
        tv_recommend.setTextColor(Color.RED);
        tvList.add(tv_recommend);
        tvList.add(tv_history);
        tvList.add(tv_seraglio);
        tvList.add(tv_decode);
        for (TextView tv : tvList) {
            tv.setOnClickListener(this);
        }
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return 4;
            }

            @Override
            public Fragment getItem(int arg0) {
                Fragment fragment = MyFragment.getInstance(arg0);
                return fragment;
            }

        });
        vp.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                for (int i = 0; i < tvList.size(); i++) {
                    if (arg0==i) {
                        tvList.get(i).setTextColor(Color.RED);
                    }else{
                        tvList.get(i).setTextColor(Color.BLUE);
                    }
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.tv_recommend:
            vp.setCurrentItem(0);
            break;
        case R.id.tv_history:
            vp.setCurrentItem(1);
            break;
        case R.id.tv_seraglio:
            vp.setCurrentItem(2);
            break;
        case R.id.tv_decode:
            vp.setCurrentItem(3);
            break;

        }
    }
}
public class DetailActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        // 找控件

        TextView title = (TextView) findViewById(R.id.title);
        TextView source = (TextView) findViewById(R.id.source);
        TextView time = (TextView) findViewById(R.id.time);
        TextView content = (TextView) findViewById(R.id.content);

        // 得到意图对象
        Intent intent = getIntent();
        News news = (News) intent.getSerializableExtra("news");
        // 为控件赋值
        title.setText(news.title);
        source.setText(news.source);
        time.setText(news.time);
        content.setText(news.content);
    }

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4412次
    • 积分:204
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论