-1

I'm trying to create base adapter for Favorites.class so I can display items in it. The getView method is not being called and the Activity won't show any view.

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;
    }
}

}

I got another base adapter for another Activity and it is working fine. I can't figure out why this is not working in here. I need to display the view (with thumbnail(vidThumbnail) and title(vidTitle) in Favorites.class

*It seems that it won't got into "completedRequest" at all.

3 Answers3

1

I faced this same issue. And I got the solution from here.. Link

Solution:

The only reasons getView() is not called are:

1) getCount() returns 0

2) You forget to call setListAdapter on the ListView.

3) If the ListView's visibility (or its container's visibility) is GONE.

Community
  • 1
  • 1
Vijay
  • 2,270
  • 3
  • 18
  • 33
0

Initialize your adapter and then set it to your ListView inside your initializeViews() method using mFavLsv.setAdapter(youradapter);

Bidhan
  • 10,089
  • 3
  • 34
  • 46
0

you have to add one more line -

mFavLsv.setAdapter(mYtadapter );

You missed set adapter in listview.

Ravi Bhandari
  • 4,319
  • 8
  • 36
  • 66