-1

i'm writing an android app that should fetch song data from a parse.com database and display it with a custom Listview.

I tried it the way Nitin suggested but still dont get any listview displayed.

Here is the new code:

package de.android;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseQuery;


public class Lyrics extends ListActivity {



    ImageButton back;
    ListView songList;
    TextView textAnzeige;
    private ArrayList<String> Titelliste = new ArrayList<String>();
    private ArrayList<String> Interpretliste = new ArrayList<String>();
    private ArrayList<String> Dauerliste = new ArrayList<String>();

    ProgressDialog dialog;



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

            Parse.initialize(this, "hGxasGU6e0WQAOh5JIOGDfvFBKrYyBJKXIzxBfAG", "WsOPsXerpsFjsjekKKbZnnjAHvXy5PQHVQEB8Cqu");




            initDataToView();


        }

    private void initDataToView() {


        new getData().execute();
        songList = (ListView)findViewById(android.R.id.list);

    }

    private class getData extends AsyncTask<Void, Void, SongAdapter>{

        ProgressDialog dialog;
        @Override
        protected void onPreExecute() {
            dialog = new ProgressDialog(Lyrics.this);
            dialog.setMessage("Please wait, while loading!");
            dialog.setIndeterminate(true);
            dialog.setCancelable(false);
            dialog.show();
            Log.d("Parse", "PreExecute");
        }

        @Override
        protected SongAdapter doInBackground(Void... params) {



            ParseQuery pq = new ParseQuery("SongDatenbank");
            pq.whereExists("Titel");
            pq.findInBackground(new FindCallback() {

                @Override
                public void done(List<ParseObject> liederListe,
                        com.parse.ParseException e) {
                    if(e==null){
                        Log.d("Parse", "Objektliste empfangen");

                            ParseObject x;

                        for(int i=0;i<liederListe.size();i++){
                            x = liederListe.get(i);
                            Titelliste.add(x.getString("Titel"));
                            Dauerliste.add(x.getString("Dauer"));
                            Interpretliste.add(x.getString("Interpret"));
                        }
                        initDataToView(); 

                        x = liederListe.get(0);

                        Log.d("Parse", x.getString("Titel"));
                        Log.d("Parse", x.getString("Dauer"));
                        Log.d("Parse", x.getString("Interpret"));


                    }else{

                        Log.d("Parse", "Objektliste nicht empfangen");
                    }
                }


            });



            return null;
        }

        protected void onPostExecute(SongAdapter result) {

            songList.setAdapter(result);

            dialog.dismiss();
            Log.d("Parse", "Postexecute");
        }


    }





    public class SongAdapter extends ArrayAdapter<String> {
         private final Context context;
         private final ArrayList<String> valuesTitel;
         private final ArrayList<String> valuesInterpret;
         private final ArrayList<String> valuesDauer;


          public SongAdapter(Context context, ArrayList<String> valuesTitel, ArrayList<String> valuesInterpret, ArrayList<String> valuesDauer) {
            super(context, R.layout.list_row);
            this.context = context;
            this.valuesTitel = valuesTitel;
            this.valuesInterpret = valuesInterpret;
            this.valuesDauer = valuesDauer;
          }



          public View getView(int position, View convertView, ViewGroup parent){
              LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

              View rowView = inflater.inflate(R.layout.list_row, parent, false);
              ImageView imageView = (ImageView)rowView.findViewById(R.id.list_image);
              TextView titelText = (TextView)rowView.findViewById(R.id.title);
              TextView artistText = (TextView)rowView.findViewById(R.id.artist);
              TextView duration = (TextView)rowView.findViewById(R.id.duration);



              titelText.setText(valuesTitel.get(position));
              artistText.setText(valuesInterpret.get(position));
              duration.setText(valuesDauer.get(position));
              Log.d("Parse", valuesTitel.get(position));
              return rowView;



          }












    }}

I think the problem is in parsing the data and give them to the adapter as parameter. Any idea how to that?

I would really appreciate it if someone could look through my code and help me with solving the problem.

thanks, Paul

Igl3
  • 4,900
  • 5
  • 35
  • 69
Paul Lich
  • 174
  • 2
  • 14

3 Answers3

1

I suggest you to make a Handler to update the user interface or use Asynctask

Update UI from Thread

check the link in the first answer it tells about long operation in doInBackground method you should fetch the data and in onPostExecute set the adapter

read the comments in the following code.

private class PrepareAdapter1 extends AsyncTask<Void,Void,ContactsListCursorAdapter > {
    ProgressDialog dialog;
    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(viewContacts.this);
        dialog.setMessage(getString(R.string.please_wait_while_loading));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }
    /* (non-Javadoc)
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected ContactsListCursorAdapter doInBackground(Void... params) {

        //do here parsing   

        return adapter1;
    }



     protected void onPostExecute(ContactsListCursorAdapter result) {
           dialog.dismiss();
    //Set Adapter
            list.setAdapter(result);

        }
    }
Community
  • 1
  • 1
Nitin
  • 1,966
  • 4
  • 22
  • 53
  • I thought about this, but how can I change or refer to the elements of a listView element, defined in list_row.xml? – Paul Lich Feb 19 '13 at 11:24
  • So I let my SongAdapter class extend AsyncTask? – Paul Lich Feb 19 '13 at 11:41
  • no it does not mean that,your adapter will be the same but data fetch from network will be in different class which extends the AsyncTask task and override its methods – Nitin Feb 19 '13 at 11:46
0

Check R.layout.list_row. that is properly designed or not. i.e the layout is having the height and width as fill_parent. because from your code everything looks fine.

Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47
-1

there is no getCount method in the Adapter class , try the following code

    @Override
    public int getCount() 
    {
        return x.size();// number of datas in the list .
    }

this method specifies the number of times the getView to be populated automatically .

............ One more suggession is that use extend BaseAdapter instead of ArrayAdapter<String>

VIGNESH
  • 2,023
  • 8
  • 31
  • 46