//项目效果:
//设置ActionBar导航:
-----------------------------------------------------------------------------------------------------------
MainActivi.note
MainActivity:
public class MainActivity extends FragmentActivity implements TabListener {
private FragmentManager fm;
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar = getActionBar();
bar.setTitle("名师");
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.addTab(bar.newTab().setText("全部").setTabListener(this));
bar.addTab(bar.newTab().setText("考研").setTabListener(this));
bar.addTab(bar.newTab().setText("公务员").setTabListener(this));
bar.addTab(bar.newTab().setText("教师资格证").setTabListener(this));
bar.addTab(bar.newTab().setText("四六级").setTabListener(this));
bar.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
break;
}
return super.onMenuItemSelected(featureId, item);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction bt1 = fm.beginTransaction();
switch (tab.getPosition()) {
case 0:
bt1.replace(R.id.fl, new F1());
break;
case 1:
bt1.replace(R.id.fl, new F2());
break;
case 2:
bt1.replace(R.id.fl, new F3());
break;
case 3:
bt1.replace(R.id.fl, new F4());
break;
case 4:
bt1.replace(R.id.fl, new F5());
break;
default:
break;
}
bt1.commit();
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
解析对象:
public class News {
public String resultCode;
public String resultMsg;
public ArrayList<MyData> resultData;
public class MyData{
public ArrayList<MyList> course_list;
public String cover_url;
public String name;
public String type_id;
}
public class MyList{
public String couse_id;
public String img_url;
public String students;
public String teacher_id;
public String teacher_name;
public String title;
}
-----------------------------------------------------------------------------------------------------------
工具类:
public class StreamUtils {
public static String StreamToStr(InputStream inputStream) throws IOException{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte [] buffer =new byte[1024];
int len=0;
while((len=inputStream.read(buffer))!=-1){
outputStream.write(buffer, 0, len);
}
return outputStream.toString();
}
public static News analyze(Context context) {
// TODO Auto-generated method stub
try {
InputStream open = context.getAssets().open("index.json");
String json = StreamUtils.StreamToStr(open);
Gson gson = new Gson();
News news = gson.fromJson(json, News.class);
return news;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
-----------------------------------------------------------------------------------------------------------
————————适配器——————————
第一个:
public MyAdapter(FragmentActivity activity, List<MyList> ml) {
// TODO Auto-generated constructor stub
this.activity=activity;
this.ml=ml;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return ml.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return ml.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) {
// TODO Auto-generated method stub
View view = View.inflate(activity, R.layout.lv_item, null);
TextView tv_name= (TextView) view.findViewById(R.id.tv_name);
TextView tv_details= (TextView) view.findViewById(R.id.tv_details);
tv_name.setText(ml.get(position).teacher_name);
tv_details.setText(ml.get(position).title);
return view;
}
第二个:
public MytAdapter(FragmentActivity fragmentActivity, MyData myData) {
// TODO Auto-generated constructor stub
this.context=fragmentActivity;
this.myData=myData;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return myData.course_list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return myData.course_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) {
// TODO Auto-generated method stub
View view = View.inflate(context, R.layout.lv_item, null);
TextView name= (TextView) view.findViewById(R.id.tv_name);
TextView details= (TextView) view.findViewById(R.id.tv_details);
name.setText(myData.course_list.get(position).teacher_name);
details.setText(myData.course_list.get(position).title);
return view;
}
-----------------------------------------------------------------------------------------------------------
---------Fragment-------
全部:
public class F1 extends Fragment {
private List<MyList> ml;
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fl_item,container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
News news = StreamUtils.analyze(getActivity());
ml = new ArrayList<MyList>();
for (MyData md : news.resultData) {
for(MyList cl:md.course_list){
ml.add(cl);
}
}
ListView lv= (ListView) view.findViewById(R.id.lv);
lv.setAdapter(new MyAdapter(getActivity(),ml));
super.onActivityCreated(savedInstanceState);
}
}
分类:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
News news = StreamUtils.analyze(getActivity());
//根据下标获得对象
MyData myData = news.resultData.get(0);
ListView lv = (ListView) view.findViewById(R.id.lv);
lv.setAdapter(new MytAdapter(getActivity(), myData));
}
-----------------------------------------------------------------------------------------------------------