android custom adapter for ListView in a fragment












-1














I've created my own custom adapter for a ListView that is in a fragment. I believe I have everything correct, but it still throws an error when launching my app and it crashes. I tried to debug it, but ended up not being able to locate the issue.



Here's my code for the fragment:



public class TabFragment0 extends Fragment {

Button addPR;
ArrayList<PRObject> PRList;
PRObjectAdapter PRListAdapter;
private ListView listViewer;
private final String PRKey = "PRArgs";

public TabFragment0() {
// Required empty public constructor
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.tab_personalrecords, container,
false);
if(loadList(PRKey) != null) {
PRList = loadList(PRKey);
}
PRListAdapter = new PRObjectAdapter(getActivity(), PRList);
listViewer = (ListView) view.findViewById(R.id.prListView);
listViewer.setAdapter(PRListAdapter);
...................................................


The line listViewer.setAdapter(PRListAdapter); is what throws the error. I've also tried PRListAdapter = new PRObjectAdapter(this.getActivity(), PRList); as wel as PRListAdapter = new PRObjectAdapter(getContext(), PRList); to no avail.



Here's my code for the adapter class:



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;

public class PRObjectAdapter extends BaseAdapter {

private LayoutInflater inflater;
private ArrayList<PRObject> objects;

private class ViewHolder {
TextView textview1;
TextView textview2;
}

public PRObjectAdapter(Context context, ArrayList<PRObject> objects) {
inflater = LayoutInflater.from(context);
this.objects = objects;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
PRObjectAdapter.ViewHolder holder = null;
if(convertView == null) {
holder = new PRObjectAdapter.ViewHolder();
convertView = inflater.inflate(R.layout.pr_item_layout, null);
holder.textview1 = (TextView)
convertView.findViewById(R.id.prTitleTV);
holder.textview2 = (TextView)
convertView.findViewById(R.id.prNumTV);
convertView.setTag(holder);
} else {
holder = (PRObjectAdapter.ViewHolder) convertView.getTag();
}
holder.textview1.setText(objects.get(position).getTitle());
holder.textview2.setText(objects.get(position).getNum());
return convertView;
}
}


I want to say that the issue is in my View getView method, but I can't pinpoint the issue.
Any help would be greatly appreciated.



EDIT:



This question is not a duplicate of
this NullPointerException question



My question specifically pertains to the fragment/customAdapter combination in Android Studio, whereas the above link is just a guide on what a NPE is and how to fix it.










share|improve this question




















  • 1




    share your log report. objects.get(position).getNum() what is the return type of "getNum() " ?
    – shb
    Nov 11 at 2:23












  • I'm a complete fricken moron. Look at the line ArrayList<PRObject> PRList;, and then compare it to PRListAdapter = new PRObjectAdapter(getActivity(), PRList);. Notice anything strange? ArrayList<PRObject> PRList; needs to be ArrayList<PRObject> PRList = new ArrayList<PRObject>();.... I'm such an idiot, lol
    – ESM
    Nov 11 at 2:47










  • Possible duplicate of What is a NullPointerException, and how do I fix it?
    – Hovercraft Full Of Eels
    Nov 11 at 2:57










  • @HovercraftFullOfEels it is not a duplicate.
    – ESM
    Nov 11 at 3:24
















-1














I've created my own custom adapter for a ListView that is in a fragment. I believe I have everything correct, but it still throws an error when launching my app and it crashes. I tried to debug it, but ended up not being able to locate the issue.



Here's my code for the fragment:



public class TabFragment0 extends Fragment {

Button addPR;
ArrayList<PRObject> PRList;
PRObjectAdapter PRListAdapter;
private ListView listViewer;
private final String PRKey = "PRArgs";

public TabFragment0() {
// Required empty public constructor
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.tab_personalrecords, container,
false);
if(loadList(PRKey) != null) {
PRList = loadList(PRKey);
}
PRListAdapter = new PRObjectAdapter(getActivity(), PRList);
listViewer = (ListView) view.findViewById(R.id.prListView);
listViewer.setAdapter(PRListAdapter);
...................................................


The line listViewer.setAdapter(PRListAdapter); is what throws the error. I've also tried PRListAdapter = new PRObjectAdapter(this.getActivity(), PRList); as wel as PRListAdapter = new PRObjectAdapter(getContext(), PRList); to no avail.



Here's my code for the adapter class:



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;

public class PRObjectAdapter extends BaseAdapter {

private LayoutInflater inflater;
private ArrayList<PRObject> objects;

private class ViewHolder {
TextView textview1;
TextView textview2;
}

public PRObjectAdapter(Context context, ArrayList<PRObject> objects) {
inflater = LayoutInflater.from(context);
this.objects = objects;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
PRObjectAdapter.ViewHolder holder = null;
if(convertView == null) {
holder = new PRObjectAdapter.ViewHolder();
convertView = inflater.inflate(R.layout.pr_item_layout, null);
holder.textview1 = (TextView)
convertView.findViewById(R.id.prTitleTV);
holder.textview2 = (TextView)
convertView.findViewById(R.id.prNumTV);
convertView.setTag(holder);
} else {
holder = (PRObjectAdapter.ViewHolder) convertView.getTag();
}
holder.textview1.setText(objects.get(position).getTitle());
holder.textview2.setText(objects.get(position).getNum());
return convertView;
}
}


I want to say that the issue is in my View getView method, but I can't pinpoint the issue.
Any help would be greatly appreciated.



EDIT:



This question is not a duplicate of
this NullPointerException question



My question specifically pertains to the fragment/customAdapter combination in Android Studio, whereas the above link is just a guide on what a NPE is and how to fix it.










share|improve this question




















  • 1




    share your log report. objects.get(position).getNum() what is the return type of "getNum() " ?
    – shb
    Nov 11 at 2:23












  • I'm a complete fricken moron. Look at the line ArrayList<PRObject> PRList;, and then compare it to PRListAdapter = new PRObjectAdapter(getActivity(), PRList);. Notice anything strange? ArrayList<PRObject> PRList; needs to be ArrayList<PRObject> PRList = new ArrayList<PRObject>();.... I'm such an idiot, lol
    – ESM
    Nov 11 at 2:47










  • Possible duplicate of What is a NullPointerException, and how do I fix it?
    – Hovercraft Full Of Eels
    Nov 11 at 2:57










  • @HovercraftFullOfEels it is not a duplicate.
    – ESM
    Nov 11 at 3:24














-1












-1








-1







I've created my own custom adapter for a ListView that is in a fragment. I believe I have everything correct, but it still throws an error when launching my app and it crashes. I tried to debug it, but ended up not being able to locate the issue.



Here's my code for the fragment:



public class TabFragment0 extends Fragment {

Button addPR;
ArrayList<PRObject> PRList;
PRObjectAdapter PRListAdapter;
private ListView listViewer;
private final String PRKey = "PRArgs";

public TabFragment0() {
// Required empty public constructor
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.tab_personalrecords, container,
false);
if(loadList(PRKey) != null) {
PRList = loadList(PRKey);
}
PRListAdapter = new PRObjectAdapter(getActivity(), PRList);
listViewer = (ListView) view.findViewById(R.id.prListView);
listViewer.setAdapter(PRListAdapter);
...................................................


The line listViewer.setAdapter(PRListAdapter); is what throws the error. I've also tried PRListAdapter = new PRObjectAdapter(this.getActivity(), PRList); as wel as PRListAdapter = new PRObjectAdapter(getContext(), PRList); to no avail.



Here's my code for the adapter class:



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;

public class PRObjectAdapter extends BaseAdapter {

private LayoutInflater inflater;
private ArrayList<PRObject> objects;

private class ViewHolder {
TextView textview1;
TextView textview2;
}

public PRObjectAdapter(Context context, ArrayList<PRObject> objects) {
inflater = LayoutInflater.from(context);
this.objects = objects;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
PRObjectAdapter.ViewHolder holder = null;
if(convertView == null) {
holder = new PRObjectAdapter.ViewHolder();
convertView = inflater.inflate(R.layout.pr_item_layout, null);
holder.textview1 = (TextView)
convertView.findViewById(R.id.prTitleTV);
holder.textview2 = (TextView)
convertView.findViewById(R.id.prNumTV);
convertView.setTag(holder);
} else {
holder = (PRObjectAdapter.ViewHolder) convertView.getTag();
}
holder.textview1.setText(objects.get(position).getTitle());
holder.textview2.setText(objects.get(position).getNum());
return convertView;
}
}


I want to say that the issue is in my View getView method, but I can't pinpoint the issue.
Any help would be greatly appreciated.



EDIT:



This question is not a duplicate of
this NullPointerException question



My question specifically pertains to the fragment/customAdapter combination in Android Studio, whereas the above link is just a guide on what a NPE is and how to fix it.










share|improve this question















I've created my own custom adapter for a ListView that is in a fragment. I believe I have everything correct, but it still throws an error when launching my app and it crashes. I tried to debug it, but ended up not being able to locate the issue.



Here's my code for the fragment:



public class TabFragment0 extends Fragment {

Button addPR;
ArrayList<PRObject> PRList;
PRObjectAdapter PRListAdapter;
private ListView listViewer;
private final String PRKey = "PRArgs";

public TabFragment0() {
// Required empty public constructor
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.tab_personalrecords, container,
false);
if(loadList(PRKey) != null) {
PRList = loadList(PRKey);
}
PRListAdapter = new PRObjectAdapter(getActivity(), PRList);
listViewer = (ListView) view.findViewById(R.id.prListView);
listViewer.setAdapter(PRListAdapter);
...................................................


The line listViewer.setAdapter(PRListAdapter); is what throws the error. I've also tried PRListAdapter = new PRObjectAdapter(this.getActivity(), PRList); as wel as PRListAdapter = new PRObjectAdapter(getContext(), PRList); to no avail.



Here's my code for the adapter class:



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;

public class PRObjectAdapter extends BaseAdapter {

private LayoutInflater inflater;
private ArrayList<PRObject> objects;

private class ViewHolder {
TextView textview1;
TextView textview2;
}

public PRObjectAdapter(Context context, ArrayList<PRObject> objects) {
inflater = LayoutInflater.from(context);
this.objects = objects;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
PRObjectAdapter.ViewHolder holder = null;
if(convertView == null) {
holder = new PRObjectAdapter.ViewHolder();
convertView = inflater.inflate(R.layout.pr_item_layout, null);
holder.textview1 = (TextView)
convertView.findViewById(R.id.prTitleTV);
holder.textview2 = (TextView)
convertView.findViewById(R.id.prNumTV);
convertView.setTag(holder);
} else {
holder = (PRObjectAdapter.ViewHolder) convertView.getTag();
}
holder.textview1.setText(objects.get(position).getTitle());
holder.textview2.setText(objects.get(position).getNum());
return convertView;
}
}


I want to say that the issue is in my View getView method, but I can't pinpoint the issue.
Any help would be greatly appreciated.



EDIT:



This question is not a duplicate of
this NullPointerException question



My question specifically pertains to the fragment/customAdapter combination in Android Studio, whereas the above link is just a guide on what a NPE is and how to fix it.







android listview android-fragments custom-adapter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 3:24

























asked Nov 11 at 2:13









ESM

163




163








  • 1




    share your log report. objects.get(position).getNum() what is the return type of "getNum() " ?
    – shb
    Nov 11 at 2:23












  • I'm a complete fricken moron. Look at the line ArrayList<PRObject> PRList;, and then compare it to PRListAdapter = new PRObjectAdapter(getActivity(), PRList);. Notice anything strange? ArrayList<PRObject> PRList; needs to be ArrayList<PRObject> PRList = new ArrayList<PRObject>();.... I'm such an idiot, lol
    – ESM
    Nov 11 at 2:47










  • Possible duplicate of What is a NullPointerException, and how do I fix it?
    – Hovercraft Full Of Eels
    Nov 11 at 2:57










  • @HovercraftFullOfEels it is not a duplicate.
    – ESM
    Nov 11 at 3:24














  • 1




    share your log report. objects.get(position).getNum() what is the return type of "getNum() " ?
    – shb
    Nov 11 at 2:23












  • I'm a complete fricken moron. Look at the line ArrayList<PRObject> PRList;, and then compare it to PRListAdapter = new PRObjectAdapter(getActivity(), PRList);. Notice anything strange? ArrayList<PRObject> PRList; needs to be ArrayList<PRObject> PRList = new ArrayList<PRObject>();.... I'm such an idiot, lol
    – ESM
    Nov 11 at 2:47










  • Possible duplicate of What is a NullPointerException, and how do I fix it?
    – Hovercraft Full Of Eels
    Nov 11 at 2:57










  • @HovercraftFullOfEels it is not a duplicate.
    – ESM
    Nov 11 at 3:24








1




1




share your log report. objects.get(position).getNum() what is the return type of "getNum() " ?
– shb
Nov 11 at 2:23






share your log report. objects.get(position).getNum() what is the return type of "getNum() " ?
– shb
Nov 11 at 2:23














I'm a complete fricken moron. Look at the line ArrayList<PRObject> PRList;, and then compare it to PRListAdapter = new PRObjectAdapter(getActivity(), PRList);. Notice anything strange? ArrayList<PRObject> PRList; needs to be ArrayList<PRObject> PRList = new ArrayList<PRObject>();.... I'm such an idiot, lol
– ESM
Nov 11 at 2:47




I'm a complete fricken moron. Look at the line ArrayList<PRObject> PRList;, and then compare it to PRListAdapter = new PRObjectAdapter(getActivity(), PRList);. Notice anything strange? ArrayList<PRObject> PRList; needs to be ArrayList<PRObject> PRList = new ArrayList<PRObject>();.... I'm such an idiot, lol
– ESM
Nov 11 at 2:47












Possible duplicate of What is a NullPointerException, and how do I fix it?
– Hovercraft Full Of Eels
Nov 11 at 2:57




Possible duplicate of What is a NullPointerException, and how do I fix it?
– Hovercraft Full Of Eels
Nov 11 at 2:57












@HovercraftFullOfEels it is not a duplicate.
– ESM
Nov 11 at 3:24




@HovercraftFullOfEels it is not a duplicate.
– ESM
Nov 11 at 3:24












1 Answer
1






active

oldest

votes


















0














Okay so it turns out nothing was wrong with the adapter class, it works perfectly as intended. My problem was that:



ArrayList<PRObject> PRList;



was set without being initialized in my fragment class.



The fix was to change it to:



ArrayList<PRObject> PRList = new ArrayList<PRObject>();



The view now loads as intended. Hopefully someone can make use out of my adapter class then since it works, hahah



Cheers everyone!






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245264%2fandroid-custom-adapter-for-listview-in-a-fragment%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Okay so it turns out nothing was wrong with the adapter class, it works perfectly as intended. My problem was that:



    ArrayList<PRObject> PRList;



    was set without being initialized in my fragment class.



    The fix was to change it to:



    ArrayList<PRObject> PRList = new ArrayList<PRObject>();



    The view now loads as intended. Hopefully someone can make use out of my adapter class then since it works, hahah



    Cheers everyone!






    share|improve this answer


























      0














      Okay so it turns out nothing was wrong with the adapter class, it works perfectly as intended. My problem was that:



      ArrayList<PRObject> PRList;



      was set without being initialized in my fragment class.



      The fix was to change it to:



      ArrayList<PRObject> PRList = new ArrayList<PRObject>();



      The view now loads as intended. Hopefully someone can make use out of my adapter class then since it works, hahah



      Cheers everyone!






      share|improve this answer
























        0












        0








        0






        Okay so it turns out nothing was wrong with the adapter class, it works perfectly as intended. My problem was that:



        ArrayList<PRObject> PRList;



        was set without being initialized in my fragment class.



        The fix was to change it to:



        ArrayList<PRObject> PRList = new ArrayList<PRObject>();



        The view now loads as intended. Hopefully someone can make use out of my adapter class then since it works, hahah



        Cheers everyone!






        share|improve this answer












        Okay so it turns out nothing was wrong with the adapter class, it works perfectly as intended. My problem was that:



        ArrayList<PRObject> PRList;



        was set without being initialized in my fragment class.



        The fix was to change it to:



        ArrayList<PRObject> PRList = new ArrayList<PRObject>();



        The view now loads as intended. Hopefully someone can make use out of my adapter class then since it works, hahah



        Cheers everyone!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 2:53









        ESM

        163




        163






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245264%2fandroid-custom-adapter-for-listview-in-a-fragment%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini