mainActivity

 avatar
Trrieu112233
java
2 months ago
1.6 kB
6
Indexable
package com.example.testapp2;

import android.os.Bundle;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class mainActivity extends AppCompatActivity {
    ListView lvAccounts;
    ArrayList<Account> accountList;
    AccountAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Anh xa listView
        lvAccounts = findViewById(R.id.listViewAccount);

        // Tai data tu shared preferences
        loadData();

        // Khoi tao Adapter
        adapter = new AccountAdapter(this, R.layout.item_account, accountList);
        // Gan adapter cho listView
        lvAccounts.setAdapter(adapter);
    }

    private void loadData(){
        accountList = new ArrayList<>();
        try {
            JSONArray jsonArray = JsonPrefHelper.getAccountsArray(this);

            for(int i = 0; i < jsonArray.length(); i ++){
                JSONObject obj = jsonArray.getJSONObject(i);

                // Tao object Account tu JSON
                Account acc = new Account(
                        obj.getString("appname"),
                        obj.getString("username"),
                        obj.getString("password"),
                        obj.getString("package"));
                accountList.add(acc);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
Editor is loading...
Leave a Comment