当我尝试通过HttpPost刷新或更新listview适配器时,我遇到了问题。
我想创建一个无休止的线程来调用HttpPost连接,以便获取数据并存储在适配器中。最后,我想将适配器重新设置为listview。我可以按钮工作。但我希望使用保持循环到mysql数据的线程。
请帮帮我。我找不到任何解决办法。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
lv2=(ListView)findViewById(R.id.ListView02);
tvalert=(TextView)findViewById(R.id.textviewalert);
getServerDatas("http://172.50.2.66/ChefGetOrdered.php");
tv = new TextView(this);
tv.setText("Incoming Order");
tv1 = new TextView(this);
tv1.setText(" Waiting list");
lv1.addHeaderView(tv);
lv2.addHeaderView(tv1);
lv2.setAdapter(adapter);
lv2.setClickable(true);
lv2.setTextFilterEnabled(true);
lv1.setAdapter(adapter);
lv1.setClickable(true);
lv1.setDivider(null);
lv1.setDividerHeight(0);
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = lv1.getItemAtPosition(position);
int a = (int) lv1.getItemIdAtPosition(position);
int ab = a+1;
Toast.makeText(Chef.this, o.toString() , Toast.LENGTH_LONG).show();
Toast.makeText(Chef.this, ab+"", Toast.LENGTH_LONG).show();
}
});
Button button2 = (Button) findViewById(R.id.ButtonCheck);
button2.setOnClickListener(this);
}
private String getServerDatas(String returnString) {
InputStream is = null;
String result = "";
List<Order> listOfPhonebook = new ArrayList<Order>();
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(returnString);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Toast.makeText(Chef.this, e.toString()+"Number 1 " , Toast.LENGTH_LONG).show();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
totalorder = jArray.length();
array_list=new String[totalorder];
table_list= new String[totalorder];
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
table_list[i]=json_data.getString("table_id");
listOfPhonebook.add(new Order("Table :"+json_data.getString("table_id")+"\n Time:",json_data.getString("order_foodname"),json_data.getString("order_quantity")));
}
adapter = new OrderAdapter(this, listOfPhonebook);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}