package com.artursworld.reactiontest.view.user;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.artursworld.reactiontest.R;
import com.artursworld.reactiontest.controller.adapters.MedicalUserListAdapter;
import com.artursworld.reactiontest.controller.util.UtilsRG;
import com.artursworld.reactiontest.model.entity.MedicalUser;
import com.artursworld.reactiontest.model.persistence.manager.MedicalUserManager;

import java.util.List;

public class MedicalUserListView extends AppCompatActivity {

    ListView listView;
    MedicalUserManager userDB;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_medical_user_list_view);
        userDB = new MedicalUserManager(getApplicationContext());
        initMedicalUserListView(userDB.getAllMedicalUsers());
    }

    private void initMedicalUserListView(List<MedicalUser> userList) {
        listView = (ListView) findViewById(R.id.medicalUserListView);
        boolean isEmptyUserList = true;
        if(userList != null){
            if(userList.size()> 0){
                isEmptyUserList = false;
            }

            String[] medicalIds = new String[userList.size()];
            int[] ages = new int[userList.size()];
            int[] images = new int[userList.size()];


            for(int i= 0 ; i<userList.size(); i++){
                medicalIds[i] = userList.get(i).getMedicalId();
                ages[i] = userList.get(i).getAge();
                images[i] = userList.get(i).getImage();
            }

            MedicalUserListAdapter adapter = new MedicalUserListAdapter(this, medicalIds, ages, images);
            listView.setAdapter(adapter);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    UtilsRG.info("selected user at position: "+position);
                    //Toast.makeText(getApplicationContext(), medicalIds[position], Toast.LENGTH_SHORT).show();
                }
            });
        }
        if (isEmptyUserList){
            TextView textView = (TextView) findViewById(R.id.medical_user_list_view_empty_list);
            textView.setText(R.string.no_user_in_db);
        }



    }
}

@@ -38,24 +38,85 @@
 view.user;%0A%0A
+import android.content.Context;%0Aimport android.os.AsyncTask;%0A
 import andro
@@ -152,16 +152,16 @@
 tivity;%0A
-
 import a
@@ -798,17 +798,45 @@
 userDB;%0A
+    private Context context;
 %0A
-
 %0A    @Ov
@@ -943,16 +943,48 @@
 State);%0A
+        this.context = context;%0A
         
@@ -1119,61 +1119,892 @@
     
-initMedicalUserListView(userDB.getAllMedicalUsers());
+getItemLists gfl = new getItemLists();%0A        gfl.execute();%0A        //initMedicalUserListView(userDB.getAllMedicalUsers());%0A    %7D%0A%0A    public class getItemLists extends AsyncTask%3CVoid, String, List%3CMedicalUser%3E%3E %7B%0A        @Override%0A        protected void onPreExecute() %7B%0A            super.onPreExecute();%0A        %7D%0A%0A        @Override%0A        protected void onProgressUpdate(String... values) %7B%0A            super.onProgressUpdate(values);%0A        %7D%0A%0A        @Override%0A        protected List%3CMedicalUser%3E doInBackground(Void... params) %7B%0A            MedicalUserManager dao = new MedicalUserManager(context);%0A            List%3CMedicalUser%3E fal = dao.getAllMedicalUsers();%0A            return fal;%0A        %7D%0A%0A        @Override%0A        protected void onPostExecute(List%3CMedicalUser%3E result) %7B%0A            super.onPostExecute(result);%0A            initMedicalUserListView(result);%0A        %7D
 %0A   

add example to async dao methods