我想在UI线程上运行doinbackground()
,但是出现cannot resolve runOnUiThread
错误。我需要这样做,因为主线程上的负载过多
这是我的活动:
public class valance extends Fragment {
Button get,add;
ListView list;
SimpleAdapter ADAhere;
ProgressBar progressBar;
Connection connect;
String ConnectionResult = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_valance, container, false);
list = (ListView)rootView.findViewById(R.id.raill);
add = (Button)rootView.findViewById(R.id.btn_add);
progressBar = (ProgressBar)rootView.findViewById(R.id.PB_Getting);
progressBar.setVisibility(View.GONE);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(),Add_valance.class);
startActivity(i);
}
});
new GetValence().execute();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/** To change selected state view */
view.setSelected(true);
HashMap<String, Object> obj = (HashMap<String, Object>) ADAhere.getItem(position);
String SlectedName = (String) obj.get("NAME");
String SlectedPrice = (String) obj.get("PRICE");
String SlectedSize = (String) obj.get("SIZE");
String SlectedRange = (String) obj.get("RANGE");
String SlectedSupp = (String) obj.get("SUPPLIER");
// Toast.makeText(getActivity().getApplicationContext(), SlectedName, Toast.LENGTH_SHORT).show();
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow();
//dialog.setTitle("Confirm your Vote");
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.diaglog);
final TextView VName = (TextView) dialog.findViewById(R.id.Name);
final TextView VRange = (TextView) dialog.findViewById(R.id.Range);
final TextView VSUPPLIER = (TextView) dialog.findViewById(R.id.Supplier);
final TextView VSIZE = (TextView) dialog.findViewById(R.id.Size);
final TextView VPrice = (TextView) dialog.findViewById(R.id.Price);
VName.setText(SlectedName);
VRange.setText(SlectedRange);
VSUPPLIER.setText(SlectedSupp);
VSIZE.setText(SlectedSize);
VPrice.setText(SlectedPrice);
dialog.show();
Button cancelBtn = (Button) dialog.findViewById(R.id.cancel_btn);
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
});
return rootView;
}
public class GetValence extends AsyncTask<String, Integer, List<Map<String, String>>> {
Connection connect;
String ConnectionResult = "";
Boolean isSuccess = false;
public List<Map<String, String>> doInBackground(String... params) {
List<Map<String, String>> data = null;
data = new ArrayList<>();
try {
ConnectionHelper conStr = new ConnectionHelper();
connect = conStr.connectionclass(); // Connect to database
if (connect == null) {
ConnectionResult = "Check Your Internet Access!";
} else {
// Change below query according to your own database.
String query = "select * from cc_valence";
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
Map<String, String> datanum = new HashMap<String, String>();
datanum.put("NAME", rs.getString("VALENCE_NAME"));
datanum.put("PRICE", rs.getString("VALENCE_UNIT_PRICE"));
datanum.put("RANGE", rs.getString("VALENCE_RANGE"));
datanum.put("SUPPLIER", rs.getString("VALENCE_SUPPLIER"));
datanum.put("SIZE", rs.getString("VALENCE_SIZE"));
data.add(datanum);
}
ConnectionResult = " successful";
isSuccess = true;
connect.close();
}
} catch (Exception ex) {
isSuccess = false;
ConnectionResult = ex.getMessage();
}
return data;
}
public void onPostExecute(List<Map<String, String>> result) {
String[] fromwhere = {"NAME", "PRICE", "SIZE", "RANGE", "SUPPLIER"};
int[] viewswhere = {R.id.Name_txtView, R.id.price_txtView, R.id.size_txtView};
ADAhere = new SimpleAdapter(getActivity(), result, R.layout.list_valence, fromwhere, viewswhere);
list.setAdapter(ADAhere);
}
}
我在某处读到AsyncTask
在不同的线程上工作,但doinbackground
却没有,因此导致一些严重的滞后,因为我正在处理我的主线程,所以我想知道我是否可以实现UI线程,但我以前从未使用过它,因此即使在观看了教程之后
答案 0 :(得分:0)
如果您在片段中使用runOnUiThread
,请尝试以下代码
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// Code
}
});
答案 1 :(得分:0)
runOnUiThread()
是Activity
的一部分(请参见docs),因此,如果要在Fragment
中使用它,则需要先获取片段的活动对象,并使用getActivity()方法。
getActivity().runOnUiThread(runnable);
答案 2 :(得分:0)
我在您的问题中没有看到runOnUiThread
的任何内容,但除此之外,还有几点:
doInBackground
中的AsyncTask
例程不是在UI线程上运行,而是使用它。它在后台线程上运行,因此您可以在那里进行更长的操作而不会阻塞UI线程。onPreExecute
do 中的onPostExecute
和AsyncTask
方法在UI线程上运行,因此您可以在其中更新视图或与UI交互doinbackground()
”,所以我认为您误解了UI线程(一个处理UI的线程)和其他后台线程(非-UI线程,您要在其中运行更长的操作)。也许某些答案here有助于澄清。