Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Json textview to Json viewlist

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 626
    Answer it
    public class ExistingCSheet extends AppCompatActivity {
     
        private TextView listing;
        String[] items;
        ListView lv;
     
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_existing_csheet);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
     
            //new JSONTask().execute("https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesDemoList.txt");
            new JSONTask().execute("http://192.168.1.102/PSF/api/product\n");
     
           // listing = (TextView) findViewById(R.id.ecsList);
     
            lv = (ListView) findViewById(R.id.esList);
     
     
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent(ExistingCSheet.this, NewCSheet.class);
                    intent.putExtra("in",lv.getItemAtPosition(i).toString());
                    startActivity(intent);
                }
            });
     
     
        }
     
     
       
        public class JSONTask extends AsyncTask<String, String, String> {
            @Override
            protected String doInBackground(String... params) {
                HttpURLConnection connection = null;
                BufferedReader reader = null;
     
                try {
                    URL url = new URL(params[0]);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.connect();
     
                    InputStream stream = connection.getInputStream();
     
                    reader = new BufferedReader(new InputStreamReader(stream));
     
                    StringBuffer buffer = new StringBuffer();
     
                    String line = "";
                    while ((line = reader.readLine()) != null) {
                        buffer.append(line);
                    }
     
                    String finalJson = buffer.toString();
     
                    //   JSONObject parentObject = new JSONObject(finalJson);
                    //   JSONArray parentArray = parentObject.getJSONArray("");
                    JSONArray parentArray = new JSONArray(finalJson);
     
                    StringBuffer finalBufferedData = new StringBuffer();
                    for (int i = 0; i < parentArray.length(); i++) {
                        JSONObject finalObject = parentArray.getJSONObject(i);
                        String t1 = finalObject.getString("ib_itemcode1");
                        String t2 = finalObject.getString("transtatuscode");
                        String t3 = finalObject.getString("invtid");
                        String t4 = finalObject.getString("descr");
     
                        // int year = finalObject.getInt("transtatuscode");
     
     
                        finalBufferedData.append(t1 + " - " + t2 + " - " + t3 + " - " + t4 + "\n");
                    }
                    //JSONObject finalObject = parentArray.getJSONObject(0);
                    return finalBufferedData.toString();
     
                    //return buffer.toString();
     
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                    try {
                        if (reader != null) {
                            reader.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }
     
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
     
                listing.setText(result);
                //not done yet
            }
     
     
     
        }
    }

    Hi everyone, now what i am trying to do is to display the json in android, however i only manage to dump all of them into a  textview. 

    I need to place them in viewlist, in such a way that every row consist of example "ib_itemcode1":2131 "transtatuscode":123 "invtid":1231 "descr":123123.

    I am still reading up on arrayAdapter however i think the bigger problem is how do i assign the json data into the array adapter.

     

    Anyhow will be very thankful.

    Thank you again for helping out!

 1 Answer(s)

  • First: create a model class like this:-
    public class MyJSONBean {
     
       /** Property ib_itemcode1 */
       String ib_itemcode1;
     
       /** Property transtatuscode */
       String transtatuscode;
     
       /** Property invtid */
       String invtid;
     
       /** Property descr */
       String descr;
     
       /**
        * Gets the ib_itemcode1
        */
       public String getIb_itemcode1() {
          return this.ib_itemcode1;
       }
     
       /**
        * Sets the ib_itemcode1
        */
       public void setIb_itemcode1(String value) {
          this.ib_itemcode1 = value;
       }
     
       /**
        * Gets the transtatuscode
        */
       public String getTranstatuscode() {
          return this.transtatuscode;
       }
     
       /**
        * Sets the transtatuscode
        */
       public void setTranstatuscode(String value) {
          this.transtatuscode = value;
       }
     
       /**
        * Gets the invtid
        */
       public String getInvtid() {
          return this.invtid;
       }
     
       /**
        * Sets the invtid
        */
       public void setInvtid(String value) {
          this.invtid = value;
       }
     
       /**
        * Gets the descr
        */
       public String getDescr() {
          return this.descr;
       }
     
       /**
        * Sets the descr
        */
       public void setDescr(String value) {
          this.descr = value;
       }
    }

    Second: declare a List:-
    List<MyJSONBean> myList = new ArrayList<>();

    Third: inside doInBackground method, put values from the JSON inside the list
    Modify the for loop like this:-
     
    for (int i = 0; i < parentArray.length(); i++) {
        MyJSONBean myBean = new MyJSONBean();
    
        JSONObject finalObject = parentArray.getJSONObject(i);
        myBean.setIb_itemcode1(finalObject.getString("ib_itemcode1"));
        myBean.setTranstatuscode(finalObject.getString("transtatuscode"));
        myBean.setInvtid(finalObject.getString("invtid"));
        myBean.setDescr(finalObject.getString("descr"));
    
        myList.add(myBean);                    
                       
                    }

    Fourth: inside onPostExecute method, using myList call the adapter class and set the adapter either in ListView or RecyclerView.
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: