从一个包含多个值的JSONObject单个字符串中在Recyclerview中显示数据。

huangapple go评论74阅读模式
英文:

Displaying data in Recyclerview from a JSONobject single string with multiple values

问题

我有一个名为DataListjsonarray,其中有一个名为Datajsonobject,字符串具有不同的值,这些值由字符"´"分隔,这些值分别对应于"Headers"对象,我需要在回收站中显示这些值,如SL.,InNo等。通过拆分字符"´",我可以获得一个字符串数组,然后我需要从适配器将这些数据显示在不同的textview中,任何想法都将非常有帮助。

    "MainData": {
        "Headers": "SL.>´InNo. - Supp<´InvNo.<´Date^´Value>´Disc.>´Rate´Others>´Amount>",
        "FieldSeparator": "´",
        "DataList": [
            {
                "Data": "1. ´19110 / Textiles´003220´01-sep-2019´70,605.00´0.00´530.25´982.75´118.00´",
                "DataInputType": 1
            },
			 {
                "Data": "2. ´19111 / Textiles´7041´01-sep-2019´8,895.00´0.00´444.75´173.25´513.00´",
                "DataInputType": 1
            },
英文:

I have a DataList jsonarray with a jsonobject as Data , the string has different values which is seprated by character "´" , the values are respectively corresponding
to the "Headers" object , i need to display this in a recycler view as SL.,InNo,etc., how can i achieve this by spliting the characher "´" which gives a string array,i
furthur need to display this data from adapter to different textview, any ideas would be really helpful.

   "MainData": {
       "Headers": "SL.>´InNo. - Supp<´InvNo.<´Date^´Value>´Disc.>´Rate´Others>´Amount>",
       "FieldSeparator": "´",
       "DataList": [
           {
               "Data": "1. ´19110 / Textiles´003220´01-sep-2019´70,605.00´0.00´530.25´982.75´118.00´",
               "DataInputType": 1
           },
   		 {
               "Data": "2. ´19111 / Textiles´7041´01-sep-2019´8,895.00´0.00´444.75´173.25´513.00´",
               "DataInputType": 1
           },
   		

答案1

得分: 2

首先,您有多种方法来执行该任务,

首先,提取所需信息为字符串,然后您可以使用

替换函数将 ''' 更改为 '',了解更多关于在Java中处理字符串的信息

提取:https://stackoverflow.com/questions/1688099/converting-json-data-to-java-object

替换函数:https://stackoverflow.com/questions/7552253/how-to-remove-special-characters-from-a-string

英文:

You have multiply approaches in order to preform that task,

first of all extract the needed information into string then you can use

replace function to change '`' into '' read more about string handling in java

extraction:
https://stackoverflow.com/questions/1688099/converting-json-data-to-java-object

replace function: https://stackoverflow.com/questions/7552253/how-to-remove-special-characters-from-a-string

答案2

得分: 2

以下是翻译好的内容:

假设您希望您的数据以类似于以下结构的可用形式存在。

[
    {
        "SL": "1",
        "InNo": "19910",
        ...
    },
    {
        "SL": "2",
        "InNo": "19911",
        ...
    }
]

正如其他人所提到的,想法是使用 `split("´")`,其余部分是您希望如何构造数据的方式。

使用类或方法来创建上述结构:

public class DefineData {

    // 假设以下所需的结构

    // [
    //    {
    //        SL : 1
    //        InNo: 19910
    //        ...
    //    },
    //    {
    //        SL : 2
    //        InNo: 19911
    //        ...
    //    }
    //
    // ]

    private ArrayList<HashMap<String, String>> dataArrayList;

    // 助手方法,请使用您自己的 JsonObject,而不是该方法
    public JSONObject getJsonObject() {
        String json = "..."; // 这里省略了大段 JSON 数据
        try {
            JSONObject obj = new JSONObject(json);
            return obj;
        } catch (Throwable tx) {
            Log.e("TAG", "getJsonObject: ", tx.getCause());
            throw new RuntimeException("");
        }
    }

    public DefineData() throws JSONException {
        dataArrayList = new ArrayList<>();

        // 假设现在暂时所有数据都是字符串
        JSONObject obj = getJsonObject();
        JSONObject mainData = obj.getJSONObject("MainData");
        String headers = mainData.getString("Headers");
        // 在您的情况下是 "&#180;",但最好从 JsonObject 中获取它
        String fieldSeparator = mainData.getString("FieldSeparator");
        JSONArray dataList = mainData.getJSONArray("DataList");

        // 遍历 dataList 并填充数据映射,并使用 FieldSeparator 拆分数据
        String[] headersArray = headers.split(fieldSeparator);

        for (int i = 0; i < dataList.length(); i++) {
            JSONObject dataJsonObject = dataList.getJSONObject(i);
            String dataString = dataJsonObject.getString("Data");
            String[] dataArray = dataString.split(fieldSeparator);
            // 遍历 dataArray
            HashMap<String, String> dataMap = new HashMap<>();
            for (int j = 0; j < dataArray.length; j++) {
                String dataItem = dataArray[j];
                String header = headersArray[j];
                dataMap.put(dataItem, header);
            }
            dataArrayList.add(dataMap);
        }

    }

    public ArrayList<HashMap<String, String>> getDataArrayList() {
        return dataArrayList;
    }
}


RecyclerView 的适配器应该类似于以下内容:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private ArrayList<HashMap<String, String>> dataArrayList;

    public MyAdapter(ArrayList<HashMap<String, String>> dataArrayList) {
        this.dataArrayList = dataArrayList;
    }

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

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 这里替换为您的根布局,而不是 view..
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_item, parent, false);

        // TextView txtView = view.findViewById(R.id.textView);

        MyViewHolder vh = new MyViewHolder(view);
        // vh.textView = txtView;
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        // 位置将类似于 DataList 位置,但这次我们有头部信息
        HashMap<String, String> key = dataArrayList.get(position);

        //String slVal = key.get("SL");
        //String inNoVal = key.get("InNo");
        // 或者直接遍历它们,以最适合的方式处理

        holder.textView.setText("所需值");
        // 对其余部分执行相同的操作...
    }

    // 视图持有者

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public View view;
        public TextView textView;
        // 视图...
        // 传入您的视图或布局 - RelativeLayout、ConstraintLayout
        public MyViewHolder(View view) {
            super(view);
            this.view = view;
        }
    }

    public ArrayList<HashMap<String, String>> getDataArrayList() {
        return dataArrayList;
    }

    public void setDataArrayList(ArrayList<HashMap<String, String>> dataArrayList) {
        this.dataArrayList = dataArrayList;
    }
}

然后,操作应该像这样简单:

MyAdapter myAdapter;
RecyclerView recyclerView;
// ...
// ...

DefineData defineData = null;
try {
    // 不要忘记传递您想要的 jsonObject!
    defineData = new DefineData();
} catch (Exception e) {
    Log.e("TAG", "MyAdapter: ", e.getLocalizedMessage());
}

mAdapter = new MyAdapter(defineData.getDataArrayList());
recyclerView.setAdapter(mAdapter);

请注意,这是您提供的代码的翻译版本。如果有任何翻译上的误差或不准确之处,请随时纠正我。

英文:

Assuming you want your data to be in a usable structure like that.

 [
    {
        &quot;SL&quot; : &quot;1&quot;
        &quot;InNo&quot;: &quot;19910&quot;
        ...
    },
    {
        &quot;SL&quot; : &quot;2&quot;
        &quot;InNo&quot;: &quot;19911&quot;
        ...
    }

 ]

As others have mentioned the idea is to use the split(&quot;&#180;&quot;) the rest are how you want to structure you data.

Use a class or a method to create the above structure:

public class DefineData {

// Assuming the below desired structure

// [
//    {
//        SL : 1
//        InNo: 19910
//        ...
//    },
//    {
//        SL : 2
//        InNo: 19911
//        ...
//    }
//
// ]

private ArrayList&lt;HashMap&lt;String, String&gt;&gt; dataArrayList;

// Helper method please use your own JsonObject instead of that method
public JSONObject getJsonObject() {
    String json = &quot;{ \&quot;MainData\&quot;:{ \&quot;Headers\&quot;:\&quot;SL.&gt;&#180;InNo. - Supp&lt;&#180;InvNo.&lt;&#180;Date^&#180;Value&gt;&#180;Disc.&gt;&#180;Rate&#180;Others&gt;&#180;Amount&gt;\&quot;, \&quot;FieldSeparator\&quot;:\&quot;&#180;\&quot;, \&quot;DataList\&quot;: [ { \&quot;Data\&quot;: \&quot;1. &#180;19110 / Textiles&#180;003220&#180;01-sep-2019&#180;70,605.00&#180;0.00&#180;530.25&#180;982.75&#180;118.00&#180;\&quot;, \&quot;DataInputType\&quot;:1 }, { \&quot;Data\&quot;:\&quot;2. &#180;19111 / Textiles&#180;7041&#180;01-sep-2019&#180;8,895.00&#180;0.00&#180;444.75&#180;173.25&#180;513.00&#180;\&quot;, \&quot;DataInputType\&quot;:1 }] } }&quot;;
    try {
        JSONObject obj = new JSONObject(json);
        return obj;
    } catch (Throwable tx) {
        Log.e(&quot;TAG&quot;, &quot;getJsonObject: &quot;, tx.getCause());
        throw new RuntimeException(&quot;&quot;);
    }
}

public DefineData() throws JSONException {
    dataArrayList = new ArrayList&lt;&gt;();


    // Assuming everything is a String for now
    JSONObject obj = getJsonObject();
    JSONObject mainData = obj.getJSONObject(&quot;MainData&quot;);
    String headers = mainData.getString(&quot;Headers&quot;);
    // In your case &quot;&#180;&quot; but it&#39;s a good practise to grab that from the JsonObject
    String fieldSeparator = mainData.getString(&quot;FieldSeparator&quot;);
    JSONArray dataList = mainData.getJSONArray(&quot;DataList&quot;);

    // Loop through dataList and populate the data map and split the data using the FieldSeparator
    String[] headersArray = headers.split(fieldSeparator);

    for (int i = 0; i &lt; dataList.length(); i++) {
        JSONObject dataJsonObject = dataList.getJSONObject(i);
        String dataString = dataJsonObject.getString(&quot;Data&quot;);
        String[] dataArray = dataString.split(fieldSeparator);
        // Loop through the dataArray
        HashMap&lt;String, String&gt; dataMap = new HashMap&lt;&gt;();
        for (int j = 0; j &lt; dataArray.length; j++) {
            String dataItem = dataArray[j];
            String header = headersArray[j];
            dataMap.put(dataItem, header);
        }
        dataArrayList.add(dataMap);
    }

}

public ArrayList&lt;HashMap&lt;String, String&gt;&gt; getDataArrayList() {
    return dataArrayList;
}
}

Your Adapter for the RecyclerView should look similar to that:

public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.MyViewHolder&gt; {

private ArrayList&lt;HashMap&lt;String, String&gt;&gt; dataArrayList;

public MyAdapter(ArrayList&lt;HashMap&lt;String, String&gt;&gt; dataArrayList) {
    this.dataArrayList = dataArrayList;
}

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

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    // Your root layout here instead of view..
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_item, parent, false);

    // TextView txtView = view.findViewById(R.id.textView);

    MyViewHolder vh = new MyViewHolder(view);
    // vh.textView = txtView;
    return vh;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    // The position will be similar to DataList position but this time we have the
    // information from the header
    HashMap&lt;String, String&gt; key = dataArrayList.get(position);

    //String slVal = key.get(&quot;SL&quot;);
    //String inNoVal = key.get(&quot;InNo&quot;);
    // Or simply iterate through them whatever works best

    holder.textView.setText(&quot;The desired value&quot;);
    // Do the same for the rest..
}


// VIEW HOLDER

public static class MyViewHolder extends RecyclerView.ViewHolder {
    public View view;
    public TextView textView;
    // Views....
    // Pass in your view or layout - RelativeLayout, ConstraintLayout
    public MyViewHolder(View view) {
        super(view);
        this.view = view;
    }
}


public ArrayList&lt;HashMap&lt;String, String&gt;&gt; getDataArrayList() {
    return dataArrayList;
}

public void setDataArrayList(ArrayList&lt;HashMap&lt;String, String&gt;&gt; dataArrayList) {
    this.dataArrayList = dataArrayList;
}

}

Then it should be as simple as:

    MyAdapter myAdapter;
    RecyclerView recyclerView;
    // ...
    // ...
    
    DefineData defineData = null;
    try {
        // Don&#39;t forget to pass in the jsonObject you want!!
        defineData = new DefineData();
    } catch (Exception e) {
        Log.e(&quot;TAG&quot;, &quot;MyAdapter: &quot;, e.getLocalizedMessage());
    }
    
    mAdapter = new MyAdapter(defineData.getDataArrayList());
    recyclerView.setAdapter(mAdapter);

答案3

得分: 0

首先,通过将JSON转换为POJO类对象,从MainData JSON对象中获取datalist。然后针对datalist中的每个Data字符串,将Data字符串进行拆分,并将每个拆分值存储/复制到相应的变量(例如,Sl. No.、InNo.等)。

要将字符串拆分为数组,请使用Strings的split函数。

String data = "1. &#180;19110 / Textiles&#180;003220&#180;01-sep-2019&#180;70,605.00&#180;0.00&#180;530.25&#180;982.75&#180;118.00&#180;";
String[] dataArray = str.split("&#180;", 0);

我建议您创建一个名为DataClass(或其他适合的名称)的类,并将所有标题添加为数据成员。一旦您有了dataArray,就创建一个新的DataClass对象,并将其添加到回收视图列表中。

英文:

First get the datalist from the MainData JSON object by converting the JSON to POJO Class object. Then for each Data string in the datalist, split the Data string and store/copy each split value to respective variables (i.e. Sl. No., InNo., etc.).

For splitting the string into an array, use split function of Strings.

String data = &quot;1. &#180;19110 / Textiles&#180;003220&#180;01-sep-2019&#180;70,605.00&#180;0.00&#180;530.25&#180;982.75&#180;118.00&#180;&quot;; 
String[] dataArray = str.split(&quot;&#180;&quot;, 0); 

I would suggest you create a class named DataClass ( or some other name that suits it) and add all headers as data members. Once you have the dataArray, create a new DataClass object and add it to the recycler view list.

huangapple
  • 本文由 发表于 2020年5月4日 16:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61587906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定