ListView
から複数の要素をに入力したいcustom ListView Adapter
。
このチュートリアルを試しました。
例外はスローされませんが、私のには何も表示されませんListView
。
コードは次のとおりです。
Lyrics.java:
package de.lichtenberger.gottschalk.android;
import static de.lichtenberger.gottschalk.android.Constant.DAUER;
import static de.lichtenberger.gottschalk.android.Constant.INTERPRET;
import static de.lichtenberger.gottschalk.android.Constant.TITEL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
public class Lyrics extends Activity {
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>();
private ArrayAdapter<String> aa;
private SAdapter sA;
ProgressDialog dialog;
private ArrayList<HashMap> list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lyrics);
Parse.initialize(this, "hGxasGU6e0WQAOh5JIOGDfvFBKrYyBJKXIzxBfAG", "WsOPsXerpsFjsjekKKbZnnjAHvXy5PQHVQEB8Cqu");
setupUI();
setupOnClickListeners();
new getData().execute();
populateList();
sA = new SAdapter(this, list);
songList.setAdapter(sA);
sA.notifyDataSetChanged();
}
private void populateList() {
list = new ArrayList<HashMap>();
HashMap temp = new HashMap();
for(int i=1; i<= Titelliste.size(); i++){
temp.put(TITEL, Titelliste.get(i).toString());
temp.put(INTERPRET, Interpretliste.get(i).toString());
temp.put(DAUER, Dauerliste.get(i).toString());
list.add(temp);
}
}
private class getData extends AsyncTask<Void, Void, SAdapter>{
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();
}
@Override
protected SAdapter doInBackground(Void... params) {
ParseQuery pq = new ParseQuery("SongDatenbank");
pq.whereExists("Titel");
pq.findInBackground(new FindCallback() {
@Override
public void done(List<ParseObject> liederListe, 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"));
}
x = liederListe.get(0);
Log.d("Parse", x.getString("Titel"));
}else{
Log.d("Parse", "Objektliste nicht empfangen");
}
}
});
return null;
}
protected void onPostExecute(SAdapter result) {
dialog.dismiss();
populateList();
sA = new SAdapter(Lyrics.this, list);
songList.setAdapter(sA);
sA.notifyDataSetChanged();
}
}
private void setupOnClickListeners() {
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent back = new Intent(Lyrics.this, Lagerfeuer_Lieder.class);
startActivity(back);
}
});
}
private void setupUI() {
back = (ImageButton)findViewById(R.id.back);
songList = (ListView)findViewById(R.id.list);
textAnzeige = (TextView)findViewById(R.id.lyricsView);
}
}
私のアダプター:
package de.lichtenberger.gottschalk.android;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import static de.lichtenberger.gottschalk.android.Constant.DAUER;
import static de.lichtenberger.gottschalk.android.Constant.TITEL;
import static de.lichtenberger.gottschalk.android.Constant.INTERPRET;
public class SAdapter extends BaseAdapter
{
public ArrayList<HashMap> list;
Activity activity;
public SAdapter(Activity activity, ArrayList<HashMap> list) {
super();
this.activity = activity;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView titel;
TextView artist;
TextView duration;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.titel = (TextView) convertView.findViewById(R.id.title);
holder.artist = (TextView) convertView.findViewById(R.id.artist);
holder.duration = (TextView) convertView.findViewById(R.id.duration);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
HashMap map = list.get(position);
holder.titel.setText(map.get(TITEL).toString());
holder.artist.setText(map.get(INTERPRET).toString());
holder.duration.setText(map.get(DAUER).toString());
return convertView;
}
}
ここに私のactivity_lyrics.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background" >
<ImageButton
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="0dp"
android:contentDescription="back"
android:id="@+id/back"
android:background="@drawable/back"
android:text="@string/Lagerfeuer"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignTop="@id/back"
android:layout_toRightOf="@id/back"
android:background="@drawable/back2"
android:contentDescription="back2"/>
<ListView
android:layout_below="@id/back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:id="@+id/list"
/>
</RelativeLayout>
そして私のlist_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/guitar_1"/>
</LinearLayout>
<!-- Title Of Song-->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text="Rihanna Love the way lie"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15sp"
android:textStyle="bold"/>
<!-- Artist Name -->
<TextView
android:id="@+id/artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:textColor="#343434"
android:textSize="10sp"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="Just gona stand there and ..." />
<!-- Rightend Duration -->
<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/title"
android:gravity="right"
android:text="5:45"
android:layout_marginRight="5dip"
android:textSize="10sp"
android:textColor="#10bcc9"
android:textStyle="bold"/>
</RelativeLayout>
誰かがそのコードを調べて、私の問題を解決する方法についてアドバイスをくれたら、本当にありがたいです。