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.
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.