android - 如何在Android中添加所有字符串列表

标签 android

嗨,下面的代码如何添加所有项目列表。

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

对于上面的行,如果我尝试在日志中打印项目,我会得到一个。如何将其更改为项目列表。

谁能帮我

响应:

[{"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);
    }

最佳答案

请使用JSONArray和JSONObject解决问题

当我使用您在字符串XML文件中上面给出的字符串并在下面的代码中使用它时,它在我的身边工作

[{“ email”:“ awdw@dv.vdv”,“ id”:“ 20”,“ mobileNumber”:“ undefined”,“ username”:“ akash4345678”},{“ email”:“ 1231312@gmail.com “,” id“:” 16“,” mobileNumber“:” 123456677896“,”用户名“:” 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>


请检查我的日志

D / yogesh:用户名= akash4345678
D / yogesh:用户名= 31413241

关于android - 如何在Android中添加所有字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57589012/

相关文章:

Android 在包中传递持久信息还是使用单例模式?

android - CallOut 弹出窗口不会通过放大/缩小来定位自身

android - 测试应用内计费 : "The publisher cannot purchase this item"

java - 错误 : Cannot access onCameraIdleListener

android - 是否可以使用内容 ://as a source for an <audio> element in a WebView

android - 检测应用程序何时保持屏幕开启

java - 为什么找不到这些符号?

java - Android:为什么我无法将图像上传到Web服务器?

java - 如何在 Android 上将 ISO 8601 字符串解析为 Java 日期

android - 如何在mysql中插入图像-android