Listview getView method not getting called

635 views Asked by At

This type of question has been asked so many times but none of the solutions on SO are working for me so I am asking again for help. I have a custom ListView which implemented pull-to-refresh and was working fine until I decided to use the new SwipeRefreshLayout and now my custom adapter getView doesn't get called.

Here is my xml layout:

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@+id/myList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#3D3C3A"
    android:background="@color/Red"
    android:dividerHeight="1dp"/>

</android.support.v4.widget.SwipeRefreshLayout>

And this is my fragment:

public class MenuFragment extends Fragment implements OnItemClickListener{

View v;
String itemPostion;
ListView listView;
SwipeRefreshLayout mSwipeRefreshLayout;
List<RssItem> itemx = new ArrayList<RssItem>();
private ProgressBar pd;
private DisplayMetrics metrics;
RssAdapter adapter = null;

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

    startService();

    listView.setFadingEdgeLength(0);
    listView.setOnItemClickListener(this);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startService();
                    mSwipeRefreshLayout.setRefreshing(false);
                }
            }, 5000);
        }
    });
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

   if (v == null) {
                View v = inflater.inflate(R.layout.fragment_layout, container, false);
                listView = (ListView) v.findViewById(R.id.myList);
                mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_refresh);
                mSwipeRefreshLayout.setColorSchemeResources(R.color.orange, R.color.green, R.color.blue);
                //pd = (ProgressBar) v.findViewById(R.id.progress);
                //pd.setVisibility(View.VISIBLE);
                startService();
            }
            else {
                //ViewGroup parent = (ViewGroup) v.getParent();
                //parent.removeView(v);
            }
            return v;
}

private void startService() {
    Intent intent = new Intent(getActivity(), RssService.class);
    intent.putExtra("mypostion", itemPostion);
    intent.putExtra(RssService.RECEIVER, resultReceiver);
    getActivity().startService(intent);
}

private final ResultReceiver resultReceiver = new ResultReceiver(new Handler()) {
    @SuppressWarnings("unchecked")
    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        List<RssItem> items = (List<RssItem>) resultData.getSerializable(RssService.ITEMS);

        Log.e("Items", String.valueOf(items.size()));
        if (items.size() > 0) {
            adapter = new RssAdapter(getActivity(), items, metrics);
            itemx = items;
            listView.setAdapter(adapter);
        }
        else {
            Toast.makeText(getActivity(), "Error occurred while downloading the feed.",Toast.LENGTH_LONG).show();
        }
    };
};

This is my custom adapter

public class RssAdapter extends BaseAdapter {

    private final List<RssItem> items;
    private final Context context;
    private DisplayMetrics metricsy;
    private String website, title, newImageURL;

    public RssAdapter(Context contextx, List<RssItem> itemsx, DisplayMetrics metricsx) {
        super();
        items = itemsx;
        metricsy = metricsx;
        context = contextx;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.rss_item, null);
            holder = new ViewHolder();
            holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
            holder.itemSource = (TextView) convertView.findViewById(R.id.itemSource);
            holder.imageicon = (ImageView) convertView.findViewById(R.id.imgicon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Animation animation = AnimationUtils.loadAnimation(context, R.anim.push_left_in);

        String source = items.get(position).getLink();
        String myDesc = items.get(position).getDesc();
        String mytitle = items.get(position).getTitle();
        String myImage = items.get(position).getMlink();
        Log.e("Link", source);
        convertView.startAnimation(animation);
        return convertView;
    }
}

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    RssItem item = itemx.get(position);
    Uri uri = Uri.parse(item.getLink());

    Intent i = new Intent(getActivity(),WebViewActivity.class);
    i.putExtra("mystring",uri.toString());
    startActivity(i);
}

@Override
public void onResume() {
    super.onResume();
}

static class ViewHolder {
        TextView itemTitle;
        TextView itemSource;
        ImageView imageicon;
}
}

The problem is getView method of the RssAdapter never get called even though getCount() returned the correct number of item. What am doing wrong here. Thanks for your help

1

There are 1 answers

0
The Original Android On BEST ANSWER

In onCreateView(), it is not returning the root view, it is returning null because the class member v is null initially and is not modified.

The existing code, change FROM:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

   if (v == null) {
      View v = inflater...
   }

   return v;

Suggested code, change TO (or similar to this):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater...
       ...
       return view;

Notes:

  • On the suggested code, the if check is removed, no need for it.
  • Returning the root view with return view.