lundi 8 juin 2015

Connect TODOList with Dashbord - Displaying last item from the database

I would like to advise you how I view on the Dashboard My last position with a database that contains a list of things to do. This is a simple app I hope someone will be able to help.

This is the code of my list of tasks. There is a view, but the last item I would like to display on the dashboard , such as the name of task, place and date.

package pl.edu.ug.aib.studentizerApp.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import org.androidannotations.annotations.EFragment;

import java.util.ArrayList;
import java.util.List;

import pl.edu.ug.aib.studentizerApp.R;
import pl.edu.ug.aib.studentizerApp.todoList.DatabaseHandler;
import pl.edu.ug.aib.studentizerApp.todoList.Task;


@EFragment(R.layout.fragment_todo)
public class TODOFragment extends Fragment {

    EditText zadanieTxt, opisTxt, dataTxt, adresTxt;
    List<Task> Tasks = new ArrayList<Task>();
    ListView TaskListView;
    DatabaseHandler dbHandler;
    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.fragment_todo, container, false);
    }
    @Override
    public void onStart() {
        super.onStart();


        zadanieTxt = (EditText) getView().findViewById(R.id.txtZadanie);
        opisTxt = (EditText) getView().findViewById(R.id.txtOpis);
        dataTxt = (EditText) getView().findViewById(R.id.txtData);
        adresTxt = (EditText) getView().findViewById(R.id.txtAdres);
        TaskListView = (ListView) getView().findViewById(R.id.listView);
        dbHandler = new DatabaseHandler(getActivity().getApplicationContext());

        TabHost tabHost = (TabHost) getView().findViewById(R.id.baner);

        tabHost.setup();

        TabHost.TabSpec tabSpec = tabHost.newTabSpec("Dodaj zadanie");
        tabSpec.setContent(R.id.tabZadanie);
        tabSpec.setIndicator("Dodaj Zadanie");
        tabHost.addTab(tabSpec);

        tabSpec = tabHost.newTabSpec("lista");
        tabSpec.setContent(R.id.tabListaZadan);
        tabSpec.setIndicator("Lista");
        tabHost.addTab(tabSpec);

        final Button addBtn = (Button) getView().findViewById(R.id.btnAdd);
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Task task = new Task(dbHandler.getTaskCount(), String.valueOf(zadanieTxt.getText()), String.valueOf(opisTxt.getText()), String.valueOf(dataTxt.getText()), String.valueOf(adresTxt.getText()));
                if (!taskExists(task)) {
                    dbHandler.createZadanie(task);
                    Tasks.add(task);
                    Toast.makeText(getActivity().getApplicationContext(), String.valueOf(zadanieTxt.getText()) + " zostało dodane do listy zadań!", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(getActivity().getApplicationContext(), String.valueOf(zadanieTxt.getText()) + " Zadanie już istnieje", Toast.LENGTH_SHORT).show();
            }
        });

        zadanieTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                addBtn.setEnabled(String.valueOf(zadanieTxt.getText()).trim().length() > 0);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });



        if (dbHandler.getTaskCount() != 0)
            Tasks.addAll(dbHandler.getAllTasks());

        populateList();
    }

    private boolean taskExists(Task task) {
        String zadanie = task.getZadanie();
        int taskCount = Tasks.size();

        for (int i = 0; i < taskCount; i++) {
            if (zadanie.compareToIgnoreCase(Tasks.get(i).getZadanie()) == 0)
                return true;
        }
        return false;
    }


    private void populateList() {
        ArrayAdapter<Task> adapter = new TaskListAdapter();
        TaskListView.setAdapter(adapter);
    }

    private class TaskListAdapter extends ArrayAdapter<Task> {
        public TaskListAdapter()
        {

            super (TODOFragment.this.getActivity(), R.layout.listview_item, Tasks);
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            if (view == null)
                view = getActivity().getLayoutInflater().inflate(R.layout.listview_item, parent, false);

            Task currentTask = Tasks.get(position);

            TextView zadanie = (TextView) view.findViewById(R.id.zadanie);
            zadanie.setText(currentTask.getZadanie());
            TextView opis = (TextView) view.findViewById(R.id.opis);
            opis.setText(currentTask.getOpis());
            TextView data = (TextView) view.findViewById(R.id.data);
            data.setText(currentTask.getData());
            TextView adres = (TextView) view.findViewById(R.id.adres);
            adres.setText(currentTask.getAdres());

            return view;
        }
    }



}

Aucun commentaire:

Enregistrer un commentaire