setAdapter没有调用getView。

时间:2013-06-17 03:55:53

标签: android

我知道这个问题已被问过好几次,但解决方案一直是针对问题的。问题。因此,即使我尝试遵循他们的所有建议,这些解决方案都没有帮助我。

所以这里。

我有这样的电影活动。请注意,我有一个MoviesAdapter内部课程,应该填充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;
        }               
    }

}

populateListView()方法是从名为AsyncMoviesGet的AsyncTask调用的,其postExecute()看起来像这样。

@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");
        }

    }

您可以在populateListView()方法中查看日志消息。这3个语句被执行。但是,setAdapter()功能似乎无法调用&#34; GetView&#34;功能。

3 个答案:

答案 0 :(得分: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");
    }

}

答案 1 :(得分: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!");
}

答案 2 :(得分: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
}