0

I know this question has been asked several times, but the solutions have been specific to the askers' problems. Consequently, none of those solutions helped me, even though I tried following all of their suggestions.

So here goes.

I have a movies Activity like this. Notice that I have a MoviesAdapter inner class, that's supposed to populate the moviesDisplay ListView.

package com.example.midtermexam;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MoviesActivity extends Activity {
    public String url;

    public ListView moviesDisplay;
    public static ArrayList<Movie> thisMovies;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movies);
        url=getIntent().getExtras().getString("url");
        moviesDisplay = (ListView)findViewById(R.id.listView1);
        new AsyncMoviesGet(this).execute(url);


    }

    public void populateListView()
    {


        Log.d("listview","adapter created");

        Log.d("listview","Listview declared");
        Log.d("listview","adapter populated");
        moviesDisplay.setAdapter(new MoviesListAdapter());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.movies, menu);
        return true;
    }

    private class MoviesListAdapter extends ArrayAdapter<Movie> {
        public MoviesListAdapter() {
            super(MoviesActivity.this, R.layout.movies_activity_listview, thisMovies);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Make sure we have a view to work with (may have been given null)
            View movieView = convertView;
            if (movieView == null) {
                movieView = getLayoutInflater().inflate(R.layout.movies_activity_listview, parent, false);
            }

            // Find the car to work with.


            return movieView;
        }               
    }

}

The populateListView() method is called from an AsyncTask called AsyncMoviesGet, whose postExecute() looks like this.

@Override
    protected void onPostExecute(ArrayList<Movie> result) {
        if(result != null){

            m.thisMovies = result;
            m.populateListView();
            Log.d("demo", result.toString());
        } else{
            Log.d("demo", "null result");
        }

    }

You can see the Log messages inside the populateListView() method. These 3 statements get executed. However, the setAdapter() function doesn't seem to call the "GetView" function.

4

3 回答 3

0

ArrayAdapter至少添加东西,使用mAdapter.addAll()

private MoviesListAdapter mAdapter;

. . . . . .

@Override
protected void onCreate(Bundle savedInstanceState) {
  . . . . . 
  mAdapter = new MoviesListAdapter();
  moviesDisplay.setAdapter(mAdapter);

}

. . . . .

protected void onPostExecute(ArrayList<Movie> result) {
    if(result != null){

        m.thisMovies = result;

        mAdapter.clear();
        mAdapter.addAll(result);
        mAdapter.notifyDatasetInvalidated();

        Log.d("demo", result.toString());
    } else{
        Log.d("demo", "null result");
    }

}
于 2013-06-17T04:40:35.113 回答
0

您必须向您的自定义适配器类添加一个构造函数,该类接受一个上下文、资源 ID 和一个包含您想要显示的项目的数据结构,在本例中是您的 thisMovies。

public MoviesListAdapter(Context context, int resourceId, 
                         ArrayList<String> viewItems)
{
    super(context, resourceId, viewItems);
}

然后你必须根据你在 onPostExecute() 中收到的结果来构建你的适配器。在此之前,在活动中创建一个实例变量,用于存储适配器。

MoviesListAdapter mMovieAdapter = null;  

之后,通过改变这个来构建它

moviesDisplay.setAdapter(new MoviesListAdapter());

到下面的东西

if(thisMovies != null && thisMovies.size() > 0) { 
    mMovieAdapter = new MoviesListAdapter(getApplicationContext(), R.layout.movies_activity_listview, thisMovies);
    moviesDisplay.setAdapter(mMovieAdapter);
} else { 
    Log.w("MyApplication","onPostExecute() did not return any items!");
}
于 2013-06-17T05:24:47.503 回答
0

您应该通过这种方式初始化和调用适配器............这只是一个例子,您需要根据您的控件对其进行修改:

ListView lv = (ListView) v.findViewById(R.id.listview1);
    ListViewAdapter adapter = new ListViewAdapter(container.getContext(),
            android.R.layout.simple_list_item_1, R.id.textview1);
    adapter.notifyDataSetChanged();
    lv.setAdapter(adapter);

在自定义适配器类中,它的构造函数也应该有这些参数:

public ListViewAdapter(Context context,int resource, int textViewResourceId, List<YourObjectType> items) {
    super(context,resource,textViewResourceId, items);
    this.context1 = context;
    // TODO Auto-generated constructor stub
}
于 2013-06-17T06:01:06.447 回答