0

Before this i am using ListView and it is working fine. But i want to set the ListView in Horizontal. so i came to know that RecycleView is more reliable then listview.

am trying to work with Recycle View and getting error like

 No adapter attached; skipping layout

It means that the problem is in my adapter class i don't know what the code is required in my adapter class i just tried this way.

main.java

public class DynamicButon extends Fragment {
    public DynamicButon(){};
    JSONObject jsonobject;
    JSONArray ownerObj;
     private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;


    ListViewAdapterDynamic mAdapter;
    ArrayList<HashMap<String, String>> arraylist;
    String uid = "0";
    SessionManager session;
    private static String url_visitor = "";
    JSONParser jParser = new JSONParser();
    ArrayList<String> itemwod = new ArrayList<String>();

    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_dynamic_buton, container, false);
        getActivity().setTitle("Complete Order");
        // listview = (ListView) view.findViewById(R.id.lvvisit);

        session = new SessionManager(getActivity());
        HashMap<String, String> user = session.getUserDetails();
        uid = user.get(SessionManager.KEY_ID);
        new DownloadJSON().execute();
        return view;
    }

    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
          //  avi.show();
        }

        @Override
        protected Void doInBackground(Void... args) {
            try {
                arraylist = new ArrayList<HashMap<String, String>>();
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", uid));
                JSONObject json = jParser.makeHttpRequest(url_visitor, "GET", params);

                int success1 = Integer.parseInt(json.getString("success4"));
                Log.d("success4", json.toString());

                if (success1 == 0) {
                    Snackbar.make(view, "Not Data Found", Snackbar.LENGTH_LONG).show();
                }
                if (success1 == 1) {
                    ownerObj = json.getJSONArray("cy");

                    for (int i = 0; i < ownerObj.length(); i++) {
                        jsonobject = ownerObj.getJSONObject(i);

                        item.add(jsonobject.getString("company"));
                    }
                }
            } catch (Exception e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {


 mRecyclerView = (RecyclerView)getActivity().findViewById(R.id.mylist);
        mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new ListViewAdapterDynamic(getActivity(), itemwod);
        mRecyclerView.setAdapter(mAdapter);
        }
    }
}

ListViewAdapter

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by sachin on 3/15/2017.
 */

public class ListViewAdapterDynamic extends BaseAdapter {
    Context cntx;

    View view;

    ArrayList<String> itemwod = new ArrayList<String>();

    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapterDynamic(Context context,
                                        ArrayList<String> o_parties
    ) {
        // TODO Auto-generated constructor stub
        cntx = context;
        itemwod = o_parties;
    }

    @Override
    public int getCount() {
        return itemwod.size();
    }

    @Override
    public Object getItem(int position) {
        return itemwod.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("deprecation")
    public View getView(final int position, View convertView, ViewGroup parent) {

        Button cancel;

        inflater = (LayoutInflater) cntx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LayoutInflater inflater = LayoutInflater.from(cntx);

        convertView = inflater.inflate(R.layout.raw_dynamic, parent,
                false);


         cancel=(Button)convertView.findViewById(R.id.btn) ;
        cancel.setText(itemwod.get(position));
        return convertView;
    }


}

Like this way i try but not working in ListViewAdapterDynamic i tried to implement

  RecyclerView.Adapter<MyViewHolder>

then getting errors that's why i just kept extends Baseadapter what i did i listview.

Andie
  • 96
  • 3
  • 21

6 Answers6

2

Use RecyclerView instead of ListView. I bet you won't regret. Set LayoutManager to HORIZONTAL. Do as follow:

First ADD RecyclerView in xml:

  <android.support.v7.widget.RecyclerView
       android:id="@+id/recycler_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

In fragment

private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;

In onPostExecute():

mRecyclerView = (RecyclerView)getActivity().findViewById(R.id.recycler_view);
mLayoutManager = new LinearLayoutManager(
                this,
                LinearLayoutManager.HORIZONTAL,
                false
        );
mRecyclerView.setLayoutManager(mLayoutManager);

Here is a simple tutorial you can follow for Horizontal RecyclerView.

tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
  • No adapter attached; skipping layout getting this in logcat – Andie Apr 04 '17 at 05:39
  • Can you please suggest me some detail code for this? i am very new in this and i don't know how the recycle view is working. i worked with only ListView.. that's why.. and i just update my code – Andie Apr 04 '17 at 05:55
  • what's the code is required on adapter class? – Andie Apr 04 '17 at 05:56
  • 1
    It's same as ListView with more features. You have to create custom Adapter for RecycleView as You created for ListView. Check out the Tutorial link I gave you above. If you find any difficulty to understand please let me know. – tahsinRupam Apr 04 '17 at 06:06
  • in my code i paste my ListViewAdapter in this what should i change in code? – Andie Apr 04 '17 at 06:16
  • 1
    Yes, I've seen it. Don't modify ListViewAdapter. Create a new adapter like ColorsAdapter in the tutorial. Modify it according to your need. Set that custom adapter as you set to listView in onPostExecute() – tahsinRupam Apr 04 '17 at 06:19
  • thank you it is working perfectly and data is fetching horizontally but the problem is it is using more space in between the list i.e. "Myfirst list"................."MySecondList"...................."MythirdList"..... the all dots are blank space it is dispalying like this.. – Andie Apr 04 '17 at 06:36
  • 1
    Are you using cardview for each item ? If you don't wanna have spacing between items then use linearLayout instead of cardview – tahsinRupam Apr 04 '17 at 06:39
  • nope am using LinearLayout only.. – Andie Apr 04 '17 at 06:41
  • should i update my xml file too? – Andie Apr 04 '17 at 06:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139819/discussion-between-tahsinrupam-and-weal-infotech). – tahsinRupam Apr 04 '17 at 06:43
0

Use RecyclerView to set your list Items.

reference https://acadgild.com/blog/horizontal-listview-android/

Abhishek Kumar
  • 1,255
  • 13
  • 21
0
LinearLayoutManager layoutManager= new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(layoutManager);

use recylerview.

0

Use Recycle View with horizonatal orientation follow this link https://developer.android.com/training/material/lists-cards.html

jay
  • 1
  • 1
0

Here you can find tutorial on how to set listview horizontally using recycler view

https://www.codeproject.com/Articles/1152595/Android-Horizontal-ListView-Tutorial

listview doesn't support horizontal scrolling in linear layout

enter image description here

0

Use this library for Horizontal ListView. Works like a charm.

https://github.com/MeetMe/Android-HorizontalListView

  • Why not RecyclerView instead of some charming external library? – Mohammed Atif Apr 03 '17 at 10:32
  • It can be achieved both ways, but listview is still one of the more flexible UI widget than recyclerView. However, the recyclerView is more optimized and gaining features as we speak. – Simranjeet Singh Apr 03 '17 at 10:36
  • **but listview is still one of the more flexible UI widget than recyclerView** Please get your facts corrected @Simranjeet. RecyclerView was made to cover the flexibility issues involved with ListView. Read this for more details -> https://developer.android.com/training/material/lists-cards.html – Mohammed Atif Apr 03 '17 at 10:40
  • I think if you don't know ... most of the advanced and flexible functionality of recyclerView can be achieved using ViewHolder. Moreover, I have never said in my post that listivew is better than recyclerView. Its everybody's personal preference which one they want to use while coding and the above said person asked for a horizontal listview. The listview will get depreciated someday. – Simranjeet Singh Apr 03 '17 at 10:53