我有一个应用程序,我从服务器获取数据并相应地更新视图。 我的代码如下:
public class NotificationActivity extends Activity{
String userid;
String ordernumber;
TextView txtLabelOrderNumber;
TextView txtLabelAmountReceived;
TextView txtLabelDeliveredBy;
String amountreceived;
String orderno;
String deliveredby;
String productname;
String brand;
String quantity;
String price;
TableLayout table_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
JSONObject json;
txtLabelOrderNumber = (TextView)findViewById(R.id.txtLabelOrderNumber);
txtLabelAmountReceived = (TextView)findViewById(R.id.txtLabelAmountReceived);
txtLabelDeliveredBy = (TextView)findViewById(R.id.txtLabelDeliveredBy);
table_layout = (TableLayout) findViewById(R.id.tableLayout1);
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.GRAY);
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
TextView label_productname = new TextView(this);
label_productname.setText("Name");
label_productname.setTextColor(Color.WHITE);
label_productname.setPadding(5, 5, 5, 5);
tr_head.addView(label_productname);// add the column to the table row here
TextView label_brand = new TextView(this);
label_brand.setText("Brand"); // set the text for the header
label_brand.setTextColor(Color.WHITE); // set the color
label_brand.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(label_brand); // add the column to the table row here
TextView label_quantity = new TextView(this);
label_quantity.setText("Qty"); // set the text for the header
label_quantity.setTextColor(Color.WHITE); // set the color
label_quantity.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(label_quantity); // add the column to the table row here
TextView label_amount = new TextView(this);
label_amount.setText("Amount"); // set the text for the header
label_amount.setTextColor(Color.WHITE); // set the color
label_amount.setPadding(5, 5, 5, 5); // set the padding (if required)
tr_head.addView(label_amount); // add the column to the table row here
table_layout.addView(tr_head);
try {
json = new JSONObject(extras.getString( "com.parse.Data" ));
Iterator itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
if(key.equals("ordernumber"))
{
ordernumber = json.getString(key);
Toast.makeText(getApplicationContext(), "key:"+json.getString(key), Toast.LENGTH_LONG).show();
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
getOrderDetails(ordernumber);
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void getOrderDetails(String ordernumber)
{
String result="";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.comy/getorderdetails.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("ordernumber", ordernumber));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
result=inputStreamToString(response.getEntity().getContent()).toString();
System.out.println("result: "+result);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
final TableRow row = new TableRow(this);
final TextView labelproductname = new TextView(this);
final TextView labelquantity = new TextView(this);
final TextView labelbrand = new TextView(this);
final TextView labelprice = new TextView(this);
JSONObject json_data = jArray.getJSONObject(i);
amountreceived =json_data.getString("amountreceived");
orderno =json_data.getString("ordernumber");
deliveredby =json_data.getString("deliveredby");
productname =json_data.getString("productname");
brand =json_data.getString("brand");
quantity =json_data.getString("quantity");
price =json_data.getString("price");
System.out.println("amountreceived: "+amountreceived);
if(i==0)
{
runOnUiThread(new Runnable() {
public void run() {
txtLabelOrderNumber.append(" "+orderno);
txtLabelAmountReceived.append(" "+amountreceived);
txtLabelDeliveredBy.append(" "+deliveredby);
}
});
}
runOnUiThread(new Runnable() {
public void run() {
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
labelproductname.setText(productname);
labelproductname.setPadding(2, 0, 5, 0);
row.addView(labelproductname);
labelbrand.setText(brand);
labelbrand.setPadding(2, 0, 5, 0);
row.addView(labelbrand);
labelquantity.setText(quantity);
labelquantity.setPadding(2, 0, 5, 0);
row.addView(labelquantity);
labelprice.setText(price);
labelprice.setPadding(2, 0, 5, 0);
row.addView(labelprice);
table_layout.addView(row);
}
});
}
}catch(JSONException e){
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Return full string
return total;
}
}
getorderdetails.php以json格式返回以下数据:
result: [{"amount":"400","ordernumber":"1-20140715171341","amountreceived":"400","deliveredby":"alok","productname":"biscuit","brand":"parle","quantity":"20","price":"200"},{"amount":"400","ordernumber":"1-20140715171341","amountreceived":"400","deliveredby":"alok","productname":"atta","brand":"pilsbury","quantity":"1","price":"200"}]
问题是,现在相同的数据会在表格布局中重复出现。
这是productname atta在表格布局中重复。
当我在runOnUiThread()函数之前打印productname时,它打印出不同的名称但是当我在runOnUiThread()中获取productname时,我得到相同的结果。
如何在ui线程中使表格布局的textview明显?
答案 0 :(得分:0)
为什么需要将代码放在线程中。你只是在这里设置值。
if(i==0)
{
runOnUiThread(new Runnable() {//why are you using this
public void run() {
txtLabelOrderNumber.append(" "+orderno);
txtLabelAmountReceived.append(" "+amountreceived);
txtLabelDeliveredBy.append(" "+deliveredby);
}
});
}
runOnUiThread(new Runnable() {//why are you using this
public void run() {
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
labelproductname.setText(productname);
labelproductname.setPadding(2, 0, 5, 0);
row.addView(labelproductname);
labelbrand.setText(brand);
labelbrand.setPadding(2, 0, 5, 0);
row.addView(labelbrand);
labelquantity.setText(quantity);
labelquantity.setPadding(2, 0, 5, 0);
row.addView(labelquantity);
labelprice.setText(price);
labelprice.setPadding(2, 0, 5, 0);
row.addView(labelprice);
table_layout.addView(row);
}
});
线程内的代码是独立执行的,相反指针向前移动增加i的值,直到labelproductname.setText(productname);
这一行执行,我们将下一个Json对象值作为“atta”。
使用调试器来理解代码所遵循的流程。当您的可运行线程稍后执行时,您的循环将被执行,因此最后保存的值在字符串productname和其他设置中。
作为解决方案,只需付出
if(i==0)
{
/* runOnUiThread(new Runnable() {
public void run() {*/
txtLabelOrderNumber.append(" "+orderno);
txtLabelAmountReceived.append(" "+amountreceived);
txtLabelDeliveredBy.append(" "+deliveredby);
/* }
});*/
}
/* runOnUiThread(new Runnable() {
@SuppressLint("NewApi") public void run() {
*/
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
labelproductname.setText(productname);
labelproductname.setPadding(2, 0, 5, 0);
row.addView(labelproductname);
labelbrand.setText(brand);
labelbrand.setPadding(2, 0, 5, 0);
row.addView(labelbrand);
labelquantity.setText(quantity);
labelquantity.setPadding(2, 0, 5, 0);
row.addView(labelquantity);
labelprice.setText(price);
labelprice.setPadding(2, 0, 5, 0);
row.addView(labelprice);
table_layout.addView(row);
/* }
});*/
}
希望这能满足您的需求。