0

I am trying to implement a listview in my fragment, however I am getting error in my fragment class and adapter.

My fragment xml:

        <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/divider2">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/MyAccountList"
            android:layout_below="@+id/divider2"/>

    </LinearLayout>

My list viewholder:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- icon -->
    <ImageView
        android:id="@+id/option_icon"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:layout_marginBottom="@dimen/activity_horizontal_margin"
        android:padding="@dimen/activity_horizontal_margin"
        android:contentDescription="icon" />


    <!-- title -->
    <TextView
        android:id="@+id/option_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/option_icon"
        android:textColor="@android:color/darker_gray"
        android:padding="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:layout_alignTop="@+id/option_icon"
        android:layout_centerVertical="true"/>

    </RelativeLayout>

Adapter:

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class MyAccount_Adapter extends BaseAdapter {


private final Activity context;
private final String[] option_title;
private final Integer[] option_icon;


public MyAccount_Adapter(Activity context, String[] option_title, Integer[] option_icon) {
    //super(context, R.layout.myaccount_options_viewholder, option_title);
    // TODO Auto-generated constructor stub

    this.context=context;
    this.option_title=option_title;
    this.option_icon=option_icon;
}

@Override
public int getCount() {
    return option_title.length; //returns total of items in the array
}

@Override
public Object getItem(int position) {
    return option_title[position]; //returns item at the specified position
}

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

public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.myaccount_options_viewholder, null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.option_title);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.profile_icon);

    txtTitle.setText(option_title[position]);
    imageView.setImageResource(option_icon[position]);
    return rowView;

};
}

And my fragment:

public class MyAccount extends Fragment {

ListView listView;
String[] optionTitle ={
        "booking record",
        "comments",
        "language",
};

Integer[] option_icon={

        R.drawable.booking_record_02,
        R.drawable.comment_record_02,
        R.drawable.language_02,
};


public MyAccount() {
}

public static MyAccount newInstance(String text){
    Bundle args = new Bundle();
}

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

final View rootView = inflater.inflate(R.layout.my_account, container, false);

    MyAccount_Adapter adapter=new MyAccount_Adapter(getActivity(), optionTitle, option_icon);
    listView=(ListView)getActivity().findViewById(R.id.MyAccountList);
    listView.setAdapter(adapter);

return rootView;

}

I am getting errors fragmentpageradapter cannot be applied in my adapter:

super(context, R.layout.myaccount_options_viewholder, option_title);

and another error MyAccount_Adapter cannot be applied and adapter cannot be applied in my fragment:

MyAccount_Adapter adapter=new MyAccount_Adapter(this, optionTitle, option_icon);
listView.setAdapter(adapter);

Is this caused by the context? I'm not sure how to solve it.

1

2 Answers 2

2
  • You cannot set FragmentPagerAdapter to ListView.

    Because setAdapter() in ListView needs ListAdapter (Adapter which implements ListAdapter) as parameter.

    You can use BaseAdapter.

  • You cannot pass this as context for the adapter.

    Because this in fragment refers to that fragment, not activity.

    So, you need to pass getActivity()

Example Adapter:

public class MyAccount_Adapter extends BaseAdapter {

private final Activity context;
private final String[] option_title;
private final Integer[] option_icon;
public MyAccount_Adapter(Activity context, String[] option_title, Integer[] option_icon) {

    this.context=context;
    this.option_title=option_title;
    this.option_icon=option_icon;
}

@Override
public int getCount() {
    return option_title.length; //returns total of items in the array
}

@Override
public Object getItem(int position) {
    return option_title[position]; //returns item at the specified position
}

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

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.myaccount_options_viewholder, null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.option_title);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.profile_icon);

    txtTitle.setText(option_title[position]);
    imageView.setImageResource(option_icon[position]);
    return rowView;

};
}

For optimizations refer this link: Using a BaseAdapter with ListView

Update:

You can get listView from rootView only, obviously as its existing there & cannot get from your activity using getActivity()

listView=(ListView) rootView.findViewById(R.id.MyAccountList);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your reply, I have made the following changes you said but now I am getting this error when I run the code : Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference, can you teach me how to solve it?
@JerryKo Updated answer with sample adapter, if you have further doubt, update the code in your question with what you have implemented now
Thank you for your time, I have updated the code, but I still getting this error : Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapte‌​r)' on a null object reference. After running the code
0

Try this code

MyAccount_Adapter adapter=new MyAccount_Adapter(getActivity(),optionTitle, option_icon); listView.setAdapter(adapter);

1 Comment

I have changed the code in from this to getActivity(), there are no more errors in my fragment class however in the adapter, I am still getting errors at super(context, R.layout.myaccount_options_viewholder, option_title); I think it might have something to do with my context can you please have a look?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.