1

我想用 aListView中的多个元素填充 a custom ListView Adapter

我试过这个教程

不会引发异常,但它不会在我的ListView.

这是代码:

歌词.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>

如果有人可以查看该代码并可以给我一些如何解决我的问题的建议,我将不胜感激。

4

4 回答 4

2

用于onPostExecute在 AsyncTask 的方法执行完成后为 ListView 设置 Adapter,doInBackground而不是在执行 AsyncTask 后立即调用它,如下所示:

 @Override
 protected void onPostExecute(SAdapter result) {

            dialog.dismiss();

            // populate Listview here...
            populateList();
            sA = new SAdapter(Lyrics.this, list);
            songList.setAdapter(sA);
            sA.notifyDataSetChanged();
        }
于 2013-02-25T10:11:39.033 回答
0

可能是一个愚蠢的答案,但是

HashMap 是一种原始类型。对泛型 HashMap 的引用应该被参数化

尝试:

HashMap <String, String> map

在适配器和主类主体中更改它。同样的问题发生在我身上,但不知道为什么,我看到的唯一区别是 hashmap 引用。

于 2014-02-18T03:03:41.627 回答
0

你可以使用这样的东西:

@Override
protected void onPostExecute(List<Contact> result) {            
    super.onPostExecute(result);
    dialog.dismiss();
    adpt.setItemList(result);
    adpt.notifyDataSetChanged();
}

@Override
protected void onPreExecute() {        
    super.onPreExecute();
    dialog.setMessage("Downloading contacts...");
    dialog.show();            
}

可以看这里(Android Async ListView–JEE And RestFul)

于 2013-02-26T13:47:06.893 回答
0
listview= (ListView)findViewById(R.id.listview);
listview.setOnItemSelectedListener(this);
String days[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
ArrayAdapter<String> newAdpt= new ArrayAdapter<String>(this,android.R.layout.simple_listview_item,days);
newAdpt.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
listview.setAdapter(newAdpt);
于 2013-02-25T10:17:41.990 回答