Reputation: 27
I want to try and populate a listview in android but i dont want the list to show until the list is completed. I have made a way that works but its very badly coded and want to change it.
Here is my code:
public class Admin extends ListActivity
{
static java.sql.Connection con;
static java.sql.Statement stmt = null;
static java.sql.ResultSet rs = null;
String user = null;
//public final static String Username = "com.example.securelogin.Username";
Button EditButton;
// Progress Dialog
private ProgressDialog pDialog;
final Vector<String> users = new Vector<String>();
boolean ready = false;
int counter = 0;
@SuppressWarnings("unchecked")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
EditButton = (Button) findViewById(R.id.button2);
EditButton.setOnClickListener(onUpdate);
counter++;
for(int i = 0; i < 1; i++)
{
new Listview().execute();
while(!ready)
{
}
}
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, users));
}
public void onListItemClick(ListView parent, View v,int position, long id)
{
super.onListItemClick(parent, v, position, id);
user = users.get(position);
Toast.makeText(this, "You have selected " + users.get(position), Toast.LENGTH_SHORT).show();
}
class Listview extends AsyncTask<Vector<String>, String, Vector<String>>
{
boolean failure = false;
@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(Admin.this);
pDialog.setMessage("Gathering Data");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Vector<String> doInBackground(Vector<String>... params)
{
try
{
rs = Database.ListUsers();
System.out.println("Executed statement");
while(rs.next())
{
String Description = rs.getString(1);
users.add(Description);
System.out.println("In the while loop");
}
System.out.println("you got past the statement");
ready = true;
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return users;
}
protected void onPostExecute(Vector<String> names)
{
// dismiss the dialog once product deleted
pDialog.dismiss();
}
}
private View.OnClickListener onUpdate= new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(user == null)
{
return;
}
else
{
Intent i = new Intent(Admin.this, EditUser.class);
i.putExtra("Username",user );
System.out.println(user);
finish();
startActivity(i);
}
}
};
}
Upvotes: 0
Views: 276
Reputation: 2866
Use an Async task to populate the data and in the onPostExecute method display the listview. While the list is loading you can use the empty view to show a loading animation or text. Could be anything you want, depends on the app your trying to build. Could even be a huge action button if the list is actually empty.(Like the Google Keep app)
Edit :
Populate data into "users" Arraylist and call the following in onPostExecute
adapter.notifyDatasetChanged();
That should refresh the listview.
Upvotes: 0
Reputation: 1215
I suggest you to use ListFragment.
If you check the documentation there is a setListShown(boolean shown) and a setListShownNoAnimation(boolean shown). Those methods are quite handy because the layout already associated with the ListFragment already has an indeterminate ProgressBar in the center of the fragment and allows you to set an empty text as well.
Basically, you can start your loading (while the user is watching the progress onto the screen) and when it finishes you can call one of the two methods above and show the result.
EDIT: Another reason to use the fragment is because if you want to support tablets and follow the Android design rules it becomes more easy through them, making your components more reusable.
If you need to support older versions of the platform, use the ListFragment provided by the support library.
Upvotes: 1