/**
* 事件列表
*
* @author 技术作坊
* @version 1.0
*/
public class RemindListViewActivity extends Activity {
private RemindListAdapter listItemAdapter = new RemindListAdapter(); // 适配器
private String jsonResult; // 从PHP返回的JSON
private List<JSONObject> jsonData; // JSON集合
private ImageView refreshImageView; // 刷新
private ProgressBar progressBar; // 圆形进度
private ListView listView; // 列表
private View footView; // 底部布局
private Button viewMoreButton; // 底部查看更多按钮
LinearLayout footerProgressBarLayout; // 底部圆形进度
private Handler handler;
private GetDetailThread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
this .requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.remind_listview);
listView = (ListView) findViewById(R.id.remind_ListView);
refreshImageView = (ImageView) findViewById(R.id.refresh_ImageView);
progressBar = (ProgressBar) findViewById(R.id.pb);
// 向列表添加一个页脚
LayoutInflater inflater = LayoutInflater.from( this );
footView = inflater.inflate(R.layout.load_more, null );
viewMoreButton = (Button) footView.findViewById(R.id.viewmorebutton);
footerProgressBarLayout = (LinearLayout) footView
.findViewById(R.id.linearlayout);
listView.addFooterView(footView);
/******当submintData()返回true时获取数据并显示查看更多数据的按钮,否则隐藏并提示程序错误******/
if (submintData( 0 ) == true ) {
progressBar.setVisibility(View.GONE);
footerProgressBarLayout.setVisibility(View.GONE);
handler = new DealHandler();
thread = new GetDetailThread();
thread.start(); // 开启一个线程获取数据
// refreshImageView.setOnClickListener(new
// refreshImageViewClick());// 刷新
viewMoreButton.setOnClickListener( new ViewMoreClickListener()); // 查看更多
} else {
handler = new DealHandler();
progressBar.setVisibility(View.GONE);
footerProgressBarLayout.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "程序错误" , Toast.LENGTH_SHORT)
.show();
}
refreshImageView.setOnClickListener( new refreshImageViewClick()); // 刷新
}
/**
* 查看更多
*
* @author Guozhilong
*
*/
class ViewMoreClickListener implements OnClickListener {
@Override
public void onClick(View v) {
footerProgressBarLayout.setVisibility(View.VISIBLE); // 显示
viewMoreButton.setVisibility(View.GONE); // 隐藏
handler.postDelayed( new Runnable() {
@Override
public void run() {
int count = listItemAdapter.getCount();
count += 10 ; // 在原有的基础上再加10
submintData(count);
// 当返回的JSON为空时做如下判断
if (jsonResult.equals( "null" )) {
footerProgressBarLayout.setVisibility(View.GONE); // 隐藏
viewMoreButton.setVisibility(View.VISIBLE); // 显示
Toast.makeText(getApplicationContext(),
"数据全部加载完成,没有更多数据" , Toast.LENGTH_SHORT).show();
} else {
footerProgressBarLayout.setVisibility(View.GONE); // 隐藏
viewMoreButton.setVisibility(View.VISIBLE); // 显示
try {
JSONArray jsonArray = new JSONArray(jsonResult);
System.out.print(jsonArray.length());
for ( int i = 0 ; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
listItemAdapter.addNewsItem(json);
}
listItemAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, 2000 );
}
}
/**
* 提交数据
*
* @param beginNumStr
* @return
*/
public Boolean submintData( int beginNumStr) {
Map<String, String> map = new HashMap<String, String>();
map.put( "beginNum" , String.valueOf(beginNumStr));
boolean result = false ;
try {
result = submintDataByHttpClientDoPost(map,
"http://192.168.1.116/DataInteraction/responsefenye.php" );
} catch (Exception e) {
e.printStackTrace();
}
if (result) {
return true ;
} else {
return false ;
}
}
/**
* 以HttpClient的DoPost方式提交数据到服务器
*
* @param map
* 传递进来的数据,以map的形式进行了封装
* @param path
* 要求服务器PHP的地址 返回的boolean类型的参数
* @throws Exception
*/
public Boolean submintDataByHttpClientDoPost(Map<String, String> map,
String path) throws Exception {
// 1. 获得一个相当于浏览器对象HttpClient,使用这个接口的实现类来创建对象,DefaultHttpClient
HttpClient hc = new DefaultHttpClient();
// DoPost方式请求的时候设置请求,关键是路径
HttpPost request = new HttpPost(path);
// 2. 为请求设置请求参数,也即是将要上传到web服务器上的参数
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : map.entrySet()) {
NameValuePair nameValuePairs = new BasicNameValuePair(
entry.getKey(), entry.getValue());
parameters.add(nameValuePairs);
}
// 请求实体HttpEntity也是一个接口,我们用它的实现类UrlEncodedFormEntity来创建对象,注意后面一个String类型的参数是用来指定编码的
HttpEntity entity = new UrlEncodedFormEntity(parameters, "gbk" );
request.setEntity(entity);
// 3. 执行请求
HttpResponse response = hc.execute(request);
// 4. 通过返回码来判断请求成功与否
if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
jsonResult = EntityUtils.toString(response.getEntity()); // 获取PHP端的echo
return true ;
}
return false ;
}
/**
* 获取JSON
*
* @param result
* @return
*/
public List<JSONObject> getData(String result) {
// String url = "http://192.168.1.116/DataInteraction/responsejson.php";
// String result = HttpConn.getResult(url);
jsonData = new ArrayList<JSONObject>();
try {
JSONArray jsonArray = new JSONArray(result);
for ( int i = 0 ; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
jsonData.add(json);
}
} catch (JSONException e) {
e.printStackTrace();
}
return jsonData;
}
// 开启线程获取数据
class GetDetailThread extends Thread {
public void run() {
jsonData = getData(jsonResult);
Message msg = handler.obtainMessage();
handler.sendMessage(msg);
}
}
class DealHandler extends Handler {
public void handleMessage(Message msg) {
listItemAdapter.setContext(RemindListViewActivity. this );
listItemAdapter.setList(jsonData);
listView.setAdapter(listItemAdapter);
}
}
/**
* 刷新
*
* @author Guozhilong
*
*/
class refreshImageViewClick implements OnClickListener {
@Override
public void onClick(View v) {
refreshImageView.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
footerProgressBarLayout.setVisibility(View.GONE);
handler.postDelayed( new Runnable() {
@Override
public void run() {
if (submintData( 0 ) == true ) {
thread = new GetDetailThread();
thread.start(); // 开启一个线程获取数据
refreshImageView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
footerProgressBarLayout.setVisibility(View.VISIBLE);
} else {
refreshImageView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
footerProgressBarLayout.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "程序错误" ,
Toast.LENGTH_SHORT).show();
}
}
}, 2000 );
}
}
}
|