[android]代码库
private GridView mGridView1,mGridView2;
private ListView mLlistView3;
private Button mApplication,mGame,mSubject;
private ApplicationAdapter mApplicationAdapter;
private GameAdapter mGameAdapter;
private SubjectAdapter mSubjectAdapter;
private List<Sorts> mAppList;
private List<TopIcs> mTopIcsList;
private Context mContext;
private ProgressBar mProgressBar;
private int selectBtn;
private final int APPBTN=1;
private final int GAME=2;
private final int SUBJECT=3;
String mApplicationName,mGameName,mSubjectName;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.category, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
mContext=getView().getContext();
initView();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.application:
getAppData(DataUrlKeys.APPLICATION_URL,"{}");
mLlistView3.setVisibility(View.GONE);
mGridView1.setVisibility(View.VISIBLE);
selectBtn=APPBTN;
break;
case R.id.game:
getAppData(DataUrlKeys.GAME_URL,"{}");
mGridView1.setVisibility(View.VISIBLE);
mLlistView3.setVisibility(View.GONE);
selectBtn=GAME;
break;
case R.id.subject:
getSubjectData(DataUrlKeys.TOPICLIST,"{\"index\":0,\"location\":\"homepage\",\"num\":4}");
mGridView1.setVisibility(View.GONE);
mLlistView3.setVisibility(View.VISIBLE);
selectBtn=SUBJECT;
break;
}
}
public void initView(){
mGridView1=(GridView) getView().findViewById(R.id.application_gridview);
mLlistView3= (ListView) getView().findViewById(R.id.subject_ListView);
mProgressBar=(ProgressBar) getView().findViewById(R.id.category_Progress);
mApplication=(Button)getView(). findViewById(R.id.application);
mGame=(Button) getView().findViewById(R.id.game);
mSubject=(Button) getView().findViewById(R.id.subject);
selectBtn=APPBTN;
mApplication.setOnClickListener(this);
mApplicationName=(String) mApplication.getText();
mGame.setOnClickListener(this);
mGameName=(String) mGame.getText();
mSubject.setOnClickListener(this);
mSubjectName=(String) mSubject.getText();
//mApplicationName=(String) mApplication.getText();
//
getAppData(DataUrlKeys.APPLICATION_URL,"{}");
mGridView1.setOnItemClickListener(gridviewListener);
mLlistView3.setOnItemClickListener(listviewListener);
}
/**
*
* 方法说明:判断点击的是那个大标签
*
* @param selectBtn
* @return
*/
private String getSelectBtn(int selectBtn){
String name="";
switch (selectBtn) {
case APPBTN:
name="应用";
break;
case GAME:
name="游戏";
break;
case SUBJECT:
name="专题";
break;
}
return name;
}
private void getAppData(String url,String params) {
mProgressBar.setVisibility(View.VISIBLE);
if(NetWorkUtil.isNetwork(mContext)){
NetWorkUtil.getJsonData(getView().getContext(), url, params,new LoadJson() {
@Override
public void getJson(String data) {
String jsonData = data;
if(!jsonData.equals("")){
// System.out.println("json: "+jsonData);
mApplicationAdapter=null;
mAppList= getListDataFromJsonData(jsonData);
// System.out.println("applist: "+mAppList.toString());
if(mAppList!=null){
mApplicationAdapter=new ApplicationAdapter(mContext, mAppList);
mGridView1.setAdapter(mApplicationAdapter);
mProgressBar.setVisibility(View.GONE);
}else{
}
}else{
System.out.println("没有数据");
}
}
});
}else{
System.out.println("没有网络连接");
}
}
private void getSubjectData(String url,String params) {
mProgressBar.setVisibility(View.VISIBLE);
if(NetWorkUtil.isNetwork(mContext)){
NetWorkUtil.getJsonData(getView().getContext(), url, params,new LoadJson() {
@Override
public void getJson(String data) {
String jsonData = data;
if(!jsonData.equals("")){
// System.out.println("json: "+jsonData);
mSubjectAdapter=null;
mTopIcsList= getSubjectListDataFromJsonData(jsonData);
System.out.println("mTopIcsList: "+mTopIcsList.toString());
if(mTopIcsList!=null){
mSubjectAdapter=new SubjectAdapter(mContext, mTopIcsList);
mLlistView3.setAdapter(mSubjectAdapter);
mProgressBar.setVisibility(View.GONE);
}else{
}
}else{
System.out.println("没有数据");
}
}
});
}else{
System.out.println("没有网络连接");
}
}
private List<Sorts> getListDataFromJsonData(String jsonData){
Category cate=new Category();
List<Sorts> app=null;
Gson gson = new Gson();
cate = gson.fromJson(jsonData, Category.class);
app=cate.getmSorts();
return app;
}
private List<TopIcs> getSubjectListDataFromJsonData(String jsonData){
TopApp top=new TopApp();
List<TopIcs> sub=null;
Gson gson = new Gson();
top = gson.fromJson(jsonData, TopApp.class);
sub=top.getTopics();
return sub;
}
private OnItemClickListener gridviewListener=new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent=new Intent(getActivity(),CategoryApplicationActivity.class);
intent.putExtra("id", mAppList.get(arg2).getAssort_id());
intent.putExtra("idName", "assort_id");
String titleTag=getSelectBtn(selectBtn);
intent.putExtra("titleName", titleTag+">"+mAppList.get(arg2).getAssort_name());
startActivity(intent);
}
};
private OnItemClickListener listviewListener=new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent=new Intent(getActivity(),CategoryApplicationActivity.class);
intent.putExtra("id", mTopIcsList.get(arg2).getTopic_id());
intent.putExtra("idName", "topic_id");
String titleTag=getSelectBtn(selectBtn);
intent.putExtra("titleName", titleTag+">"+mTopIcsList.get(arg2).getTitle());
startActivity(intent);
}
};