layout_favorite_list.xml
<?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;
publicclass FavoriteActivityList extends Activity implements OnClickListener{
publicstatic String NEWS_TITLE = "title";
publicstatic String NEWS_TYPE = "type";
private ListView mList;
private List<FavoriteItem> mFavoriteList;
private ItemAdapter mAdapter;
private ViewSwitcher viewSwitcher;
private ImageButton mBackImageButton;
private TextView mTitleTextView;
privateintmMode;
private PopupWindowImageButton mMenuImageButton;
@Override
protectedvoid 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
protectedvoid onResume() {
// TODO Auto-generated method stub
super.onResume();
mMenuImageButton.reload(Util.SHOW_FAVORITE);
}
privateOnItemClickListenerlistener = 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);
}elseif(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);
}
}
};
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 0:
requestData();
break;
default:
break;
}
}
privatevoid requestData() {
viewSwitcher.setVisibility(View.VISIBLE);
viewSwitcher.showNext();
Thread t = new Thread() {
@Override
publicvoid 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() {
publicvoid 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
publicint getCount() {
return mFavoriteList.size();
}
@Override
public Object getItem(int location) {
returnmFavoriteList.get(location);
}
@Override
publiclong getItemId(int location) {
returnmFavoriteList.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
publicvoid onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_newscontent_back:
backKey();
break;
default:
break;
}
}
privatevoid backKey() {
setResult(RESULT_OK);
finish();
getParent().overridePendingTransition(R.anim.news_dync_in_from_left,
R.anim.news_dync_out_to_right);
}
}