27

As the title I want to create a listview with custom row in Fragment. My code below.

Fragment class

public class PhotosFragment extends Fragment{

public PhotosFragment(){}

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

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

    ArrayList<ListviewContactItem> listContact = GetlistContact();
    ListView lv = (ListView)getActivity().findViewById(R.id.lv_contact);
    lv.setAdapter(new ListviewContactAdapter(getActivity(), listContact));

    return rootView;
}

private ArrayList<ListviewContactItem> GetlistContact(){
    ArrayList<ListviewContactItem> contactlist = new ArrayList<ListviewContactItem>();

    ListviewContactItem contact = new ListviewContactItem();

    contact.SetName("Topher");
    contact.SetPhone("01213113568");
    contactlist.add(contact);

    contact = new ListviewContactItem();
    contact.SetName("Jean");
    contact.SetPhone("01213869102");
    contactlist.add(contact);

    contact = new ListviewContactItem();
    contact.SetName("Andrew");
    contact.SetPhone("01213123985");
    contactlist.add(contact);

    return contactlist; 
    }   
}

Adapter class

public class ListviewContactAdapter extends BaseAdapter{
private static ArrayList<ListviewContactItem> listContact;

private LayoutInflater mInflater;

public ListviewContactAdapter(Context photosFragment, ArrayList<ListviewContactItem> results){
    listContact = results;
    mInflater = LayoutInflater.from(photosFragment);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return listContact.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return listContact.get(arg0);
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}


public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    if(convertView == null){
        convertView = mInflater.inflate(R.layout.contact_item, null);
        holder = new ViewHolder();
        holder.txtname = (TextView) convertView.findViewById(R.id.lv_contact_item_name);          
        holder.txtphone = (TextView) convertView.findViewById(R.id.lv_contact_item_phone);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtname.setText(listContact.get(position).GetName());
    holder.txtphone.setText(listContact.get(position).GetPhone());

    return convertView;
}

static class ViewHolder{
    TextView txtname, txtphone;
}
}

But when I run the app that display no thing. Could anyone tell me what wrong here and how can I fix it?

gamo
  • 1,549
  • 7
  • 24
  • 36
  • 1
    No, It runs fine but display nothing in fragment. – gamo Mar 19 '14 at 23:59
  • I'm also trying the same thing. Can you give me the full source code? – gangadhars Sep 27 '14 at 18:08
  • All thing I posted in my quension. And you can also find out many tutorial about listview on google. – gamo Sep 29 '14 at 06:56
  • for me too same does not turn out here my the code http://stackoverflow.com/questions/26254727/actionbar-side-menu-navigation-with-nested-viewpager-fragment-tabs-tutorial – user3796282 Oct 09 '14 at 05:26
  • Hi @gamo can you show me code for layout "contact_item" and Java "ListviewContactItem". I am trying to do something similar. – vikas devde Sep 20 '17 at 09:13

5 Answers5

40

I guess your app crashes because of NullPointerException.

Change this

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

to

ListView lv = (ListView)rootView.findViewById(R.id.lv_contact);

assuming listview belongs to the fragment layout.

The rest of the code looks alright

Edit:

Well since you said it is not working i tried it myself

enter image description here

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @user3422401 alright i will try your code and get back in 5 minutes. But the codes look alright – Raghunandan Mar 20 '14 at 04:38
  • @user3422401 it works fine i only made changes as i suggested in my post. – Raghunandan Mar 20 '14 at 04:49
  • @user3422401 do check the snaps works well. SO we need more info. The code posted is fine. Where do you add fragment to the activity. post the xml layouts – Raghunandan Mar 20 '14 at 04:51
  • I tried but it doesn't work for me. My Fragment class inside a sliding menu call by MainActivity but I think it's not important. – gamo Mar 20 '14 at 10:01
  • @user3422401 AFAIK there is nothing wrong in the code you posted. There is something wrong else where – Raghunandan Mar 20 '14 at 10:03
  • Finaly I found where wrong. That in my MainActivy class. Thank you and sorry because waste your time for this. – gamo Mar 20 '14 at 12:02
  • Good catch with that, i ran a replace command converting some of my code from "this" to "getActivity()" and totally missed it, couple of hours of googling lead me here. cheers – Guernica Nov 03 '14 at 01:10
  • @Raghunandan Can you please tell what's the problem with getActivity? YOu have solved my problem :) – hellodear Dec 03 '14 at 14:15
4

Please use ListFragment. Otherwise, it won't work.

EDIT 1: Then you'll only need setListAdapter() and getListView().

Julisch
  • 308
  • 1
  • 16
2

you need to give:

public void onActivityCreated(Bundle savedInstanceState)    
{
  super.onActivityCreated(savedInstanceState);
}

inside fragment.

Aman Gupta
  • 5,548
  • 10
  • 52
  • 88
2

The inflate() method takes three parameters:

  1. The id of a layout XML file (inside R.layout),
  2. A parent ViewGroup into which the fragment's View is to be inserted,

  3. A third boolean telling whether the fragment's View as inflated from the layout XML file should be inserted into the parent ViewGroup.

In this case we pass false because the View will be attached to the parent ViewGroup elsewhere, by some of the Android code we call (in other words, behind our backs). When you pass false as last parameter to inflate(), the parent ViewGroup is still used for layout calculations of the inflated View, so you cannot pass null as parent ViewGroup .

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

So, You need to call rootView in here

ListView lv = (ListView)rootView.findViewById(R.id.lv_contact);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

Instead:

public class PhotosFragment extends Fragment

You can use:

public class PhotosFragment extends ListFragment

It change the methods

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayList<ListviewContactItem> listContact = GetlistContact();
        setAdapter(new ListviewContactAdapter(getActivity(), listContact));
    }

onActivityCreated is void and you didn't need to return a view like in onCreateView

You can see an example here

VictorPurMar
  • 151
  • 9