主页面MainActivity
package com.example.yuekao_demo03;
import com.example.fragment.Ont_MyFragment;
import com.example.fragment.Three_MyFragment;
import com.example.fragment.Two_MyFragment;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements OnClickListener {
private Button b1,b2,b3,clear;
private Ont_MyFragment one;
private Two_MyFragment two;
private Three_MyFragment three;
FragmentManager fm;
FragmentTransaction ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button();
fragment();
}
private void button() {
// TODO Auto-generated method stub
b1 = (Button) findViewById(R.id.one_button);
b2 = (Button) findViewById(R.id.two_button);
b3 = (Button) findViewById(R.id.three_button);
clear = (Button) findViewById(R.id.clear_button);
}
private void fragment() {
// TODO Auto-generated method stub
one = new Ont_MyFragment();
two = new Two_MyFragment();
three = new Three_MyFragment();
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
ft.add(R.id.fragment, one);
ft.add(R.id.fragment, two);
ft.add(R.id.fragment, three);
ft.hide(two).hide(three);
ft.commit();
b1 .setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
clear.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentTransaction ft =fm.beginTransaction();
switch (v.getId()) {
case R.id.one_button:
ft.show(one);
ft.hide(two);
ft.hide(three);
b1.setBackgroundColor(Color.WHITE);
b2.setBackgroundColor(Color.BLACK);
b3.setBackgroundColor(Color.BLACK);
break;
case R.id.two_button:
ft.show(two);
ft.hide(one);
ft.hide(three);
b1.setBackgroundColor(Color.BLACK);
b2.setBackgroundColor(Color.WHITE);
b3.setBackgroundColor(Color.BLACK);
break;
case R.id.three_button:
ft.show(three);
ft.hide(two);
ft.hide(one);
b1.setBackgroundColor(Color.BLACK);
b2.setBackgroundColor(Color.BLACK);
b3.setBackgroundColor(Color.WHITE);
break;
case R.id.clear_button:
DataDeleter.cleanInternalCache(MainActivity.this);
DataDeleter.cleanDatabases(MainActivity.this);
Toast.makeText(MainActivity.this, "清除缓存", 2000).show();
break;
}
}
}
fragment页面
package com.example.fragment;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.example.adapter.MyAdapter;
import com.example.bean.More;
import com.example.bean.MyBean;
import com.example.yuekao_demo03.R;
import com.google.gson.Gson;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
public class Ont_MyFragment extends Fragment{
private ListView listview;
private List<More> list;
private MyAdapter adapter;
private static MyBean bean;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_f1, null);
listview = (ListView) v.findViewById(R.id.f1);
init();
return v;
}
private void init() {
HttpUtils hu = new HttpUtils();
String url = "http://172.22.37.80:8080/1402c/kebiao.json";
hu.configTimeout(5000);
hu.send(HttpMethod.GET, url,
new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
list = new ArrayList<More>();
Gson g = new Gson();
bean = g.fromJson(arg0.result, MyBean.class);
list = bean.getData().get(0).getMore();
Log.e("aaa",".................. "+list.toString());
adapter = new MyAdapter(list, getActivity());
listview.setAdapter(adapter);
add();
}
});
}
private void add() {
DbUtils db = DbUtils.create(getActivity());
for (int i = 0; i < list.size(); i++) {
More m = new More(list.get(i).getCategory_id(),list.get(i).getLesson_id(),
list.get(i).getLesson_images(),list.get(i).getLesson_number(),
list.get(i).getLesson_title(),list.get(i).getUser_name());
try {
db.save(m);
} catch (DbException e) {
e.printStackTrace();
}
}
}
}
adapter
package com.example.adapter;
import java.util.List;
import com.example.bean.More;
import com.example.yuekao_demo03.R;
import com.example.yuekao_demo03.R.id;
import com.lidroid.xutils.BitmapUtils;
import android.content.Context;
import android.content.pm.LabeledIntent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private List<More> list;
private Context context;
private LayoutInflater intent;
private BitmapUtils bu;
public MyAdapter(List<More> list, Context context) {
super();
this.list = list;
this.context = context;
intent = LayoutInflater.from(context);
bu = new BitmapUtils(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) {
Helper h;
if (convertView == null) {
h = new Helper();
convertView = intent.inflate(R.layout.activity_item, null);
h.img = (ImageView) convertView.findViewById(R.id.item_imageView1);
h.title = (TextView) convertView.findViewById(R.id.item_title);
h.name = (TextView) convertView.findViewById(R.id.item_nam);
convertView.setTag(h);
}else{
h = (Helper) convertView.getTag();
}
h.title.setText(list.get(position).getLesson_title());
h.name.setText(list.get(position).getUser_name());
bu.display(h.img, list.get(position).getLesson_images());
return convertView;
}
class Helper{
ImageView img;
TextView title,name;
}
}
还有一些事布局文件,这些我就不发了,实现的效果挺简单的,适合超级小白使用。也洗完大神们多多教教我们这些初学者!
还有两个jar包 xUtils-2.6.14.jar ----------------- gson-2.3.1.jar 这俩jar包 就自己 下载好了!