import android.app.ProgressDialog;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Handler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class MainActivity extends ActionBarActivity {
private TextView tv;
private ListView lv;
private Handler handler;
private Thread thread;
private SimpleAdapter simpleAdapter;
private ProgressDialog progressDialog;
public MainActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView1);
lv=(ListView)findViewById(R.id.listView);
simpleAdapter=new SimpleAdapter(this,getdate(),R.layout.layout,new String[]{“name”},new int[]{R.id.textView2});
// progressDialog = ProgressDialog.show(MainActivity.this, “请稍等”, “正在下载数据…”, true);
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case 0:
// smAdapter.notifyDataSetChanged(); //发送消息通知ListView更新
//list.setAdapter(smAdapter); // 重新设置ListView的数据适配器
// tv.setText(getdate()+””);
simpleAdapter.notifyDataSetChanged();
lv.setAdapter(simpleAdapter);
break;
default:
//do something
break;
}
}
};
new Thread(new Runnable() {
@Override
public void run() {
try {
// Thread.sleep(2000);
SimpleClient.getHttpClient();//初始化工具类(SimpleClient)
zifuchuan=SimpleClient.doPost(“http://192.168.0.9/lianxi/shuchu.aspx”,null);
// getdate();
handler.sendEmptyMessage(0);
// progressDialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
private List<Map<String,Object>> getdate(){
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
try {
JSONArray arr = new JSONArray(zifuchuan);
for (int i = 0; i < arr.length(); i++) {
Map<String,Object> map=new HashMap<String,Object>();
JSONObject temp = (JSONObject) arr.get(i);
map.put(“name”,temp.getString(“name”));
list.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
String zifuchuan=””;
String name;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
simpleAdapter=new SimpleAdapter(this,getdate(),R.layout.layout,new String[]{“name”},new int[]{R.id.textView2}); 这句写在线程里是报错的!
单显示 getdate()是能显示出来的 可是我加了线程 就不显示 求各位大神给看看哪错了!