Home » Android » java – How can i loop the textview based on array that i have in android?-Exceptionshub

java – How can i loop the textview based on array that i have in android?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

i have a response of JSONArray that look like this:

[ 
   { 
      "sub_directory_name":"Directory 1",
      "address":"Address 1",
      "email":[ 
         { 
            "key":0,
            "value":"Directory 1 Email"
         }
      ],
      "contact_no":[ 
         { 
            "key":0,
            "value":"Directory 1 Contact Num"
         }
      ],
      "description":""
   },
   { 
      "sub_directory_name":"Directory 2",
      "address":"Directory 2 Address",
      "email":[ 
         { 
            "key":0,
            "value":"Directory 2 Email"
         }
      ],
      "contact_no":[ 
         { 
            "key":0,
            "value":"Directory 2 Contact"
         }
      ],
      "description":""
   },
   { 
      "sub_directory_name":"Directory 3",
      "address":"Directory 3 Address",
      "email":[ 
         { 
            "key":0,
            "value":"Directory 3 Email 3.1"
         },
         { 
            "key":1,
            "value":"Directory 3 Email 3.2"
         }
      ],
      "contact_no":[ 
         { 
            "key":0,
            "value":"Directory 3 Contact 3.1"
         },
         { 
            "key":1,
            "value":"Directory 3 Contact 3.2"
         }
      ],
      "description":""
   },
   { 
      "sub_directory_name":"Directory 4",
      "address":"Directory 4 Address",
      "email":[ 
         { 
            "key":0,
            "value":"Directory 4 Email 4.1"
         },
         { 
            "key":1,
            "value":"Directory 4 Email 4.2"
         },
         { 
            "key":2,
            "value":"Directory 4 Email 4.3"
         }
      ],
      "contact_no":[ 
         { 
            "key":0,
            "value":"Directory 4 Contact 4.1"
         },
         { 
            "key":1,
            "value":"Directory 4 Contact 4.1"
         }
      ],
      "description":""
   }
]

How can i make my view to be like this using textview?

Directory 1
Address: Address 1
Email: Directory 1 Email
Contact: Directory 1 Contact Num


Directory 2
Address: Directory 2 Address
Email: Director 2 Email
Contact: Directory 2 Contact

Directory 3
Address: Directory 3 Address
Email: Directory 3 Email 3.1
       Directory 3 Email 3.2
Contact: Directory 3 Contact 3.1
         Directory 3 Contact 3.2

Directory 4
Address: Directory 4 Address
Email:Directory 4 Email 4.1
      Directory 4 Email 4.2
      Directory 4 Email 4.3
Contact:Directory 4 Contact 4.1
        Directory 4 Contact 4.2

My current code is look this:

DirectoryCustomListAdapter

 public class DirectoryCustomListAdapter extends BaseAdapter {
    private ArrayList<DirectoryDetailsModel> listData;
    private LayoutInflater layoutInflater;

    public DirectoryCustomListAdapter(Context aContext, ArrayList<DirectoryDetailsModel> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(aContext);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.activity_detail_directories, null);
            holder = new ViewHolder();
            holder.tvSubDirectory = (TextView) convertView.findViewById(R.id.tvSubDirectory);
            holder.tvDirectoryAddress = (TextView) convertView.findViewById(R.id.tvDirectoryAddress);
            holder.tvDirectoryEmail = (TextView) convertView.findViewById(R.id.tvDirectoryEmail);
            holder.tvDirectoryContact = (TextView) convertView.findViewById(R.id.tvDirectoryContact);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvSubDirectory.setText(listData.get(position).getSub_directory_name());
        holder.tvDirectoryAddress.setText(listData.get(position).getSub_address());
        holder.tvDirectoryEmail.setText(listData.get(position).getSub_email().toString());
        holder.tvDirectoryContact.setText(listData.get(position).getSub_contact().toString());

        return convertView;
    }

    static class ViewHolder {
        TextView tvSubDirectory,tvDirectoryAddress,tvDirectoryEmail,tvDirectoryContact;
    }
}
MyModel

public class DirectoryDetailsModel {

    public String sub_directory_name;
    public String sub_address;
    private JSONArray sub_email;
    private JSONArray sub_contact;


    public String getSub_directory_name() {
        return sub_directory_name;
    }

    public void setSub_directory_name(String sub_directory_name) {
        this.sub_directory_name = sub_directory_name;
    }

    public String getSub_address() {
        return sub_address;
    }

    public void setSub_address(String sub_address) {
        this.sub_address = sub_address;
    }

    public JSONArray getSub_email() {
        return sub_email;
    }

    public void setSub_email(JSONArray sub_email) {
        this.sub_email = sub_email;
    }

    public JSONArray getSub_contact() {
        return sub_contact;
    }

    public void setSub_contact(JSONArray sub_contact) {
        this.sub_contact = sub_contact;
    }


}
MainActivity

public class DirectoryDetailActivity extends AppCompatActivity {

    JSONArray subdirectory_details;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_directories);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        // add back arrow to toolbar
        if (getSupportActionBar() != null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        ArrayList details = getListData();
        final ListView listViewDirectory = (ListView) findViewById(R.id.custom_list);
        listViewDirectory.setAdapter(new DirectoryCustomListAdapter(this, details));

    }

    private ArrayList getListData() {
        ArrayList<DirectoryDetailsModel> results = new ArrayList<DirectoryDetailsModel>();
        try{

            subdirectory_details  = new JSONArray(getIntent().getStringExtra("subdirectory_details"));

            for (int i=0; i < subdirectory_details.length(); i++)
            {
                JSONObject detail_object = subdirectory_details.getJSONObject(i);

                // get all value here

                String detail_sub_directory = detail_object.getString("sub_directory_name");

                Log.d("detail_directory_subsss",detail_sub_directory);


            }

        }
        catch (org.json.JSONException jsone){
            jsone.printStackTrace();
        }

        return results;

    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

}

I am trying to store each on the ArrayList, i am still trying to experiment it when i tried to use arraylist i don’t know if i am doing it right but it is only displaying the last element in the array. is there an easy way i can do this on textview? any help will be really appreciated.

How to&Answers:

Instead of looping Use recyclerview with viewholders (reusable view)