我想做的效果是下啦listview进行数据更新。
updata()是从服务器端读取数据,将返回一个 resultListForJson,(类型 ArrayList<HashMap<String,Object>>())
我把updata()方法放在onCreate()里,只能做读取一次,就不能进行更新。我有想过把updata放到getdata(),但是就会报错,不知道怎么把handler和HttpAsyncTask怎么结合。求大神指导!
报错
updata()是从服务器端读取数据,将返回一个 resultListForJson,(类型 ArrayList<HashMap<String,Object>>())
我把updata()方法放在onCreate()里,只能做读取一次,就不能进行更新。我有想过把updata放到getdata(),但是就会报错,不知道怎么把handler和HttpAsyncTask怎么结合。求大神指导!
报错
FATAL EXCEPTION: Thread-10
05-21 22:00:11.376: E/AndroidRuntime(633): java.lang.ExceptionInInitializerError
05-21 22:00:11.376: E/AndroidRuntime(633): at com.flea.MainActivity.upData(MainActivity.java:82)
05-21 22:00:11.376: E/AndroidRuntime(633): at com.flea.MainActivity.getData(MainActivity.java:132)
05-21 22:00:11.376: E/AndroidRuntime(633): at com.flea.MainActivity$3.run(MainActivity.java:111)
05-21 22:00:11.376: E/AndroidRuntime(633): at java.lang.Thread.run(Thread.java:1019)
05-21 22:00:11.376: E/AndroidRuntime(633): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-21 22:00:11.376: E/AndroidRuntime(633): at android.os.Handler.<init>(Handler.java:121)
05-21 22:00:11.376: E/AndroidRuntime(633): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
05-21 22:00:11.376: E/AndroidRuntime(633): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
05-21 22:00:11.376: E/AndroidRuntime(633): at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
package com.flea;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import com.flea.autolistview.widget.AutoListView;
import com.flea.autolistview.widget.AutoListView.OnLoadListener;
import com.flea.autolistview.widget.AutoListView.OnRefreshListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity implements OnRefreshListener,
OnLoadListener {
private ArrayList<HashMap<String, Object>> resultListForJson=new ArrayList<HashMap<String,Object>>();
private ArrayList<HashMap<String, Object>> resultList=new ArrayList<HashMap<String,Object>>();
private ImageButton imgbtnnewdata;
private AutoListView lstv;
private MyAdapter myadapter;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
ArrayList<HashMap<String, Object>> result = (ArrayList<HashMap<String, Object>>) msg.obj;
switch (msg.what) {
case AutoListView.REFRESH:
lstv.onRefreshComplete();
resultList.clear();
resultList.addAll(result);
break;
case AutoListView.LOAD:
lstv.onLoadComplete();
resultList.addAll(result);
break;
}
Log.d("sqt", "resultList"+resultList);
lstv.setResultSize(resultListForJson.size());
myadapter.notifyDataSetChanged();
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstv = (AutoListView) findViewById(R.id.lstv);
//upData();//更新listview数据
Log.d("sqt", "resultList:"+resultList);
myadapter = new MyAdapter(MainActivity.this, resultList);
lstv.setAdapter(myadapter);
lstv.setOnRefreshListener(MainActivity.this);
lstv.setOnLoadListener(MainActivity.this);
initData();
Log.d("sqt","start upData");
upData();
Log.d("sqt","stop upData");
//final ListView myListView=(ListView)findViewById(R.id.listmenu);
imgbtnnewdata=(ImageButton)findViewById(R.id.imageButton1);
//需要在layout中创建listview,adpate将自动使用listview,其实listview的id名字需要为原始id,android:id="@+id/android:list"
//upData();//更新listview数据
setOnClickListener();//按钮的动作
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void upData(){
String url="http://192.168.1.128:8080/JsonServerHttp/GetSalesServlet";
HttpAsyncTask getMyString=new HttpAsyncTask(MainActivity.this);
resultListForJson=getMyString.doInBackground(url);
Log.d("sqt", "resultListForJson:"+resultListForJson);
}
public void setOnClickListener(){
imgbtnnewdata.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
private void initData() {
loadData(AutoListView.REFRESH);
}
private void loadData(final int what) {
// 这里模拟从服务器获取数据
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(700);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = handler.obtainMessage();
msg.what = what;
msg.obj = getData();
handler.sendMessage(msg);
}
}).start();
}
@Override
public void onLoad() {
// TODO Auto-generated method stub
loadData(AutoListView.LOAD);
}
@Override
public void onRefresh() {
// TODO Auto-generated method stub
loadData(AutoListView.REFRESH);
}
// 测试数据
public ArrayList<HashMap<String, Object>> getData() {
ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
//upData();
Log.d("sqt","start");
String salename=resultListForJson.get(1).get("salename").toString();
System.out.println("salename1"+salename);
for (int i=(resultListForJson.size()-1);i>-1;i--) {
HashMap<String, Object> map=new HashMap<String, Object>();
map.put("salename",resultListForJson.get(i).get("salename").toString());
map.put("saletime",resultListForJson.get(i).get("saletime").toString());
map.put("salemoney",resultListForJson.get(i).get("salemoney").toString());
result.add(map);
Log.d("sqt", "result:"+result);
}
return result;
}
}