我正在编写一个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;
}
}}
我认为问题在于解析数据并将它们作为参数提供给适配器。 知道怎么做吗?
如果有人能查看我的代码并帮助我解决问题,我将非常感激。
谢谢,保罗
答案 0 :(得分:1)
我建议你让Handler更新用户界面或使用Asynctask
检查第一个答案中的链接,它告诉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);
}
}
答案 1 :(得分:0)
检查R.layout.list_row
。是否设计得当。即布局的高度和宽度为fill_parent。因为你的代码一切都很好。
答案 2 :(得分:-1)
Adapter类中没有getCount
方法,请尝试以下代码
@Override
public int getCount()
{
return x.size();// number of datas in the list .
}
此方法指定自动填充getView的次数。
............还有一个建议是使用extend BaseAdapter
代替ArrayAdapter<String>