收藏列表实例【ViewSwitcher+Thread+JSONObject

系统 1868 0

 

 

layout_favorite_list.xml

 


收藏列表实例【ViewSwitcher+Thread+JSONObject ++handler+Adapter】
 

 


<? xml version = "1.0" encoding = "utf-8" ?>

< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"

    android:layout_width = "fill_parent"

    android:layout_height = "fill_parent"

    android:background = "@color/listcolor"

    android:orientation = "vertical" >

   

    < include layout = "@layout/header_bar" />

   

    < FrameLayout

        android:layout_width = "match_parent"

        android:layout_height = "match_parent"

        android:orientation = "vertical" >

 

        < ViewSwitcher

            android:id = "@+id/viewswitcher_news_top"

            android:layout_width = "match_parent"

            android:layout_height = "match_parent"

            android:layout_gravity = "center" >

        </ ViewSwitcher >

 

        < ListView

            android:id = "@+id/favorite_list"

            android:layout_width = "match_parent"

            android:layout_height = "match_parent"

            android:cacheColorHint = "@color/transparent"

            android:divider = "@drawable/tip_line" />

    </ FrameLayout >

 

</ LinearLayout >

 

 

 

 

FavoriteActivityList.java

 

 

 

package com.cps.media.ui.favorite;

 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

 

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;

import org.json.JSONObject;

 

import com.cps.media.R;

import com.cps.media.model.ExhibitorItem;

import com.cps.media.model.FavoriteItem;

import com.cps.media.model.NewsItem;

import com.cps.media.ui.PopupWindowImageButton;

import com.cps.media.ui.account.AccountManager;

import com.cps.media.ui.exhibitors.ExhibitorActivity;

import com.cps.media.ui.news.NewsContainerActivity;

import com.cps.media.util.CONST;

import com.cps.media.util.Util;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.os.Handler;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.BaseAdapter;

import android.widget.ImageButton;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.ViewSwitcher;

import android.widget.AdapterView.OnItemClickListener;

 

public class FavoriteActivityList extends Activity implements OnClickListener{

   

    public static String NEWS_TITLE = "title" ;

    public static String NEWS_TYPE = "type" ;

   

    private ListView mList ;

   

    private List<FavoriteItem> mFavoriteList ;

   

    private ItemAdapter mAdapter ;

 

    private ViewSwitcher viewSwitcher ;

 

    private ImageButton mBackImageButton ;

 

    private TextView mTitleTextView ;

   

    private int mMode ;

   

    private PopupWindowImageButton mMenuImageButton ;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       // TODO Auto-generated method stub

       super .onCreate(savedInstanceState);

       setContentView(R.layout. layout_favorite_list );

      

       mList = (ListView) findViewById(R.id. favorite_list );

      

       viewSwitcher = (ViewSwitcher) findViewById(R.id. viewswitcher_news_top );

       viewSwitcher .addView(getLayoutInflater().inflate(

              R.layout. layout_progress_page , null ));

 

       mBackImageButton = (ImageButton) findViewById(R.id. btn_newscontent_back );

       mBackImageButton .setOnClickListener( this );

 

       mMenuImageButton = (PopupWindowImageButton) findViewById(R.id. header_menu );

 

       mTitleTextView = (TextView) findViewById(R.id. new_header_title );

        mTitleTextView .setText(getIntent().getIntExtra( NEWS_TITLE , R.string. news_top_left_text ));

       mMode = getIntent().getIntExtra( NEWS_TYPE , Util. FAVORITE_NEWS );

       requestData();

    }

   

    @Override

    protected void onResume() {

       // TODO Auto-generated method stub

       super .onResume();

       mMenuImageButton .reload(Util. SHOW_FAVORITE );

    }

   

    private OnItemClickListener listener = new OnItemClickListener() {

 

       @Override

       public void onItemClick (AdapterView<?> parent, View view, int position,

              long id) {

           Intent intent = null ;

           if ( mMode == Util. FAVORITE_NEWS ){

              intent = new Intent(FavoriteActivityList. this ,

                     NewsContainerActivity. class );

              NewsItem item = new NewsItem();

              item.setId( mFavoriteList .get(position).getCid());

              item.setNewsType( mFavoriteList .get(position).getSrc_type());

              item.setTitle( mFavoriteList .get(position).getTitle());

              intent.putExtra( "news_item" , item);

              startActivityForResult(intent, 0);

           } else if ( mMode == Util. FAVORITE_EXHIBITOR ){

              intent = new Intent(FavoriteActivityList. this ,

                     ExhibitorActivity. class );

              ExhibitorItem item = new ExhibitorItem();

              item.setId( mFavoriteList .get(position).getCid());

              item.setName( mFavoriteList .get(position).getTitle());

              intent.putExtra( "exhibitor_item" , item);

              startActivityForResult(intent,0);

           }

       }

    };

   

    protected void onActivityResult( int requestCode, int resultCode, Intent data) {

       switch (requestCode) {

       case 0:

           requestData();

           break ;

 

       default :

           break ;

       }

    }

   

    private void requestData() {

       viewSwitcher .setVisibility(View. VISIBLE );

       viewSwitcher .showNext();

       Thread t = new Thread() {

           @Override

           public void run() {

              super .run();

              HttpClient client = new DefaultHttpClient();

              StringBuilder builder = new StringBuilder();

              int error = 1;

              JSONArray array = null ;

              HttpGet myget = null ;

              if (getIntent().getIntExtra( NEWS_TYPE , Util. FAVORITE_NEWS ) == Util. FAVORITE_NEWS ) {

                  myget = new HttpGet(CONST. URL_FAVORIT_LIST

                         .replace( "{0}" , "english" )

                         .replace( "{1}" , "favorList" )

                         .replace( "{2}" , Util. FAVORITE_NEWS + "" )

                         .replace(

                                "{3}" ,

                                AccountManager. getInstance ().getAccount()

                                       .getId()

                                       + "" ));

              } else {

                  myget = new HttpGet(CONST. URL_FAVORIT_LIST

                         .replace( "{0}" , "english" )

                         .replace( "{1}" , "favorList" )

                         .replace( "{2}" , Util. FAVORITE_EXHIBITOR + "" )

                         .replace(

                                "{3}" ,

                                AccountManager. getInstance ().getAccount()

                                       .getId()

                                       + "" ));

              }

              try {

                  HttpResponse response = client.execute(myget);

                  BufferedReader reader = new BufferedReader(

                         new InputStreamReader(response.getEntity()

                                .getContent()));

                  for (String s = reader.readLine(); s != null ; s = reader

                         .readLine()) {

                     builder.append(s);

                  }

                  JSONObject jsonObject = new JSONObject(builder.toString());

                  error = jsonObject.getInt( "errno" );

                  String errormsg = jsonObject.getString( "errmsg" );

                  Log. v ( "wgp" ,myget.getURI().toString());

                  Log. v ( "wgp" , "error=" + error);

                  Log. v ( "wgp" , "errormsg=" + errormsg);

                  if (error == 0) {

                     array = jsonObject.getJSONArray( "data" );

                     mFavoriteList = new ArrayList<FavoriteItem>();

                     FavoriteItem item = null ;

                     String srcType;

                     for ( int i = 0; i < array.length(); i++) {

                         item = new FavoriteItem();

                         item.setId(array.getJSONObject(i).getInt( "id" ));

                         item.setUserId(array.getJSONObject(i).getInt( "userid" ));

                         item.setUsername(array.getJSONObject(i).getString( "username" ));

                         item.setType(array.getJSONObject(i).getInt( "type" ));

                         item.setCid(array.getJSONObject(i).getInt( "cid" ));

                         item.setTitle(array.getJSONObject(i).getString( "title" ));

                         item.setUrl(array.getJSONObject(i).getString( "url" ));

                         item.setTag(array.getJSONObject(i).getString( "tag" ));

                         item.setSrc(array.getJSONObject(i).getString( "src" ));

                         item.setCreateDate(array.getJSONObject(i).getString( "created" ));

                         item.setModifiedDate(array.getJSONObject(i).getString( "modified" ));

                         srcType = array.getJSONObject(i).getString( "src_type" );

                         if (srcType != null && srcType.equals( "cps" )){

                            item.setSrc_type( "industry" );

                         } else {

                            item.setSrc_type(srcType);

                         }

                         mFavoriteList.add(item);

                     }

                     if ( mFavoriteList .size() == 0) {

                         handler .sendEmptyMessage(-1);

                     } else {

                         handler.sendEmptyMessage(1);

                     }

                  }

              } catch (Exception e) {

                  e.printStackTrace();

                  handler .sendEmptyMessage(-1);

              } finally {

              }

           }

       };

        t.start();

    }

   

    Handler handler = new Handler() {

       public void handleMessage(android.os.Message msg) {

           if (msg. what == 1) {

              mAdapter = new ItemAdapter();

              mList.setAdapter(mAdapter);

              mList .setOnItemClickListener( listener );

           }

           viewSwitcher .setVisibility(View. INVISIBLE );

       };

    };

   

    class ItemAdapter extends BaseAdapter {

 

       private ViewHolder holder ;

 

       @Override

       public int getCount() {

           return mFavoriteList.size();

       }

 

       @Override

       public Object getItem( int location) {

           return mFavoriteList.get(location);

       }

 

       @Override

       public long getItemId( int location) {

           return mFavoriteList .get(location).getId();

       }

 

       @Override

       public View getView ( int position, View convertView, ViewGroup parent) {

 

           try {

              if (convertView == null ) {

                  holder = new ViewHolder();

                  convertView = getLayoutInflater().inflate(

                         R.layout. layout_favorite_list_item , null );

                  holder . title = (TextView) convertView

                         .findViewById(R.id. favorite_title );

                  convertView.setTag( holder );

              } else {

                  holder = (ViewHolder) convertView.getTag();

              }

              holder . title .setText( mFavoriteList.get(position).getTitle() );

           } catch (Exception e) {

              e.printStackTrace();

           }

           return convertView;

 

       }

 

       class ViewHolder {

           TextView title ;

       }

    }

 

    @Override

    public void onClick(View v) {

       int id = v.getId();

       switch (id) {

       case R.id. btn_newscontent_back :

           backKey();

           break ;

 

       default :

           break ;

       }

    }

   

    private void backKey() {

       setResult( RESULT_OK );

       finish();

       getParent().overridePendingTransition(R.anim. news_dync_in_from_left ,

              R.anim. news_dync_out_to_right );

    }

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

收藏列表实例【ViewSwitcher+Thread+JSONObject ++handler+Adapter】


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论