我使用LayoutInflater
在我的应用程序中创建线性布局。我从Jason带来的这个项目的值,我将改变run方法中的值。
public void run()
{
/**
* Updating parsed JSON data into ListView
**/
// coNa .setText(productsList.get(0));
// coId.setText(productsList.get(1));
}
我在onCreate
函数中创建了它,但是当值发生变化时应用程序正在接收异常。
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.studenttablemain);
ll = (LinearLayout) findViewById(R.id.ll);
for (int i=0;i<5;i++){
View view = LayoutInflater.from(this).inflate(R.layout.studanttableitem,null);
TextView coNa = (TextView)findViewById(R.id.coursename);
TextView coId = (TextView)findViewById(R.id.courseid);
TextView teacherid = (TextView)findViewById(R.id.teachername);
TextView day = (TextView)findViewById(R.id.days);
TextView time = (TextView)findViewById(R.id.time);
TextView hole = (TextView)findViewById(R.id.hole);
// Assigning value to imageview and textview here
ll.addView(view);
}
new LoadAllProducts().execute();
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(CopyOfstudanttable.this);
pDialog.setMessage("Loading information . Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("studant: ", json.toString());
String name="";
String sid="";
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
//for (int i = 0; i < products.length(); i++) {
Log.d("length", "-"+products.length()+"-item");
JSONObject c = products.getJSONObject(0);
// Storing each json item in variable
//String id = c.getString(TAG_PID);
name = c.getString(TAG_NAME);
sid = c.getString(TAG_SID);
Log.d("name", "-"+c.getString(TAG_NAME)+"-item");
Log.d("id", "-"+c.getString(TAG_SID)+"-item");
productsList.add(name);
productsList.add(sid);
// }
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
LogIn.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.toString();
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
// coNa .setText(productsList.get(0));
// coId.setText(productsList.get(1));
}
});
}
}
答案 0 :(得分:1)
您只能从创建它们的线程(即ui线程)更改视图。要从另一个线程内部更改值,您可以使用例如View.post(Runnable)
方法。看看这个:Painless threading
<强>更新强> AsyncTask的onPostExecute方法已在ui线程上运行,因此你不必再在其中调用runOnUiThread
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
//Can set properties right here, cause you are already on Ui thread
coNa.setText(productsList.get(0));
coId.setText(productsList.get(1));
}
答案 1 :(得分:0)
您无法更新子线程中的UI。您应该使用处理程序从子线程接收消息并更新UI。
Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
/**
* Updating parsed JSON data into ListView
* */
// coNa .setText(productsList.get(0));
// coId.setText(productsList.get(1));
break;
}
super.handleMessage(msg);
}
};
class myThread implements Runnable {
public void run() {
// get Json data...
Message message = new Message();
message.what = 1;
this.myHandler.sendMessage(message);
}
}
我的代码:
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 1:
if (historyInfos == null) {
return;
}
mAdapter.notifyDataSetChanged();
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call_detail);
initView();
initData();
}
答案 2 :(得分:0)
您必须将代码从 public static void sendGET() throws IOException {
FileOutputStream fos = null;
URL obj = new URL("https://crowdtest-fileservice.azure-mobile.net/api/files/");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("sharename", "newamactashare");
con.setRequestProperty("directorypath", "MaheshApp/TestLibrary/");
con.setRequestProperty("filename", "Test.apk");
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
fos = new FileOutputStream("C:/Users/uma.maheshwaran/Desktop/Test.mt");
InputStream iin = con.getInputStream();
byte[] buffer = new byte[4096]; // declare 4KB buffer
int len;
// while we have availble data, continue downloading and storing to
// local file
while ((len = iin.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
iin.close();
fos.close();
// print result
System.out.println("Done");
} else {
BufferedReader br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("GET request not worked");
}
}
移至doInBackground
方法。您应该从 uiThread
onPostExecute