Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView with Fragment

I'm working on my school project which is based on a Fragment. I have made the XML files but I'm getting an error in the nearfragment.java. Could you please tell me where the error is as I'm familiar with Activity but not with Fragment. Please help me to rectify the error.

near_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="{relativePackage}.${activityClass}" >

    <ListView
        android:id="@+id/list_li"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

near_layout_mylist.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"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon_list"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp" />

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/item_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:textColor="#33CC33" />
        <TextView
            android:id="@+id/textView1_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>
</LinearLayout>

CustomListNearAdapter.java

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

public class CustomListNearAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] itemname;
    private final Integer[] imgid;

    public CustomListNearAdapter(Activity context, String[] itemname, Integer[] imgid) {
        super(context, R.layout.near_layout_mylist, itemname);
        // TODO Auto-generated constructor stub

        this.context=context;
        this.itemname=itemname;
        this.imgid=imgid;
    }

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

        TextView txtTitle = (TextView) rowView.findViewById(R.id.item_list);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon_list);
        TextView extratxt = (TextView) rowView.findViewById(R.id.textView1_list);

        txtTitle.setText(itemname[position]);
        imageView.setImageResource(imgid[position]);
        extratxt.setText("Description "+itemname[position]);
        return rowView;

    };
}

NearFragment

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;

/**
 * Created by Ratan on 7/29/2015.
 */
public class NearFragment extends Fragment {



    ListView list;
    String[] itemname ={
            "Safari",
            "Camera",
            "Global",
            "FireFox",
            "UC Browser",
            "Android Folder",
            "VLC Player",
            "Cold War"
    };

    Integer[] imgid={
            R.drawable.akash,
            R.drawable.akash,
            R.drawable.akash,
            R.drawable.akash,
            R.drawable.akash,
            R.drawable.akash,
            R.drawable.akash,
            R.drawable.akash,
    };

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

        View view = inflater.inflate(R.layout.near_layout, container,false);
        CustomListNearAdapter adapter=new CustomListNearAdapter(this, itemname, imgid);

        ListView listView = (ListView) view.findViewById(R.id.list_item);
        listView.setAdapter(new CustomListNearAdapter(getActivity()));
        return view;


    }


}

The error lies in nearfragment, I'm not able to write the code for the Fragment. So please give me some directions.

like image 286
Akashdeep Singh Avatar asked Sep 26 '22 05:09

Akashdeep Singh


2 Answers

In yours code you are not passing the correct values to the constructor while setting the adapter , means at the line in yours code.

listView.setAdapter(new CustomListNearAdapter(getActivity()));

In above line there is problem, so replace the code with below lines of code.

        CustomListNearAdapter adapter=new CustomListNearAdapter(getActivity(), itemname, imgid);

        ListView listView = (ListView) view.findViewById(R.id.list_li);
        listView.setAdapter(adapter);
like image 177
Ravindra Kushwaha Avatar answered Oct 09 '22 14:10

Ravindra Kushwaha


Use getActivity() instead of this in bellow line

CustomListNearAdapter adapter=new CustomListNearAdapter(this, itemname, imgid);

and set adapter to listview like bellow

listView.setAdapter(adapter);
like image 1
Bajirao Shinde Avatar answered Oct 09 '22 15:10

Bajirao Shinde