Search code examples
javaandroidandroid-fragmentsandroid-arrayadaptercustom-adapter

can we set custom adapter to android fragment(My requirement Strictly needs only custom Adapter not array Adapter)


Iam trying display some data from web services in a Fragment,for this i need to use custom adapter because i am using pojo class to set and get data.

iam new to android and i know custom adapter to activity but i need for fragment.

My requirement is to display like the single item xml file singleitem.xml

<TextView
    android:layout_width="match_parent"
    android:id="@+id/txt1"
    android:textSize="30sp"
    android:gravity="center"
    android:text="ine"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="match_parent"
    android:id="@+id/txt2"
    android:textSize="30sp"
    android:gravity="center"
    android:text="ine"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="match_parent"
    android:id="@+id/txt3"
    android:textSize="30sp"
    android:gravity="center"
    android:text="ine"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="match_parent"
    android:id="@+id/txt4"
    android:textSize="30sp"
    android:gravity="center"
    android:text="ine"
    android:layout_height="wrap_content" />

which i want to display some data which contains 4 different fields in a listview row.

And i want custom adapter to setAdpater() to my Listview.

Is there any procedure to add custom adapter to Fragment or with in array adapter can i assign the values by using pojo class.Suggest me.

this is the custom adapter class iam using in Activity can anyone suggest the possible changes for the adapter class

public class EnrollmentAdapter extends BaseAdapter {
MainActivity activity;
ArrayList<EnrollmentData> data;
LayoutInflater inflater;

public EnrollmentAdapter(MainActivity activity,ArrayList<EnrollmentData> data)
{
    inflater = LayoutInflater.from(activity);
    this.data = data;
}

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

@Override
public Object getItem(int i) {
    return null;
}

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

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    view = inflater.inflate(R.layout.layout_enrollmentsingleitem,null);
    TextView tv1 = (TextView)view.findViewById(R.id.txt1);
    TextView tv2 = (TextView)view.findViewById(R.id.txt2);
    TextView tv3 = (TextView)view.findViewById(R.id.txt3);
    TextView tv4 = (TextView)view.findViewById(R.id.txt4);
    tv1.setText(data.get(i).getReg_num());
    tv2.setText(data.get(i).getW_list_on());
    tv3.setText(data.get(i).getW_list_priority());
    tv4.setText(data.get(i).getElements_to_complete());
    return view;
}

Solution

  • I can solve my above question by using array adapter also meet my requirements

    public class CourseCompleteAdapter extends ArrayAdapter<CourseCompletedInnerData> {
    private Context context;
    int  layout;
    ArrayList<CourseCompletedInnerData> data;
    public CourseCompleteAdapter(Context context, int layout, ArrayList<CourseCompletedInnerData> data) {
        super(context, layout,data);
        this.data = data;
        this.layout = layout;
        this.context =context;
    }
    
    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(layout, parent, false);
        }
        TextView tvScore = (TextView)convertView.findViewById(R.id.txtcoursesinnerscore);
        TextView tvActionstatus = (TextView)convertView.findViewById(R.id.txtcoursesinneractionstatus);
        tvScore.setText(data.get(position).getScore());
        tvActionstatus.setText(data.get(position).getAction_status());
         return convertView;
    }
    

    }