Android dialog bar during listview - android

i am trying to add items to list and i want to start dialog bar before the start of the loop which add items to list and dismiss dialog after loop finish
ProgressDialog pd;
pd = new ProgressDialog(SearchList.this);
pd.setTitle("Please wait");
pd.setMessage("loading String.. ");
pd.show();
new Thread(new Runnable() {
#Override
public void run() {
try {
for (int i = 0; i <500000; i++) {
list.add("String "+i);
}
pd.dismiss();
} catch (Exception e) {
}
}
}).start();
m_adapter = new Adapter(this, R.layout.itemview, list);
lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(m_adapter);

Use AsyncTask to show/hide ProgressDialog as follows:
protected void onPreExecute() {
dialog=ProgressDialog.show(mContext, "", "Your message");
super.onPreExecute();
}
protected void onPostExecute(Void result) {
if(dialog!=null)
{
dialog.dismiss();
}
}
Use doInBackground function to do your looping:
protected Void doInBackground(String... params) {}
See example how to use AsyncTask

Try this way,hope this will help you to solve your problem.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getList(this,new AddItemFinsihListener() {
#Override
public void onFinish(Object object) {
ArrayList<String> list = (ArrayList<String>) object;
for (String row : list){
System.out.println(row);
}
// try to set your list adapter here
}
});
}
public void getList(final Context context, final AddItemFinsihListener target) {
final ProgressDialog pd = new ProgressDialog(context);
new AsyncTask<Void,Void,Object>(){
#Override
protected void onPreExecute() {
super.onPreExecute();
pd.setTitle("Please wait");
pd.setMessage("Page is loading..");
pd.show();
}
#Override
protected Object doInBackground(Void... params) {
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i <500000; i++) {
list.add("String "+i);
}
return list;
}
#Override
protected void onPostExecute(Object object) {
super.onPostExecute(object);
pd.dismiss();
target.onFinish(object);
}
}.execute();
}
interface AddItemFinsihListener{
public void onFinish(Object object);
}

You need to add the Array first before you initialized the adapter and add it to the ListView.
try this:
ProgressDialog pd;
pd = new ProgressDialog(SearchList.this);
pd.setTitle("Please wait");
pd.setMessage("loading String.. ");
pd.show();
new Thread(new Runnable() {
#Override
public void run() {
try {
for (int i = 0; i <500000; i++) {
list.add("String "+i);
}
SearchList.this.runOnUiThread(new Runnable() {
#Override
public void run() {
m_adapter = new Adapter(this, R.layout.itemview, list);
lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(m_adapter);
pd.dismiss();
}
});
} catch (Exception e) {
}
}
}).start();

Related

progressdialog going behind a dialog

I have created dialog and it inflates xml which gets updated with the info .But the progress dialog is shown behind the dialog which pops up .How do I show those progressdialog on top of dialog with inflated xml.
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog();
}
public class loadasync extends AsyncTask<Void, Void, JSONObject> {
ProgressDialog progressDialog ;
#Override
protected JSONObject doInBackground(Void... params) {
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("loading");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
});
}
#Override
protected void onPostExecute(JSONObject result) {
runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog.dismiss();
}
});
}
}
public void dialog() {
dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialogxml);
loadasync loadasyncdata=new loadasync();
loadasyncdata.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
dialog.show();
}
I cannot see that you are using the setProgressStyle() in your code. That is:
progessDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
for instance.
So in your code try the following:
public void run() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("loading");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
Something to consider : OnPostExecute and onPreExecute are both run on the UI thread, so you can remove the runOnUIThread stuff, just do
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("loading");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
and respectively in onPostExecute
protected void onPostExecute(JSONObject result) {
progressDialog.dismiss();
}
Should maybe be a comment, but the reputation ...

WindowManager$BadTokenException near dialog.show in AsyncTask android

How to show a dialog box in AsyncTask. Getting BadToketException in dialog.show();
I tried many ways but I could not solve it.
Also tried to pass context to the dialog box in different ways, but it is giving me the same result.
public class RetriveStock extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
message = client.clientReceive(1); // I get data here.
return null;
}
#Override
protected void onCancelled() {
super.onCancelled();
}
#Override
protected void onPostExecute(Void result) {
if (message.contains("AlertExecuted:")) {
final Dialog dialog = new Dialog(CreateAlert.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.display_dialog);// Dialog layout
TextView dialogText = (TextView) dialog.findViewById(R.id.digMsg);
dialogText.setText("Alert Executed!");
Button ok = (Button) dialog.findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
try {
dialog.show(); //WindowManager$BadTokenException
} catch (Exception e) {
e.printStackTrace();
}
}
super.onPostExecute(result);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
Please help.
protected void onPreExecute() {
// TODO Auto-generated method stub
// progressDialog = ProgressDialog.show(this, "", "loading news content");
progressDialog = new ProgressDialog(context , AlertDialog.THEME_HOLO_LIGHT);
progressDialog.setMessage(""+getString(R.string.laodnews));
progressDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.animate));
progressDialog.setCancelable(false);
progressDialog.show();
}
start dailoge in pre execute and stop in onpostexecute..
is CreateAlert registered activity in manifest..if not then you have to pass registered activity context

How to refresh ListView in AsyncTask?

I am using AsyncTask and fetch twitter feed asynchronously to display in a listView. It works fine Then for refreshing the listview I put the asyncTask call into a Runnable's Run() to load it after a specific interval. The feed loads after specific time interval but the listview is not updating. I use notifyDataSetChanged() inside the asyncTask but it is not working.
This is my trying code:
public class Twitter7FeedActivity extends Activity {
LazyAdapter adapter;
ListView list;
JSONObject jsonObj = null;
JSONArray jsonArray = null;
#SuppressLint("SimpleDateFormat")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_twitter7_feed);
callAsynchronousTask();
}
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
new GetFeedTask().execute(CommonUtils.BEARER_TOKEN,
CommonUtils.URL);
} catch (Exception e) {
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 50000);
}
protected class GetFeedTask extends AsyncTask<String, Void, String> {
private ProgressDialog dialog = new ProgressDialog(
Twitter7FeedActivity.this);
protected void onPreExecute() {
this.dialog.setMessage("Please wait");
this.dialog.show();
}
#Override
protected String doInBackground(String... params) {
// related code
}
#Override
protected void onPostExecute(String jsonText) {
// related code
adapter = new LazyAdapter(Twitter7FeedActivity.this,
// tweets);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
Have you tried moving this line:
list = (ListView) findViewById(R.id.list);
after your setContentView in your onCreate method?
// related code adapter = new LazyAdapter(Twitter7FeedActivity.this, tweets);
uncomment this code which is in onPostExecute() method. I think it may help you out. thanks...
You can do the work mentioned in your question using only a Handler. Something like this:
public class Twitter7FeedActivity extends Activity {
LazyAdapter adapter;
ListView list;
JSONObject jsonObj = null;
JSONArray jsonArray = null;
#SuppressLint("SimpleDateFormat")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_twitter7_feed);
callAsynchronousTask();
}
public void callAsynchronousTask() {
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
ProgressDialog dialog;
public void run() {
try {
//Show your dialog here
runOnUiThread(new Runnable() {
public void run() {
runnable.this.dialog = new ProgressDialog( Twitter7FeedActivity.this);
runnable.this.dialog.setMessage("Please wait");
runnable.this.dialog.show();
}
});
//Do your AsyncTask's doInBackground work here
//Update the UI with runOnUiThread or using the Ui threa handler
runOnUiThread(new Runnable() {
public void run() {
// related code adapter = new LazyAdapter(Twitter7FeedActivity.this, tweets);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
//Then post the thread to excecute after 50000 milliseconds.
handler.postDelayed(runnable, 50000);
} catch (Exception e)
{ }
}
};
handler.post(runnable);
}
}
This way you avoid using an AsyncTask inside a TimerTask.

Android: ProgressDialog and Show Data from XML Parsing Problem

I am parsing an XML from a URL and showing it. I want to show ProgressDialog while my XML Parser Method parse the XML and get it ready to show. The problem I am facing is that no data is shown after ProgressDialog rather it shows blank screen.
Here is my code:
ArrayList<XMLItem> items= new ArrayList<XMLItem>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mydata);
adapter= new MyAdapter(this, R.layout.customcell, items);
listView= (ListView) findViewById(id.ListView01);
listView.setAdapter(adapter);
final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
Thread thread=new Thread(new Runnable(){
public void run(){
doTheAutoRefresh();
runOnUiThread(new Runnable(){
public void run() {
if(dialog.isShowing())
dialog.dismiss();
adapter.notifyDataSetChanged(); //this line throws an exception and if i comment this line, blank screen is shown
}
});
}
});
thread.start();
}
private void doTheAutoRefresh() {
handler.postDelayed(new Runnable() {
public void run(){
loadData(); // this is where you put your refresh code
doTheAutoRefresh();
}
}, 15000);
}
private void loadData(){
Document doc;
try {
doc = XMLReader.parseURL("my url to parse xml");
NodeList nl = doc.getElementsByTagName("l");
for(int i=0; i<nl.getLength(); i++){
NamedNodeMap np = nl.item(i).getAttributes();
String t= np.getNamedItem("htn").getNodeValue();
String sT= np.getNamedItem("atn").getNodeValue();
XMLItem item= new XMLItem(t, sT);
items.add(item);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This code shows ProgressDialog perfectly, but after progress dialog, it shows blank screen. Please help me sorting out this problem.
Thanks
private class xyz extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(tranning.this);
#Override
protected void onPreExecute() {
this.dialog.setMessage("Please Wait...");
this.dialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// do your load data function part here.
// just populate the data structures that are necessary
// for the adapter.
return null;
}
#Override
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
// notify adapter here.
adapter.notifyDataSetChanged();
refreshTimer(); // to do the same thing after 15 seconds.
}
}
}
now call the asycTask for the first time in your onCreate() probably.
new xyz().execute()
make a function to put a timer for refresh, we do that by using postDelayed.
private void refreshTimer(){
handler.postDelayed( runner , 15000);
}
private Runnable runner = new Runnable(){
public void run(){
new xyz().execute();
}
}
i think you need to implement AsyncTask ::
private class xyz extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(tranning.this);
#Override
protected void onPreExecute() {
this.dialog.setMessage("Please Wait...");
this.dialog.show();
// put your code which preload with processDialog
}
#Override
protected Void doInBackground(Void... arg0) {
// put your code here
return null;
}
#Override
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
and use this in main ::
new xyz().execute();
add a function showData() and call it after dismissing the dialog
ArrayList<XMLItem> items= new ArrayList<XMLItem>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mydata);
listView= (ListView) findViewById(id.ListView01);
final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
Thread thread=new Thread(new Runnable(){
public void run(){
loadData();
runOnUiThread(new Runnable(){
public void run() {
if(dialog.isShowing()){
dialog.dismiss();
showData(); //////////////////////////////////////
}
}
});
}
});
thread.start();
}
private void showData(){
adapter= new MyAdapter(this, R.layout.customcell, items);
listView.setAdapter(adapter);
}
private void loadData(){
Document doc;
try {
doc = XMLReader.parseURL("my url to parse xml");
NodeList nl = doc.getElementsByTagName("l");
for(int i=0; i<nl.getLength(); i++){
NamedNodeMap np = nl.item(i).getAttributes();
String t= np.getNamedItem("htn").getNodeValue();
String sT= np.getNamedItem("atn").getNodeValue();
XMLItem item= new XMLItem(t, sT);
items.add(item);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

progress dialog doesnt work with custom list adapter

progress dialog doesnt work while listing data with listAdapter
i think its all about sending activity to myAdapter
final myAdapter adapter = new myAdapter(rss_list_activity.this, liste);
final ProgressDialog dialog = ProgressDialog.show(this, "Title",
"Message", true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
Thread checkUpdate = new Thread() {
public void run() {
getListFromXml();
lv.setAdapter(adapter);
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
here's some information on threading, i would use an AsyncTask if I were you:
http://developer.android.com/resources/articles/painless-threading.html
Something similar to this should work:
LoadListTask tsk = new LoadListTask();
tsk.execute((Void) null);
with this somewhere
private class LoadListTask extends AsyncTask<Void, Void, List<theType>>
{
final ProgressDialog dialog;
#Override
protected void onPreExecute()
{
super.onPreExecute();
dialog = ProgressDialog.show(MainClass.this, "Title", "Message", true);
}
#Override
protected void onPostExecute(List<theType> result)
{
super.onPostExecute(result);
final myAdapter adapter = new myAdapter(rss_list_activity.this, result);
lv.setAdapter(adapter);
dialog.dismiss();
}
#Override
protected List<theType> doInBackground(Void... params)
{
return getListFromXml();
}
}
use dialog.show(); to show dialog. you are directly dismissing the dialog using dialog.dismiss();

Categories

Resources