jyothi 34567
jyothi 34567

Reputation: 39

How to add all the list of string in android

Hi in the below code How to add all the list of items.

final String item = String.valueOf ((operatorlist.get (position)));

For the above line if I am trying print the item in log I am getting one.How to change it to list of items.

can any one help me

Response:

[{"email":"awdw@dv.vdv","id":"20","mobileNumber":"undefined","username":"akash4345678"},{"email":"1231312@gmail.com","id":"16","mobileNumber":"123456677896","username":"31413241"}]

Adapter.java:

public class SwipeRecyclerViewAdapter extends RecyclerSwipeAdapter<SwipeRecyclerViewAdapter.SimpleViewHolder> {


private Context mContext;
private ArrayList operatorlist;
ArrayList arrayList;
Dialog myDialog;
String building_name;


public SwipeRecyclerViewAdapter(Context context, ArrayList <List<GetOperatorList>> String) {
    this.mContext = context;
    this.operatorlist = String;
}

@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleview_operator_item, parent, false);
    myDialog = new Dialog(mContext);
    return new SimpleViewHolder(view);
}

@Override
public void onBindViewHolder(final SimpleViewHolder viewHolder, final int position) {

    int rowPos  = viewHolder.getAdapterPosition ();


    final String item = String.valueOf (operatorlist.get (position));
    try{
        JSONArray jsonarr = new JSONArray(item);
        ArrayList arrayList=new ArrayList();
        for(int i=0;i<jsonarr.length ();i++){
            JSONObject jsonobj = jsonarr.getJSONObject(i);
            String username = String.valueOf (jsonobj.get ("username"));
            arrayList.add (username);
            Log.d("yogesh","username = "+username);
        }
    }catch (Exception e){
        Log.d("yogesh","Eception"+e);
    }

Upvotes: 1

Views: 282

Answers (2)

Yogesh Bangar
Yogesh Bangar

Reputation: 550

Please use JSONArray and JSONObject to solve the issue

as I use your string that you gave above in string XML file and use in below code and its working at my side

[{"email":"awdw@dv.vdv","id":"20","mobileNumber":"undefined","username":"akash4345678"},{"email":"1231312@gmail.com","id":"16","mobileNumber":"123456677896","username":"31413241"}]

    public class SwipeRecyclerViewAdapter extends RecyclerView.Adapter<SwipeRecyclerViewAdapter.SimpleViewHolder> {


    private Context mContext;
    private ArrayList operatorlist;
    ArrayList arrayList;
    Dialog myDialog;
    String building_name;


    public SwipeRecyclerViewAdapter(Context context, ArrayList <List<GetOperatorList>> String) {
        this.mContext = context;
        this.operatorlist = String;
    }

    public class SimpleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView mtxtName,mtxtMsg,mtxtDate;
        private SimpleViewHolder(View view) {
            super(view);
            mtxtName = view.findViewById(R.id.name);
        }
        @Override
        public void onClick(View view) {}
        final protected Context getContext() {
            return itemView.getContext();
        }
    }
    @Override
    public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleview_operator_item, parent, false);
        myDialog = new Dialog(mContext);
        return new SimpleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final SimpleViewHolder viewHolder, final int position) {

        int rowPos = viewHolder.getAdapterPosition();


        final String item = String.valueOf(operatorlist.get(position));
        viewHolder.mtxtName.setText(item);
    }

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

recycleview_operator_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardUseCompatPadding="true">
    <TextView android:layout_height="wrap_content" android:layout_width="match_parent"
        android:text="name" android:id="@+id/name" android:textStyle="bold"/>
</LinearLayout>

please check my log

D/yogesh: username = akash4345678 D/yogesh: username = 31413241

Upvotes: 1

Parham
Parham

Reputation: 116

You can use gson to store class objects in string.

Add the GSON dependency in your Gradle file:

implementation 'com.google.code.gson:gson:2.8.5'

And in your class:

Gson gson = new Gson();
String json = gson.toJson(operatorlist.get (position));
Log.v("TAG", json);

Above code can be used to Log one object of the list. if you want all of them you can simply use a loop to iterate through them.

Upvotes: 0

Related Questions