Search code examples
androidandroid-layoutandroid-fragmentsandroid-resourcesandroid-listfragment

Diference between resources in ListFragment


So, I want to create a ListView fragment, but all im getting is blank fragment. My guess is that 3 layout resourses (R.layout.fragment_home) should be diferent, but I can't understand which.

Fragment code:

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


    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    listView = (ListView) rootView.findViewById(R.layout.fragment_home);
    News n = new News("Pavadinimas", "http://www.google.lt", "Apibudinimas", "2015");
    news.add(n);
    mAdapter = new List(getActivity(), R.layout.fragment_home, news);
    setListAdapter(mAdapter);


    return listView;
}

R.layout.fragment_home:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".List" >


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="14dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

After trying to return rootview I get this: E/AndroidRuntime(26302): Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class Maybe problem here ?

frag = new HomeFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, frag).commit();

mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);

Array adapter:

public class List extends ArrayAdapter<News>{

private final Context context;
private final ArrayList<News> news;

public List(Context context, int resource, ArrayList<News> news){
    super(context, resource, news);

    this.context = context;
    this.news=news;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.fragment_home, parent, false);

    TextView textView1 = (TextView) rowView.findViewById(R.id.textView1);
    textView1.setText(news.get(position).desc);
    textView1.setHint(news.get(position).link);

    return rowView;
}
}

News class:

public class News
{
public String title;
public String link;
public String desc;
public String date;

public News(String title, String link, String desc, String date)
{
    this.title = title;
    this.link = link;
    this.desc = desc;
    this.date = date;
}
}

Solution

  • My guess is that 3 layout resourses (R.layout.fragment_home) should be diferent, but I can't understand which.

    You are partly right. You need to infalte 2 layouts one for the listView and the other for the fragment. ListView belongs to the fragment layout

    You inflate a custom layout for listview items.

    You need to change this

    listView = (ListView) rootView.findViewById(R.layout.fragment_home);
    

    to

    listView = (ListView) rootView.findViewById(R.id.list);
    

    You need to have a ListView with id list in fragment_home.xml.

    In your adapter getView you need to inflate a custom layout.

    Edit:

    HomeFragment

    public class HomeFragment extends Fragment { // note extends fragment
    
        ListView listView;
        ArrayList<News> news = new ArrayList<News>();
        List mAdapter;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
    
            View rootView = inflater.inflate(R.layout.fragment_home, container, false);
            listView = (ListView) rootView.findViewById(R.id.listView1);
            News n = new News("Pavadinimas", "http://www.google.lt", "Apibudinimas", "2015");
            news.add(n);
            mAdapter = new List(getActivity(), 0, news);
            listView.setAdapter(mAdapter); // note the change setAdapter
    
            return rootView;
        }
    
    }
    

    fragment_home.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >
        </ListView>
    
    </RelativeLayout>
    

    News.java

    public class News {
        String name,url,title,year;
    
        public News(String name, String url, String title, String year) {
            // TODO Auto-generated constructor stub
            this.name= name;
            this.url = url;
            this.title = title;
            this.year=year;
        }
    
    }
    

    List.java

    class List extends ArrayAdapter<News>
    {
        LayoutInflater mInflater;
        ArrayList<News> news;
        public List(Context context, int resource, ArrayList<News> news) {
            super(context, resource, news);
            mInflater = LayoutInflater.from(context);
            this.news = news;
        }
      public View getView(int pos, View arg1, ViewGroup arg2) {
    
       ViewHolder holder;
       if(arg1==null)
       {
       arg1=mInflater.inflate(R.layout.list_item, null);
       holder= new ViewHolder();
       holder.tv1 = (TextView) arg1.findViewById(R.id.textView1);
       holder.tv2 = (TextView) arg1.findViewById(R.id.textView2);
       holder.tv3 = (TextView) arg1.findViewById(R.id.textView3);
       arg1.setTag(holder);
       }else
       {
           holder = (ViewHolder) arg1.getTag();
       }
       News n= news.get(pos);
       holder.tv1.setText(n.title);
    
      return arg1;
    }
        static class ViewHolder
        {
            TextView tv1,tv2,tv3;
        }
    }
    

    list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="31dp"
            android:layout_marginTop="53dp"
            android:text="TextView" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView1"
            android:layout_alignBottom="@+id/textView1"
            android:layout_centerHorizontal="true"
            android:text="TextView" />
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView2"
            android:layout_alignBottom="@+id/textView2"
            android:layout_alignParentRight="true"
            android:layout_marginRight="21dp"
            android:text="TextView" />
    
    </RelativeLayout>