基础适配器中的getView无法正常工作

时间:2015-05-19 06:55:28

标签: android android-adapter

我正在尝试为Favorites.class创建基础适配器,以便我可以在其中显示项目。 未调用getView方法,并且Activity不会显示任何视图。

BaseAdapter:

public class fYtAdapter extends BaseAdapter {
private List<SearchResult> mfVideoList = null;
private LayoutInflater mfLayoutInflater = null;
private Favorites fActivity = null;

public fYtAdapter(Favorites iActivity){
    fActivity = iActivity;
    mfLayoutInflater = LayoutInflater.from(fActivity);
}

public void setmVideoList(List<SearchResult> mVideoList){
    this.mfVideoList = mVideoList;
}

@Override
public int getCount() {
    return (mfVideoList == null)? (0):(mfVideoList.size());
}

@Override
public Object getItem(int position) {
    return (mfVideoList != null && mfVideoList.size()>position)? (mfVideoList.get(position)):(null);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder mHolder;
    if(convertView != null){
        mHolder = (ViewHolder)convertView.getTag();
    }else{
        mHolder = new ViewHolder();
        convertView = mfLayoutInflater.inflate(R.layout.view_video_item,null);
        mHolder.mVideoThumbnail = (ImageView)convertView.findViewById(R.id.video_thumbnail);
        mHolder.mVideoTitle = (TextView)convertView.findViewById(R.id.video_title);

        convertView.setTag(mHolder);
    }
    //Setting the data
    final SearchResult result = mfVideoList.get(position);
    mHolder.mVideoTitle.setText(result.getSnippet().getTitle());

    //Loading the image
    Picasso.with(fActivity).load(result.getSnippet().getThumbnails().getMedium().getUrl()).into(mHolder.mVideoThumbnail);
    return  convertView;
}

private class ViewHolder{
    private TextView mVideoTitle = null;
    private ImageView mVideoThumbnail = null;
}

}

Favorites.class

public class Favorites extends ActionBarActivity
    implements View.OnClickListener, AdapterView.OnItemClickListener, ServerResponseListener{


private LayoutInflater mLayoutInflater = null;
private ProgressDialog mLoadingDialog = null;
private fYtAdapter mYtadapter = null;
String vidID = null, vidTitle = null, vidThumbnail = null;
ListView mFavLsv;
Bundle extras;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.favorites_layout);
    getSupportActionBar().setTitle("Favorites");
    initializeViews();

    extras = getIntent().getExtras();
    this.vidID = extras.getString("id");
    this.vidTitle = extras.getString("title");
    this.vidThumbnail = extras.getString("thumbnail");
}
public void initializeViews(){
    mLayoutInflater = LayoutInflater.from(this);
    mFavLsv  = (ListView)findViewById(R.id.yt_fav_lst);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

//Not needed in this Activity
@Override
public void onClick(View v) {

}

//To start video
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent videoIntent = YouTubeStandalonePlayer.createVideoIntent(this, AppConstants.KEY, vidID);
    startActivity(videoIntent);
}

@Override
public void prepareRequest(Object... objects) {
    //Parse the response based on type of request
    Integer reqCode = (Integer) objects[0];

    if(reqCode == null || reqCode == 0)
        throw new NullPointerException("Request Code's value is Invalid.");
    String dialogMsg = null;
    switch(reqCode){
        case SEARCH_VIDEO:
            dialogMsg = SEARCH_VIDEO_MSG;
            break;
    }
    if(mLoadingDialog != null && !mLoadingDialog.isShowing())
        mLoadingDialog = ProgressDialog.show(this, DIALOG_TITLE, dialogMsg, true, true);
}

@Override
public void goBackground(Object... objects) {

}

@Override
public void completedRequest(Object... objects) {
    //Dismiss the dialog
    if(mLoadingDialog != null && mLoadingDialog.isShowing())
        mLoadingDialog.dismiss();

    //Parse the response based on type of request
    Integer reqCode = (Integer) objects[0];
    if(reqCode == null || reqCode == 0)
        throw  new NullPointerException("Request Code's value is Invalid.");
    switch (reqCode){
        case SEARCH_VIDEO:
            if(mYtadapter == null){
                mYtadapter = new fYtAdapter(this);
                mYtadapter.setmVideoList((List<SearchResult>)objects[1]);
                mFavLsv.setAdapter(mYtadapter);
            }else{
                mYtadapter.setmVideoList((List<SearchResult>) objects[1]);
                mYtadapter.notifyDataSetChanged();
            }
            break;
    }
}

}

我为另一个Activity获得了另一个基础适配器,它运行正常。 我无法弄清楚为什么这不起作用。 我需要在Favorites.class中显示视图(带缩略图(vidThumbnail)和标题(vidTitle)

*似乎它根本不会进入“completedRequest”。

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。我从这里得到了解决方案.. Link

解决方案:

getCount()未被调用的唯一原因是:

1)setListAdapter返回0

2)您忘记在ListView上调用{{1}}。

3)如果ListView的可见性(或其容器的可见性)为GONE。

答案 1 :(得分:0)

初始化适配器,然后使用.mousedown()

将其设置为initializeViews()方法内的ListView

答案 2 :(得分:0)

你必须再添加一行 -

getView()

你在listview中错过了设置适配器。