1

I was using the code from the doInBackground section to load a custom listview from a database and now that I have added it to an AsynchTask it shows nothing when the dialog has been dismissed. Does anyone know what I am doing wrong so that it will display my list when the progress bar dismisses again:

private class LoadList extends AsyncTask<String, UserRecord, JSONArray> {
     protected JSONArray doInBackground(String... link) {
        //http post
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://wallpaperapp.x10.mx/new.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }catch(Exception e){
            Log.e("log_tag", "Error in http connection"+e.toString());
        }

        //convert response to string
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");

            String line="0";
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
        }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }

        try{
            jArray = new JSONArray(result);
            JSONObject json_data=null;

            displayname = new String[jArray.length()];
            song_name = new String[jArray.length()];
            artist = new String[jArray.length()];
            description = new String[jArray.length()];
            genre = new String[jArray.length()];
            custom_genre = new String[jArray.length()];
            album = new String[jArray.length()];
            timestamp = new String[jArray.length()];
            song_id  = new int[jArray.length()];
            avatar = new String[jArray.length()];
            drawable = new Drawable[jArray.length()];
            test_rating = new Float[jArray.length()];
            songurl = new String[jArray.length()];

            for(int i=0;i<jArray.length();i++){
                json_data = jArray.getJSONObject(i);
                song_id[i]=json_data.getInt("id");
                song_name[i]=json_data.getString("songname");
                artist[i]=json_data.getString("artist");
                displayname[i]=json_data.getString("displayname");
                description[i]=json_data.getString("description");
                genre[i]=json_data.getString("genre");
                custom_genre[i]=json_data.getString("customgenre");
                album[i]=json_data.getString("album");
                timestamp[i]=json_data.getString("format");
                avatar[i]=json_data.getString("image_url");
                songurl[i]=json_data.getString("song_url");
                drawable[i] = LoadImageFromWebOperations(avatar[i]);
                test_rating[i] = (float) json_data.getDouble("rating");
                user5  = new UserRecord(genre[i], displayname[i], timestamp[i], drawable[i], test_rating[i], songurl[i]);

                publishProgress(user5);

            }
        }
        catch(JSONException e1){
            Toast.makeText(getBaseContext(), "No results found." ,Toast.LENGTH_LONG).show();

        } catch (ParseException e1) {
            e1.printStackTrace();
        }           

         return null;
     }

     protected void onProgressUpdate(UserRecord... progress) {
         users.add(user5);

     }

     protected void onPostExecute(JSONArray jArray) {
         dialog.dismiss();
     }

     protected void onPreExecute(){
         dialog = ProgressDialog.show(mainmenu.this, "", 
                 "Loading. Please wait...", true);
     }
}

Here is my onCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newsmain);      

    nameValuePairs = new ArrayList<NameValuePair>();
    users = new ArrayList<UserRecord>();                

    ListView listView = (ListView) findViewById(R.id.ListViewId);

    songname = (TextView) findViewById(R.id.tvTrack);
    songimage = (ImageView) findViewById (R.id.imageView1);
    playbutton = (Button) findViewById (R.id.playbutton1);
    downloadbutton = (Button) findViewById (R.id.downloadbutton3);
    listView.setAdapter(new UserItemAdapter(this, android.R.layout.simple_list_item_1, users));
    flipper = (ViewFlipper) findViewById(R.id.tab2); 
    //when a view is displayed
    flipper.setInAnimation(this,android.R.anim.fade_in);
    //when a view disappears
    flipper.setOutAnimation(this, android.R.anim.fade_out);

    screenWidth = getResources().getDisplayMetrics().widthPixels;
    new LoadList().execute();
            }

The UserRecord that is getting published:

public class UserRecord {
    public String genrez;
    public String displaynamez;
    public String timestampz;
    public Drawable image_urlz;
    public Float test_ratingz;
    public String songurlz;

    public UserRecord(String genrez, String displaynamez, String timestampz, Drawable image_urlz, Float test_rating, String songurlz) {
        this.genrez = genrez;
        this.displaynamez = displaynamez;
        this.timestampz = timestampz;
        this.image_urlz = image_urlz;
        this.test_ratingz = test_rating;
        this.songurlz = songurlz;

    }
}

2 Answers 2

3

Adding this to my onPostExecute worked! I am so happy I figured it out.. thanks

 listView.setAdapter(new UserItemAdapter(mainmenu.this, android.R.layout.simple_list_item_1, users));
Sign up to request clarification or add additional context in comments.

Comments

0
 @Override
protected void onPostExecute(String result) {
    dialog.dismiss();

}

please put listview code in this function, means binding data to listview

2 Comments

Thank you, I will try in a little bit here and try that, sounds like the right answer. I think it is in onCreate right now..
Hey I tried to show more information.. my screen is still blank after the dialog closes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.