<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:focusableInTouchMode="true"
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>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="@string/advisement_text"
android:textColor="@color/black1"
android:textSize="18sp" />
<EditText
android:id="@+id/advisement_id"
android:layout_width="match_parent"
android:layout_height="130dip"
android:layout_margin="10dp"
android:gravity="top"
android:hint="@string/advisement_description"
android:textSize="16sp" />
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:background="@drawable/register_finish_button"
android:text="@string/submit_advisement"
android:textColor="@color/white1"
android:textSize="20sp" />
<
ListView
android:id="@+id/advisement_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@color/transparent"
android:divider="@drawable/tip_line" />
</LinearLayout>
</FrameLayout>
MyAdvisementActivity.java
package com.cps.media.ui.advisement;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
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 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.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import com.cps.media.R;
import com.cps.media.model.AccountItem;
import com.cps.media.model.AdviseItem;
import com.cps.media.ui.account.AccountLoginActivity;
import com.cps.media.ui.account.AccountManager;
import com.cps.media.ui.advisement.AdvisementHistoryActivity.MyAdapter;
import com.cps.media.ui.advisement.AdvisementHistoryActivity.MyAdapter.ViewHolder;
import com.cps.media.util.CONST;
public class MyAdvisementActivity extends Activity implements OnClickListener {
private String TAG = "MyAdvisementActivity";
private EditText mAdvisementText;
private Button mSubmitButton;
private ListView mListView;
private List<AdviseItem> mAdviseList;
private BaseAdapter mAdapter;
private AdviseItem mAdvise;
private ViewSwitcher viewSwitcher;
private SimpleDateFormat mDateFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_my_advisement);
mDateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
mAdvisementText = (EditText) findViewById(R.id.advisement_id);
mSubmitButton = (Button) findViewById(R.id.submit_button);
mSubmitButton.setOnClickListener(this);
mListView = (ListView) findViewById(R.id.advisement_list);
viewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher_news_top);
viewSwitcher.addView(getLayoutInflater().inflate(
R.layout.layout_progress_page, null));
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(AccountManager.getInstance().getAccount() != null ){
requestData();
}else{
viewSwitcher.setVisibility(View.INVISIBLE);
}
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.submit_button:
AccountItem account = AccountManager.getInstance().getAccount();
Intent intent = null;
if (account == null) {
intent = new Intent(this, AccountLoginActivity.class);
startActivity(intent);
} else {
String content = mAdvisementText.getText().toString().trim();
if (content == null || "".equals(content.trim())) {
Toast.makeText(MyAdvisementActivity.this, "请填写咨询内容。",
Toast.LENGTH_SHORT).show();
return;
}
mAdvise = new AdviseItem();
mAdvise.setUsername(account.getUsername());
mAdvise.setEmail(account.getEmail());
mAdvise.setContent(mAdvisementText.getText().toString());
submitData();
}
break;
default:
break;
}
}
private void
submitData
() {
Thread t = new Thread() {
@Override
public void run() {
super.run();
HttpClient client = new DefaultHttpClient();
StringBuilder builder = new StringBuilder();
int error = 1;
String url = CONST.URL_ADVISEMENT;
try {
url = url.replace("{0}", URLEncoder.encode(
mAdvise.getUsername(), CONST.URL_ENCODING_UTF_8));
url = url.replace("{1}", URLEncoder.encode(
mAdvise.getEmail(), CONST.URL_ENCODING_UTF_8));
url = url.replace("{2}", URLEncoder.encode(
mAdvise.getContent(), CONST.URL_ENCODING_UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
HttpGet myget = new HttpGet(url);
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", "error=" + error);
Log.v("wgp", "errormsg=" + errormsg);
if (error == 0) {
handler.sendEmptyMessage(1);
} else {
handler.sendEmptyMessage(-1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
};
t.start();
}
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;
HttpGet myget = new HttpGet(CONST.URL_ADVISEMENT_ACCOUNT_LIST+AccountManager.getInstance().getAccount().getUsername());
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(TAG, "error=" + error);
Log.v(TAG, "errormsg=" + errormsg);
JSONArray array = null;
if (error == 0) {
AdviseItem item;
mAdviseList = new ArrayList<AdviseItem>();
array = jsonObject.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
item = new AdviseItem();
item.setId(array.getJSONObject(i).getInt("id"));
item.setContent(array.getJSONObject(i).getString(
"content"));
item.setUsername(array.getJSONObject(i).getString(
"user_name"));
item.setAddTime(convert2String(array.getJSONObject(i).getLong("add_time")));
mAdviseList.add(item);
}
if (mAdviseList.size() > 0) {
handler.sendEmptyMessage(2);
//2...............................
}
}else{
handler.sendEmptyMessage(-2);
}
} catch (Exception e) {
handler.sendEmptyMessage(-1);
e.printStackTrace();
} finally {
}
}
};
t.start();
}
public String convert2String(long time) {
if (time > 0l) {
Date date = new Date(time * 1000);
return mDateFormat.format(date);
}
return "";
}
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
mAdvisementText.setText("");
requestData();
Toast.makeText(MyAdvisementActivity.this, "感谢您的咨询,我们将会尽快给您答复。",
Toast.LENGTH_SHORT).show();
} else
if (msg.what == 2) {
//2............................
mAdapter = new MyAdapter();
mListView.setAdapter(mAdapter);
} else if (msg.what == -1) {
Toast.makeText(MyAdvisementActivity.this, "提交失败!请检查网络连接是否正常。",
Toast.LENGTH_SHORT).show();
}
viewSwitcher.setVisibility(View.INVISIBLE);
};
};
class MyAdapter extends BaseAdapter {
private ViewHolder holder;
@Override
public int getCount() {
return mAdviseList.size();
}
@Override
public Object getItem(int location) {
return mAdviseList.get(location);
}
@Override
public long getItemId(int location) {
return mAdviseList.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_advisement_list_item, null);
holder.tv1 = (TextView) convertView
.findViewById(R.id.advisement_test);
holder.tvtime = (TextView) convertView
.findViewById(R.id.advisement_time);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv1.setText(mAdviseList.get(position).getContent());
holder.tvtime.setText(mAdviseList.get(position).getAddTime());
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
class ViewHolder {
TextView tv1;
TextView tvtime;
}
}
}
AdviseItem.java
package com.cps.media.model;
import java.io.Serializable;
public class AdviseItem implements Serializable{
private static final long serialVersionUID = 1L;
private int id;
private String content;
private String username;
private String email;
private String addTime;
public String getAddTime() {
return addTime;
}
public void setAddTime(String addTime) {
this.addTime = addTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}