Hi in the below I am displaying operator list displaying using recyclerview.
For this created Table layout contains table row .each row contains data that is coming from server and display via table format.
Response from server:
[{"email":"awdw@dv.vdv","id":"20","mobileNumber":"undefined","username":"akash4345678"}]
But data is displaying in vertial format want to display horizontally.
Expected output:
username email MobileNumber
akash4345678 awdw@dv.vdv undefined
output for below code:
Username Email MobileNumber
akash4345678
awdw@dv.vdv
undefined
Manage_operatorlist.java:
private void doTheAutoRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Write code for your refresh logic
progressDialog = new ProgressDialog (getActivity ());
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Communicating...");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
progressDialog.show();
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API.URL_BASE)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client (client)
.build();
API service = retrofit.create (API.class);
Call<List<GetOperatorList>> userCall = service.getopetaorlist ();
userCall.enqueue(new Callback<List<GetOperatorList>> () {
@Override
public void onResponse(Call <List<GetOperatorList>> call, Response <List<GetOperatorList>> response) {
if(response.isSuccessful()) {
progressDialog.dismiss ( );
Log.d ("Response body", new Gson ( ).toJson (response.body ( )));
String Status = new Gson ( ).toJson (response.body ( ));
JSONArray jsonArray = null;
try {
JSONObject jsonObject=null;
jsonArray = new JSONArray (Status);
arrayList = new ArrayList <> ( );
String id;
// String name[]={} ;
for (int i=0;i<jsonArray.length ();i++) {
username=jsonArray.getJSONObject (i).getString ("username");
email=jsonArray.getJSONObject (i).getString ("email");
mobileNumber=jsonArray.getJSONObject (i).getString ("mobileNumber");
//email=jsonArray.getJSONObject (i).getString ("email");
//mobile_number=jsonArray.getJSONObject (i).getString ("MobileNumber");
Typeface typeface = Typeface.createFromAsset(getActivity ().getAssets (), "fonts/astype - Secca Light.otf");
arrayList.add (username);
arrayList.add (email);
arrayList.add (mobileNumber);
// arrayList.add (email);
// arrayList.add (mobile_number);
}
// Creating Adapter object
SwipeRecyclerViewAdapter mAdapter = new SwipeRecyclerViewAdapter((Context) getActivity (), arrayList);
// Setting Mode to Single to reveal bottom View for one item in List
// Setting Mode to Mutliple to reveal bottom Views for multile items in List
((SwipeRecyclerViewAdapter) mAdapter).setMode(Attributes.Mode.Single);
mRecyclerView.setAdapter(mAdapter);
} catch (JSONException e) {
e.printStackTrace ( );
}
}
else {
progressDialog.dismiss ();
Log.d("Response errorBody", String.valueOf(response.errorBody()));
}
}
@Override
public void onFailure(Call<List<GetOperatorList>> call, Throwable t) {
// lv.setAdapter (adapter);
System.out.println("onFailure");
System.out.println(t.fillInStackTrace());
progressDialog.dismiss();
Toast.makeText(getActivity (), "Some error occurred -> ", Toast.LENGTH_LONG).show();;
// progressDialog.dismiss();
}
});
}
}, 5000);
return ;
}
ManageOperatorlist.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/realmsbg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center_horizontal"
android:background="@drawable/layout_border">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/manage_operator"
android:layout_centerHorizontal="true"
android:padding="12dp"
android:background="@drawable/layout_border"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center"
android:text="Manage Operator List"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="10dp"
android:paddingLeft="5dp"
android:orientation="horizontal">
<TableRow android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/txtRank"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="top"
android:layout_marginRight="10dp"
android:text="UserName"
android:background="@drawable/cellborder"
android:textColor="@color/color_white"
android:textSize="18sp" />
<TextView
android:id="@+id/txtMovieName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:layout_gravity="top"
android:layout_marginRight="40dp"
android:text="Email"
android:textColor="@color/color_white"
android:background="@drawable/cellborder"
android:textSize="18sp" />
<TextView
android:id="@+id/txtYear"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="top"
android:text="MobileNumber"
android:textColor="@color/color_white"
android:layout_marginRight="10dp"
android:background="@drawable/cellborder"
android:textSize="18sp" />
</TableRow>
</TableLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:orientation="horizontal" />
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="No Records"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
SwipeAdapter.java:
@Override
public void onBindViewHolder(final SimpleViewHolder viewHolder, final int position) {
int rowPos = viewHolder.getAdapterPosition ();
final String item = String.valueOf (operatorlist.get (position));
String[] strArray = new String[]{item};
System.out.println (strArray);
viewHolder.tvEmail.setText (strArray[0]);
// viewHolder.tvName.setText (strArray[1]);
// String[] strArray1=new String[]{String.valueOf (strArray)};;
// for (int i=0;i<strArray.length;i++){
// strArray1=strArray;
//
//
// // viewHolder.tvEmail.setText (strArray1[1].get());
//
// }
// System.out.println (strArray[1]); //prints "name"
// viewHolder.tvName.setText ((strArray[3]));
// viewHolder.tvEmail.setText (strArray[0]);
// viewHolder.txtRank.setText (strArray[1]);
viewHolder.swipeLayout.setShowMode (SwipeLayout.ShowMode.PullOut);
// Drag From Left
viewHolder.swipeLayout.addDrag (SwipeLayout.DragEdge.Left, viewHolder.swipeLayout.findViewById (R.id.bottom_wrapper1));
// Drag From Right
viewHolder.swipeLayout.addDrag (SwipeLayout.DragEdge.Right, viewHolder.swipeLayout.findViewById (R.id.bottom_wrapper));
// Handling different events when swiping
viewHolder.swipeLayout.addSwipeListener (new SwipeLayout.SwipeListener ( ) {
@Override
public void onClose(SwipeLayout layout) {
//when the SurfaceView totally cover the BottomView.
}
@Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
//you are swiping.
}
@Override
public void onStartOpen(SwipeLayout layout) {
}
@Override
public void onOpen(SwipeLayout layout) {
//when the BottomView totally show.
}
@Override
public void onStartClose(SwipeLayout layout) {
}
@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
//when user's hand released.
}
});