android 在 asynctask 中显示 dialogfragment
android displaying dialogfragment in asynctask
我的 AsyncTask 中的对话框片段有一些问题。基本上这是发生的事情的要点。我有一个自定义列表适配器来构建 ListView。在执行前,我显示我的对话框。数据通过 doInBackground 加载到列表中。在 post 执行时,我关闭了我的对话框。问题是我现在只剩下一个空白屏幕。对话框出现然后消失,但没有项目列表。真的不确定发生了什么。这是我的 DialogFragment class:
public class ProcessingDialog extends DialogFragment {
private String dialog_text;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialog_view = inflater.inflate(R.layout.dialog_loading, null);
final TextView tv1 = (TextView) dialog_view.findViewById(R.id.dialog_text);
dialog_text = getArguments().getString("message");
tv1.setText(dialog_text);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(dialog_view);
// Create the AlertDialog object and return it
return builder.create();
}
}
这是我的 MainActivity:
public class MainActivity extends FragmentActivity {
private Context context = this;
private List<RandomdataModel> list;
private LayoutInflater layoutInflater;
private CustomListAdapter listAdapter;
private ProcessingDialog dialog_fragment_loading;
private MessageDialog dialog_fragment_message;
private final String[] parts = new String[]{"Sprocket", "Screw", "Nail",
"Widget", "Gear", "Cog", "Tube", "Spring", "Tape", "Bolt"};
private static final String LOGTAG = "MainActivity";
private static final boolean DEBUG = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
listAdapter = new CustomListAdapter(this);
ListView listView = (ListView) this.findViewById(R.id.listView);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
if (DEBUG) {
Log.d(LOGTAG, "onItemClick(): arg0=" + arg0.getClass().getSimpleName());
Log.d(LOGTAG, "onItemClick(): arg1=" + view.getClass().getSimpleName());
}
TextView tv1 = (TextView) view.findViewById(R.id.listitemTextIDValue);
TextView tv2 = (TextView) view.findViewById(R.id.listitemTextPartValue);
RandomdataModel data = new RandomdataModel();
data.setId(Integer.parseInt(tv1.getText().toString()));
data.setPart(tv2.getText().toString());
Intent intent = new Intent(context, DisplayActivity.class);
intent.putExtra("data", data);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
});
}
@Override
public void onResume() {
//called after onStart() just before activity comes to the foreground
super.onResume();
new UpdateList().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class CustomListAdapter
extends BaseAdapter {
private Context context;
private List<RandomdataModel> list;
private LayoutInflater layoutInflater;
CustomListAdapter(Context context) {
this.context = context;
}
public void setList(List<RandomdataModel> list) {
this.list = list;
}
@Override
public int getCount() {
return ((list == null) ? 0 : list.size());
}
@Override
public Object getItem(int position) {
// In theory we should not be called if getCount() returned 0;
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
int position;
TextView textView1;
TextView textView2;
Button button;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (DEBUG) {
Log.d(LOGTAG, "getView.position=" + position);
Log.d(LOGTAG, "getView.convertView=" + convertView);
}
if (list == null) {
// In theory it should not happen but handle this in some graceful way.
// Returning null will not produce graceful results.
//display dialog of no data found....
return null;
}
ViewHolder holder;
if (convertView == null) {
convertView = getLayoutInflator().inflate(R.layout.list_view_items, null);
holder = new ViewHolder();
holder.textView1 = (TextView) convertView.findViewById(R.id.listitemTextIDValue);
holder.textView2 = (TextView) convertView.findViewById(R.id.listitemTextPartValue);
holder.button = (Button) convertView.findViewById(R.id.listitemButton);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
RandomdataModel item = list.get(position);
holder.textView1.setText("" + item.getId());
holder.textView2.setText(item.getPart());
holder.button.setText("Submit " + item.getId());
return convertView;
}
private LayoutInflater getLayoutInflator() {
if (layoutInflater == null) {
layoutInflater = (LayoutInflater)
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
return layoutInflater;
}
}
class UpdateList
extends AsyncTask<Void, Void, List<RandomdataModel>> {
private final String LOGTAG = "Updating List";
private final boolean DEBUG = true;
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
//progressBarCircle.setVisibility(View.VISIBLE);
dialog_fragment_loading = processingDialog("Loading Data...");
dialog_fragment_loading.show(getSupportFragmentManager(), "loading_data");
}
@Override
protected List<RandomdataModel> doInBackground(Void... params) {
if (DEBUG) Log.d(LOGTAG, "**** doInBackground() STARTING");
List<RandomdataModel> listData = new ArrayList<>();
RandomdataModel data;
int len = parts.length;
try {
Thread.sleep(4000);
//loading data from array
for (int i = 0; i < len; i++) {
data = new RandomdataModel();
data.setId(i);
data.setPart(parts[i]);
listData.add(data);
}
} catch (Exception e) {
if (DEBUG) Log.d(LOGTAG, e.toString());
e.printStackTrace();
}
return listData;
}
@Override
protected void onPostExecute(List<RandomdataModel> listData) {
//close dialog
dialog_fragment_loading.dismiss();
listAdapter.setList(listData);
}
}
public static ProcessingDialog processingDialog(String message) {
final ProcessingDialog f = new ProcessingDialog();
final Bundle args = new Bundle();
args.putString("message", message);
f.setArguments(args);
return f;
}
}
一直在搜索和搜索,只是没有运气,所以我想 post 我的问题在这里。任何帮助将不胜感激!
请在设置新列表后尝试调用listAdapter.notifyDataSetChanged()
。
我的 AsyncTask 中的对话框片段有一些问题。基本上这是发生的事情的要点。我有一个自定义列表适配器来构建 ListView。在执行前,我显示我的对话框。数据通过 doInBackground 加载到列表中。在 post 执行时,我关闭了我的对话框。问题是我现在只剩下一个空白屏幕。对话框出现然后消失,但没有项目列表。真的不确定发生了什么。这是我的 DialogFragment class:
public class ProcessingDialog extends DialogFragment {
private String dialog_text;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialog_view = inflater.inflate(R.layout.dialog_loading, null);
final TextView tv1 = (TextView) dialog_view.findViewById(R.id.dialog_text);
dialog_text = getArguments().getString("message");
tv1.setText(dialog_text);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(dialog_view);
// Create the AlertDialog object and return it
return builder.create();
}
}
这是我的 MainActivity:
public class MainActivity extends FragmentActivity {
private Context context = this;
private List<RandomdataModel> list;
private LayoutInflater layoutInflater;
private CustomListAdapter listAdapter;
private ProcessingDialog dialog_fragment_loading;
private MessageDialog dialog_fragment_message;
private final String[] parts = new String[]{"Sprocket", "Screw", "Nail",
"Widget", "Gear", "Cog", "Tube", "Spring", "Tape", "Bolt"};
private static final String LOGTAG = "MainActivity";
private static final boolean DEBUG = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
listAdapter = new CustomListAdapter(this);
ListView listView = (ListView) this.findViewById(R.id.listView);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
if (DEBUG) {
Log.d(LOGTAG, "onItemClick(): arg0=" + arg0.getClass().getSimpleName());
Log.d(LOGTAG, "onItemClick(): arg1=" + view.getClass().getSimpleName());
}
TextView tv1 = (TextView) view.findViewById(R.id.listitemTextIDValue);
TextView tv2 = (TextView) view.findViewById(R.id.listitemTextPartValue);
RandomdataModel data = new RandomdataModel();
data.setId(Integer.parseInt(tv1.getText().toString()));
data.setPart(tv2.getText().toString());
Intent intent = new Intent(context, DisplayActivity.class);
intent.putExtra("data", data);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
});
}
@Override
public void onResume() {
//called after onStart() just before activity comes to the foreground
super.onResume();
new UpdateList().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class CustomListAdapter
extends BaseAdapter {
private Context context;
private List<RandomdataModel> list;
private LayoutInflater layoutInflater;
CustomListAdapter(Context context) {
this.context = context;
}
public void setList(List<RandomdataModel> list) {
this.list = list;
}
@Override
public int getCount() {
return ((list == null) ? 0 : list.size());
}
@Override
public Object getItem(int position) {
// In theory we should not be called if getCount() returned 0;
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
int position;
TextView textView1;
TextView textView2;
Button button;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (DEBUG) {
Log.d(LOGTAG, "getView.position=" + position);
Log.d(LOGTAG, "getView.convertView=" + convertView);
}
if (list == null) {
// In theory it should not happen but handle this in some graceful way.
// Returning null will not produce graceful results.
//display dialog of no data found....
return null;
}
ViewHolder holder;
if (convertView == null) {
convertView = getLayoutInflator().inflate(R.layout.list_view_items, null);
holder = new ViewHolder();
holder.textView1 = (TextView) convertView.findViewById(R.id.listitemTextIDValue);
holder.textView2 = (TextView) convertView.findViewById(R.id.listitemTextPartValue);
holder.button = (Button) convertView.findViewById(R.id.listitemButton);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
RandomdataModel item = list.get(position);
holder.textView1.setText("" + item.getId());
holder.textView2.setText(item.getPart());
holder.button.setText("Submit " + item.getId());
return convertView;
}
private LayoutInflater getLayoutInflator() {
if (layoutInflater == null) {
layoutInflater = (LayoutInflater)
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
return layoutInflater;
}
}
class UpdateList
extends AsyncTask<Void, Void, List<RandomdataModel>> {
private final String LOGTAG = "Updating List";
private final boolean DEBUG = true;
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
//progressBarCircle.setVisibility(View.VISIBLE);
dialog_fragment_loading = processingDialog("Loading Data...");
dialog_fragment_loading.show(getSupportFragmentManager(), "loading_data");
}
@Override
protected List<RandomdataModel> doInBackground(Void... params) {
if (DEBUG) Log.d(LOGTAG, "**** doInBackground() STARTING");
List<RandomdataModel> listData = new ArrayList<>();
RandomdataModel data;
int len = parts.length;
try {
Thread.sleep(4000);
//loading data from array
for (int i = 0; i < len; i++) {
data = new RandomdataModel();
data.setId(i);
data.setPart(parts[i]);
listData.add(data);
}
} catch (Exception e) {
if (DEBUG) Log.d(LOGTAG, e.toString());
e.printStackTrace();
}
return listData;
}
@Override
protected void onPostExecute(List<RandomdataModel> listData) {
//close dialog
dialog_fragment_loading.dismiss();
listAdapter.setList(listData);
}
}
public static ProcessingDialog processingDialog(String message) {
final ProcessingDialog f = new ProcessingDialog();
final Bundle args = new Bundle();
args.putString("message", message);
f.setArguments(args);
return f;
}
}
一直在搜索和搜索,只是没有运气,所以我想 post 我的问题在这里。任何帮助将不胜感激!
请在设置新列表后尝试调用listAdapter.notifyDataSetChanged()
。