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.

4
  • 3
    Still using listView? Use recyclerview with horizontal layout orientation. Commented Apr 3, 2017 at 10:11
  • Instead of ListView it's better to use RecyclerView Commented Apr 3, 2017 at 10:16
  • RecyclerView is made for the same purpose. Commented Apr 3, 2017 at 10:31
  • Use RecyclerView or HorizontalScrollView instead ListView. Commented Apr 3, 2017 at 10:43

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

11 Comments

No adapter attached; skipping layout getting this in logcat
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
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.
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()
Are you using cardview for each item ? If you don't wanna have spacing between items then use linearLayout instead of cardview
|
0
LinearLayoutManager layoutManager= new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setLayoutManager(layoutManager); 

use recylerview.

Comments

0

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

Comments

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

1 Comment

Links can become obsolete, Pls include relevant code as well when answering
0

Use RecyclerView to set your list Items.

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

2 Comments

where did i put this code?
Better to use Recyclerview @WealInfotech
0

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

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

4 Comments

Why not RecyclerView instead of some charming external library?
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.
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 -> developer.android.com/training/material/lists-cards.html
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.