-1

我正在编写一个 android 应用程序,它应该从 parse.com 数据库中获取歌曲数据并使用自定义 Listview 显示它。

我按照 Nitin 建议的方式尝试了它,但仍然没有显示任何列表视图。

这是新代码:

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;



          }












    }}

我认为问题在于解析数据并将它们作为参数提供给适配器。知道怎么做吗?

如果有人可以查看我的代码并帮助我解决问题,我将不胜感激。

谢谢,保罗

4

3 回答 3

1

我建议你制作一个 Handler 来更新用户界面或使用 Asynctask

从 Thread 更新 UI

检查第一个答案中的链接,它讲述了 doInBackground 方法中的长时间操作,您应该获取数据并在 onPostExecute 中设置适配器

阅读以下代码中的注释。

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);

        }
    }
于 2013-02-19T11:15:13.807 回答
0

检查R.layout.list_row。设计得当与否。即布局的高度和宽度为fill_parent。因为从您的代码来看,一切都很好。

于 2013-02-19T11:18:41.587 回答
-1

Adapter类中没有getCount方法,试试下面的代码

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

此方法指定 getView 自动填充的次数。

............还有一个建议是使用extend BaseAdapter而不是ArrayAdapter<String>

于 2013-02-19T11:13:07.553 回答