3

I trying to build an app with 4 tabs. Every tab has a different fragment linked to it. The issue is that I want to make a custom listView for each fragment, but it ends with some unsolvable error... I have talked to other developers, but I still can't make one that works! It's really frustrating!

I have:

  • A MainActivity class that works, it uses swipe-able tabs
  • An XML with the design I want on my custom ListView.
  • An XML called fragment1 with a ListView.

These are normal errors I get:

  • "The method findViewById(int) is undefined for the type Fragment1UG"
  • "The method setContentView(int) is undefined for the type Fragment1UG. 1 quick fix available: Create method 'setContentView()'"

One of the guides I'm trying to understand and use:

This is my 1st fragment:

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 Fragment1test extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            //This layout contains your list view 
                View view = inflater.inflate(R.layout.fragment1, container, false);

               //now you must initialize your list view
                ListView yourListView = (ListView)view.findViewById(R.id.ListView1);
                ListView.setAdapter(new ListAdapter());
              return view;
    }
}

My ListAdapter.java code (from a tutorial):

import java.util.List;

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

public class ListAdapter extends ArrayAdapter<Item> {

public ListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    // TODO Auto-generated constructor stub
}

private List<Item> items;

public ListAdapter(Context context, int resource, List<Item> items) {

    super(context, resource, items);

    this.items = items;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;

    if (v == null) {

        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.list_design, null);

    }

    Item p = items.get(position);

    if (p != null) {

        TextView tt = (TextView) v.findViewById(R.id.game_txtTitle);
        TextView tt1 = (TextView) v.findViewById(R.id.game_txtRelease);
        TextView tt3 = (TextView) v.findViewById(R.id.game_txtPlatform);

        if (tt != null) {
            tt.setText(p.getId());
        }
        if (tt1 != null) {

            tt1.setText(p.getCategory().getId());
        }
        if (tt3 != null) {

            tt3.setText(p.getDescription());
        }
    }
johgru
  • 241
  • 2
  • 5
  • 20

3 Answers3

12
public class Fragment1test extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //This layout contains your list view 
            View view = inflater.inflate(R.layout.fragment_basic, container, false);

           //now you must initialize your list view
           ListView listview =(ListView)view.findViewById(R.id.your_listview);

           //EDITED Code 
           String[] items = new String[] {"Item 1", "Item 2", "Item 3"};
           ArrayAdapter<String> adapter =
           new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items); 

            listview.setAdapter(adapter);  

            //To have custom list view use this : you must define CustomeAdapter class
            // listview.setadapter(new CustomeAdapter(getActivity()));
           //getActivty is used instead of Context
         return view;
    }
  }

Refer this link & question to know how to create custom adapter

Note : do not use List fragment or List activity to create custom listview

EDIT

 ListView yourListView = (ListView)view.findViewById(R.id.ListView1);
//Here items must be a List<Items> according to your class instead of String[] array
ListAdapter listadapter = new ListAdapter(getActivity(), android.R.layout.simple_list_item_1, items)
ListView.setAdapter( listAdapter);
Community
  • 1
  • 1
edwin
  • 7,985
  • 10
  • 51
  • 82
  • Thank you, but I get an error at -> listview.setadapter(new CustomeAdapter()); "The constructor ListAdapter() is undefined" – johgru Jun 29 '13 at 15:53
  • How do I use my own list design? With a custom adapter? – johgru Jun 29 '13 at 17:18
  • did you refer the links i given in answer . it gives answer to your question. Inside the getView() method of your custom adapter class you can implement your list design by inflating your item layout for your listview . – edwin Jun 29 '13 at 17:22
  • "Cannot make a static reference to the non-static method setAdapter(ListAdapter) from the type ListView" "The constructor ListAdapter(FragmentActivity) is undefined" – johgru Jun 29 '13 at 17:30
  • The character 'v' in Listview should be changed to ListView. – Alston May 23 '14 at 12:58
  • (Listview) --> (ListView). You still forgot one. – Alston May 23 '14 at 14:34
  • @Stallman my dear friend we are humans mistakes happens .If you find a mistake you can edit it .I will take care of it .and is this because of this you down-vote my answer – edwin May 28 '14 at 04:15
1
public class fragmentpassword extends Fragment {

        String[] name={"A","B","C","D"};
        int [] image={R.drawable.ic_drawer,R.drawable.ic_drawer,R.drawable.ic_drawer,R.drawable.ic_drawer};

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

            View v = inflater.inflate(R.layout.layoutpassword, container, false);

            ListView li=(ListView)v.findViewById(R.id.listViewPassword);
            li.setAdapter(new PasswordAdapter(getActivity(),R.layout.passwordlay,name));
            return v;
        }

        class PasswordAdapter extends ArrayAdapter {

            public PasswordAdapter(Context context, int resource, String[] objects) {
                super(context, resource, objects);
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v=((Activity)getContext()).getLayoutInflater().inflate(R.layout.passwordlay,null);
                TextView txt1 = (TextView) v.findViewById(R.id.textViewpasslay);
                  txt1.setText(name[position]);
                ImageView img = (ImageView) v.findViewById(R.id.imageViewpasslay);
                img.setBackgroundResource(image[position]);

                return v;
            }
        }
    }


    **XML for customlist: passwordlay.xml**

    <?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">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageViewpasslay" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/textViewpasslay"/>
    </LinearLayout>

    **XML for fragment:  layoutpassword.xml**

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

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listViewPassword"/>
    </LinearLayout>
0

Fragments don't use setContentView(int), so you can't use that, use the inflater in public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) and return the view inflated. As for findViewById(int) do it like this getActivity().findViewById(int).

LuckyMe
  • 3,820
  • 2
  • 27
  • 35
  • Thank you! I still not working, but I understand a lot more now. What about this error? "The constructor ArrayAdapter(ListActivity, int, String[]) is undefined" – johgru Jun 29 '13 at 14:21