我正在通过程序填写表格布局。这需要很多时间。所以直到整个表格布局没有填满,我想显示进度对话框。
我不能使用 UIthread 以外的新线程来显示进度对话框以及填充表格布局。
如何在填充表格布局的同时显示 porgress 对话框。
谢谢。
这是整个代码
public class MainActivity extends Activity {
private static TextView edtSearch;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> lsMdlMaterial = new ArrayList<String>();
for(int i=0;i<1000;i++){
lsMdlMaterial.add("item:"+(i+1));
}
final TextView Txt= new TextView(this);
final Handler handler= new Handler();
final Handler handler1= new Handler();
showProgressDialog();
new Thread(new Runnable() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
showCommoditySelection(MainActivity.this,Txt, lsMdlMaterial);
dismissProgressDialog();
}
});
}
}).start();
}
private void showProgressDialog() {
mProgressDialog = new ProgressDialog(this);
mProgressDialog = ProgressDialog.show(
this,
"",
"loading", true);
}
/**
* if progressdialog not null than dismiss.
* **/
private void dismissProgressDialog() {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
// showCommoditySelection Selection using Interface.
public static void showCommoditySelection(final Activity activity,
final TextView txtMaterial,
final ArrayList<String> lsMdlMaterial) {
/* click distributor view and open popup */
final Dialog popupWindow = new Dialog(activity);
popupWindow.requestWindowFeature(Window.FEATURE_NO_TITLE);
popupWindow.setContentView(R.layout.xml_sc_selectdistributor);
popupWindow.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
popupWindow.setCanceledOnTouchOutside(false);
popupWindow.show();
final TableLayout SortByTableView = (TableLayout) popupWindow
.findViewById(R.id.xml_sc_selectdistributor_tblView);
edtSearch = (EditText) popupWindow
.findViewById(R.id.xml_salescontract_edtSearch);
/*edtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence keyword, int start,
int before, int count) {
FillCommodity(popupWindow, activity,
txtMaterial, lsMdlMaterial, SortByTableView,
keyword.toString());
}
@Override
public void beforeTextChanged(CharSequence keyword, int start,
int count, int after) {
}
@Override
public void afterTextChanged(Editable editable) {
}
});*/
FillCommodity(popupWindow, activity, txtMaterial,
lsMdlMaterial, SortByTableView, "");
}
private static void FillCommodity(final Dialog popupWindow,
final Activity activity,
final TextView txtMaterial,
final ArrayList<String> lsMdlMaterial,
TableLayout SortByTableView, String keyword) {
SortByTableView.removeAllViews();
for (int i = 0; i < lsMdlMaterial.size(); i++) {
String codeName = lsMdlMaterial.get(i)
.toLowerCase()
;
if (codeName.contains(keyword)) {
View viewLine = new View(activity);
viewLine.setLayoutParams(new LayoutParams(514, 1));
final TextView txt = new TextView(activity);
txt.setHeight(40);
txt.setPadding(10, 10, 10, 10);
txt.setTextSize(16);
txt.setId(i);
txt.setText(lsMdlMaterial.get(i));
SortByTableView.addView(txt);
SortByTableView.addView(viewLine);
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
}
}
}
}