Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am developing a POS system in android and trying to list images from an api into a fragment with a recyclerview.when i debug i see the list returned but not in the android code.what more can i do to show the information in my layout.maybe i'm doing something wrong. the main activity is supposed to load the categorylist activity. categorylist is using both categoylistrecyclerview and categorylistrecycler adapter. when i debug i see the list in the category list but i get an error in main activity no recycler adapter attached.meanwhile i have attached it.maybe i'm not setting the image well in the recycleradapter.please check ItemObject if i set the image well

What I have tried:

this is activity main
<?xml version="1.0" encoding="utf-8"?>

<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"
    xmlns:include="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#f4efef"
        android:orientation="vertical">


        <TextView
            android:id="@+id/order"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="Order"
            android:textAllCaps="true"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="16sp" />
        <!--<View-->
            <!--android:id="@+id/view1"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="2dp"-->
            <!--android:background="@color/divider" />-->


        <TextView
            android:id="@+id/seatno"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/order"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="SEAT NO."
            android:textAllCaps="true"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="16sp" />

        <TableLayout
            android:id="@+id/itemlist"
            android:layout_width="match_parent"
            android:layout_height="400dp">

            <TableRow>


                <TextView
                    android:layout_height="30dp"
                    android:layout_column="1"
                    android:width="250dp"
                    android:padding="3dip"
                    android:text="Item"
                    android:textAlignment="center"
                    android:layout_weight="2"
                    android:textSize="16sp"/>

                <TextView
                    android:layout_height="30dp"
                    android:layout_column="2"
                    android:width="80dp"
                    android:gravity="end"
                    android:padding="3dip"
                    android:text="Price"
                    android:textAlignment="center"
                    android:layout_weight="2"
                    android:textSize="16sp"/>

                <TextView
                    android:layout_height="30dp"
                    android:layout_column="3"
                    android:width="60dp"
                    android:gravity="end"
                    android:padding="3dip"
                    android:text="Qty"
                    android:textAlignment="center"
                    android:layout_weight="2"
                    android:textSize="16sp"/>

          </TableRow>
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </TableLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/totalamt"
                android:layout_width="390dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_below="@id/itemlist"
                android:text="Amount Due"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="18sp" />


        </LinearLayout>

        <LinearLayout
            android:id="@+id/LinearLayout02"
            android:layout_height="80dp"
            android:layout_width="match_parent"

            android:gravity="center">

            <Button
                android:id="@+id/Button02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Clear">
            </Button>

            <Button
                android:id="@+id/Button03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Payment">
            </Button>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="500dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:background="#f4efef"
        android:gravity="center_horizontal">


            <EditText
                android:id="@+id/edtSearch"
                android:layout_width="732dp"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:imeOptions="actionSearch"
                android:hint="Search for Item"
                android:gravity="center_horizontal"/>


        <fragment
            android:id="@+id/catframe"
            android:name ="com.sipconsult.golfpos.CategoryList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/categoryframe">

        </fragment>

    </LinearLayout>

</LinearLayout>



this is category frame
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.sipconsult.golfpos.CategoryList"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Price"
        android:textAlignment="center"
        android:layout_weight="2"
        android:textSize="16sp"/>

    <android.support.v7.widget.RecyclerView
        android:layout_below="@id/txt1"
        android:id="@+id/categoryrecycler"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>


this is categorylist
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cardview"
    android:layout_width="250dp"
    android:layout_height="250dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardCornerRadius="10dp"
    android:layout_margin="10dp">

    <RelativeLayout
        android:layout_width="278dp"
        android:layout_height="233dp">

        <ImageView
            android:id="@+id/categoryimage"
            android:layout_width="250dp"
            android:layout_height="200dp"

            android:src="@drawable/three"
            android:scaleType="centerCrop"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:contentDescription=""
            tools:ignore="ContentDescription"/>

        <TextView
            android:id="@+id/categoryname"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/categoryimage"
            android:background="@color/cardview_light_background"
            android:textAlignment="center"
            android:paddingBottom="8dp"
            android:paddingTop="8dp"
            android:text="Categoryname"
            android:textColor="@color/cardview_dark_background"
            android:textSize="14sp" />

    </RelativeLayout>

</android.support.v7.widget.CardView>


this is listviewwithimage
<?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"
    android:elevation="1dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="200dp"
        android:layout_height="200dp"

        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_margin="15dp"
        android:contentDescription="null" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:gravity="center"
        android:text="New Text"
        tools:ignore="RtlHardcoded"
        android:layout_alignEnd="@+id/imageView"
        android:layout_alignStart="@+id/imageView"
        android:layout_below="@+id/imageView" />
</RelativeLayout>


this is ItemObject
public class ItemObject {
    private String Category_Name;
    private String Image;
    private int Id;

    public ItemObject(int id, String name, String image) {
        this.Id = id;
        this.Category_Name = name;
        this.Image = image;

    }
    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public String getCategory_Name() {
        return Category_Name;
    }

    public void setCategory_Name(String name) {
        Category_Name = name;
    }

    public String getImage() {
        return Image;
    }


    public void setImage(String image) {
        Image = image;
    }

this is categorylistrecycleradapter
public class CategorylistRecyclerAdapter extends RecyclerView.Adapter<CategorylistViewHolders>{
    private ArrayList<ItemObject> itemList;
    private Context context;

    public CategorylistRecyclerAdapter(Context context, ArrayList<ItemObject> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public CategorylistViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.categorylist, parent, false);
        CategorylistViewHolders rcv = new CategorylistViewHolders(layoutView);
        return rcv;
    }

    @Override
    public void onBindViewHolder(CategorylistViewHolders holder, int position) {

        holder.categoryname.setText(itemList.get(position).getCategory_Name());
        holder.categoryimage.setImageResource(Integer.parseInt(itemList.get(position).getImage()));
    }

    @Override
    public int getItemCount() {
        return this.itemList.size();
    }
}


this is categorylistviewholder
public class CategorylistViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{

    public TextView categoryname;
    public ImageView categoryimage;

    public CategorylistViewHolders(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        categoryname = (TextView)itemView.findViewById(R.id.categoryname);
        categoryimage = (ImageView)itemView.findViewById(R.id.categoryimage);
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(view.getContext(), "Clicked Category = " + getPosition(), Toast.LENGTH_SHORT).show();
    }
}


this is categorylist java,loads the list from api
public class CategoryList extends Fragment {
    private GridLayoutManager gridLayout;
    private static String URL = "http:xxx";
    private  static  String Image_URL= "http:xxx";
    private ArrayList<ItemObject> List;
    private RecyclerView recyclerView;

    private CategorylistRecyclerAdapter rcAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view = inflater.inflate(R.layout.categoryframe, container, false);


        gridLayout = new GridLayoutManager(getActivity(),3);
        recyclerView = (RecyclerView) view.findViewById(R.id.categoryrecycler);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(gridLayout);

        List = new ArrayList<>();

        loadRecyclerViewData();
        return view;


    }

    /*@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        text = (TextView)view.findViewById(R.id.txt1);

        gridLayout = new GridLayoutManager(getActivity(),3);
        recyclerView = (RecyclerView) view.findViewById(R.id.categoryrecycler);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(gridLayout);

        List = new ArrayList<>();
        text.setText("Hello world");
        loadRecyclerViewData();


    }*/

    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

    }

    private void loadRecyclerViewData() {
        final ProgressDialog progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("Loading....");
        progressDialog.show();

        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,

                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        progressDialog.dismiss();
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONArray jsonArray = jsonObject.getJSONArray("Model");
                            //int[] images = getImg();
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject p = jsonArray.getJSONObject(i);
                                ItemObject list = new ItemObject
                                        (
                                        p.getInt("CategoryId"),
                                        p.getString("Category_Name"),
                                        p.getString(Image_URL + "Image")

                                );
                                List.add(list);
                            }
                             rcAdapter = new CategorylistRecyclerAdapter(getActivity(),List);
                            recyclerView.setAdapter(rcAdapter);


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        requestQueue.add(stringRequest);
    }
}


this is main activity,it is supposed to load categorylist as a fragment where i can manipulate with data
public class MainActivity extends AppCompatActivity{

    CategoryList ct;
    FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ct = new CategoryList();

        FragmentManager manager=getSupportFragmentManager();
        transaction=manager.beginTransaction();
        transaction.add(R.id.catframe, ct, "Frag_Top");
        transaction.commit();
        /*if (getFragmentManager().findFragmentById(R.id.catframe) == null) {
            getFragmentManager().beginTransaction().add(R.id.catframe,ct,"Fragment One" ).commit();
        }*/

    }
       /* ct = new CategoryList();

       final  FragmentManager fragmentManager = getFragmentManager();
         transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.catframe,ct,"Fragment one");
        transaction.commit();*/


    }
Posted
Updated 28-Nov-17 4:27am
v2
Comments
Richard MacCutchan 28-Nov-17 9:49am    
TL;DR
Member 13053943 28-Nov-17 11:08am    
what does it mean
Richard MacCutchan 28-Nov-17 11:37am    
It means that no one is going to try and debug all that code for you. Please remove all the code that is not related to your question and add some proper detail of what is going wrong.
David Crow 28-Nov-17 20:05pm    
Move this to the actual Android forum. Way too much here for a simple Q&A post.
Cashat Vcc 14-Apr-21 15:53pm    
Hey did you find the answer? Can u share the solutions with me ? I have the same problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900