Search code examples
androidlistviewandroid-arrayadapter

Android Custom adapter image list view with swipe tabs


I am trying to make an Android application with tabs and swipe view, and I have a list view inside one tab, and also I have added an image view to the list and I used a custom adapter to fill data in the list, I have some errors while loading data to the list (but works perfectly fine activity classes ) , lines and errors are written below

 Adapter adapter=new Adapter(this, names , distripton, price, images); 
 error is the constructor Adapter(javafragment,string[],string[])is undefined

My xml page is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_dark"
     >
     <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</RelativeLayout>

And the java fragment class is

package com.example.newlistimage;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class Javaframent extends Fragment {

    ListView lv;
    String[] names={"haseem","hac"};
    String[] distripton={"dis1","dis2"};
    String[] price={"10","20"};
    int[] images={R.drawable.ic_launcher,R.drawable.ic_launcher};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        lv=(ListView) getActivity().findViewById(R.id.listView1);
        Adapter adapter=new Adapter(this, names , distripton, price, images);
        lv.setAdapter(adapter);

        return inflater.inflate(R.layout.java_layout ,container,false);
    }

}

The custom adapter class

package com.example.newlistimage;

  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.ImageView;
  import android.widget.TextView;

public class Adapter extends ArrayAdapter<String>  {

    int[] images={};
    String[] names={};
    String[] prices={};
    String[] discriptions={};
    Context c;
    LayoutInflater inflater; 


        public Adapter(Context context,String[] names,String[] distrion,String[]    price,int[] img) {
        super(context, R.layout.model,names);
            this.c=context;
            this.names=names;
            this.prices=price;
            this.discriptions=distrion;
            this.images=img;

    }

    public class ViewHolder
    {
        TextView nametv;
        TextView discriptiontv;
        TextView pricetv;
        ImageView images;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub


        if(convertView==null)
        {
            inflater=(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.model, null);

        }

        final ViewHolder holder=new ViewHolder();
        holder.nametv=(TextView) convertView.findViewById(R.id.nametv);
        holder.discriptiontv=(TextView) convertView.findViewById(R.id.discriptiontv);
        holder.pricetv=(TextView) convertView.findViewById(R.id.moneytv);
        holder.images=(ImageView) convertView.findViewById(R.id.imageView1);

        holder.images.setImageResource(images[position]);
        holder.nametv.setText(names[position]);
        holder.pricetv.setText(prices[position]);
        holder.discriptiontv.setText(discriptions [position]);

    //  return super.getView(position, convertView, parent);
        return convertView;
    }

}

Solution

  • The problem is here :

    Adapter adapter=new Adapter(this, names , distripton, price, images);
    

    in a Fragment in order to get the context you have to call getActivity() method.

    so your code would be like this :

    Adapter adapter=new Adapter(getActivity(), names , distripton, price, images);
    

    Update :

    i think that NullPointerException is about this line of code :

     lv=(ListView) getActivity().findViewById(R.id.listView1);
    

    when you try to find your listview with the View of your activity while it's not there instead you should create a view and find your listview by that

    i have edited your code this should work :

      @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View v = inflater.inflate(R.layout.java_layout ,container,false);
            lv=(ListView) v.findViewById(R.id.listView1);
            Adapter adapter=new Adapter(this, names , distripton, price, images);
            lv.setAdapter(adapter);
    
            return v;
        }