Interface to send data from adapter to activity isn't functioning

Viewed 54

I am doing this for the first time, and I am a beginner, so please pardon any silly mistakes. I am working on a tasks app, and I basically have an adapter class and an activity, where I want the list view (for which the adapter is) to be displayed. I get some amount of data for the list-view from a previous activity, and then some data I set in the adapter class (integers like th, tm and tl as shown in code). Now, I want to send this data back to my activity, where I I'll be using this data (the integers) in a formula. Finally, I want to send the result of the formula back to the adapter to be displayed... I don't know if my thinking is correct and if this is the best method to do so, but I couldn't find any other way, since I have some data in the activity itself that is required for the formula. I tried doing this using Interface, as suggested on various websites. However, it isn't working at all. Please help me find the problem. I am also open to suggestions for more efficient ways of tackling my problem. :)

Code for Adapter Class :

    package com.example.taskmasterv3;

import android.app.Activity;
import android.content.Context;
import android.icu.text.UFormat;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.ListIterator;
import java.util.PriorityQueue;

public class
SubtaskDetailAdapter extends ArrayAdapter<SubtaskPartTwo> {



    DataTransferInterface dataTransferInterface;
    private final Context context;
    private ArrayList<SubtaskPartTwo> values;
    public boolean deleted;
    DBHelper dbHelper;
    int ph = 10;
    int pm = 9;
    int pl = 8;
    int th = 3;
    int tm = 2;
    int tl = 1;
    private int totalTime;
    private int x;
    int a;
    int b;
    int c;
    int d;
    int e;
    int f;
    int totaltimeints;
    int ans;
    ListIterator<SubtaskPartTwo> listIterator;

    public SubtaskDetailAdapter(Context context, ArrayList<SubtaskPartTwo> list, DataTransferInterface dataTransferInterface, Activity activity) {

        //since your are using custom view,pass zero and inflate the custom view by overriding getview

        super(context, 0 , list);
        this.context = context;
        this.values = list;
        this.dataTransferInterface = dataTransferInterface;




    }



    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {


        //check if its null, if so inflate it, else simply reuse it
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.task_summary_item, parent, false);
        }

        //use convertView to refer the childviews to populate it with data
        TextView tvSubtaskName = convertView.findViewById(R.id.tvlolitaskname);
        ImageView ivPri = convertView.findViewById(R.id.ivloliPri);
        ImageView ivTime = convertView.findViewById(R.id.ivloliTime);
        TextView tvEstTaskTime = convertView.findViewById(R.id.tvEstTaskTime);


        tvSubtaskName.setText(values.get(position).getSubtaskName());



        tvEstTaskTime.setText(values.get(position).getEstTaskTime() + "");



        if (values.get(position).getPriHigh() == 1)
        {
            ivPri.setImageResource(R.drawable.priority_high);
            values.get(position).setPri(ph);


        }
        if (values.get(position).getPriMed() == 1)
        {
            ivPri.setImageResource(R.drawable.priority_med);
            values.get(position).setPri(pm);
        }
        if (values.get(position).getPriLow() == 1)
        {
            ivPri.setImageResource(R.drawable.priority_low);
            values.get(position).setPri(pl);
        }

        if (values.get(position).getTimeMore() == 1)
        {
            ivTime.setImageResource(R.drawable.time_symbol_more);
            values.get(position).setTime(th);
        }
        if (values.get(position).getTimeMed() == 1)
        {
            ivTime.setImageResource(R.drawable.time_symbol_med);
            values.get(position).setTime(tm);
        }
        if (values.get(position).getTimeLess() == 1)
        {
            ivTime.setImageResource(R.drawable.time_symbol_less);
            values.get(position).setTime(tl);
        }

    dataTransferInterface.onSetValues(values);

        // THIS IS WHERE I USE SORT
        Collections.sort(values, new TaskComparator());


        //return the view you inflated
        return convertView;
    }





    public void addANewSubTask(SubtaskPartTwo newSubTask) {
        add(newSubTask);
        notifyDataSetChanged();

    }



    // this doesnt work yet, fix this bug ! (same on the other adapter)
    @Override
    public int getCount() {
        if (this.values.size()>11)
        {
            return 10;
        }
        else
        {
            return super.getCount();
        }
    }


}

Code for Activity :

package com.example.taskmasterv3;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.PriorityQueue;

public class TaskSummary extends AppCompatActivity implements DataTransferInterface {

    ListView lvTaskList;
    TextView tvBreak, tvBreakAfterEvery, txt1, txt2, text1, hmm;
    TextView break_duration_mins;
    ArrayList<SubtaskPartTwo> subtaskList = new ArrayList<>();
    String subtname;
    String pri;
    String time;
    int working_hours;
    int working_minutes;
    int totalTime;
    DBHelper dbHelper;
    int timeInts;
    int timeConstant;
    int answer;


    @Override
    public void onSetValues(ArrayList<SubtaskPartTwo> al) {

        for (int i1 = 0; i1 < al.size(); i1++){
            timeInts += al.get(i1).getTime();
        }

        timeConstant = totalTime/timeInts;

        for (int i2 = 0; i2 < al.size(); i2++){
            answer = (timeConstant * al.get(i2).getTime());
        }


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.tasksummary, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        dbHelper.deleteAll();

        if (item.getItemId() == R.id.restore) {
            Intent inti = new Intent(this, TaskInfo.class);
            startActivity(inti);

        }



        return super.onOptionsItemSelected(item);
    }

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






        lvTaskList = findViewById(R.id.lvTaskList);
        tvBreak = findViewById(R.id.tvBreak);
        tvBreakAfterEvery = findViewById(R.id.tvBreakAfterEvery);
        txt1 = findViewById(R.id.txt1);
        txt2 = findViewById(R.id.txt2);
        break_duration_mins = findViewById(R.id.break_duration_mins);
        text1 = findViewById(R.id.text1);
        hmm = findViewById(R.id.hmm);
        dbHelper = new DBHelper(this);

        working_hours = getIntent().getIntExtra("working_hours", 1);
        working_minutes = getIntent().getIntExtra("working_minutes", 0);
        int alol = working_hours * 60;
        int blol = working_minutes;
        totalTime = alol + blol;
        double without_break_hours = getIntent().getIntExtra("without_break_hours", 1);
        double without_break_minutes = getIntent().getIntExtra("without_break_minutes", 0);
        double break_duration = getIntent().getIntExtra("break_duration", 20);
        String a = working_hours + " h";
        txt1.setText(a);
        String b = working_minutes + " m";
        break_duration_mins.setText(b);
        String c = break_duration + " m";
        txt2.setText(c);



        //Mathematics

        double g = working_hours * 100;
        double h = g + working_minutes;
        double i = h + break_duration;
        double j = i / 60;
        double p = (int) j;
        double q = j - p;
        double r = q * 60;

        without_break_hours = p;
        without_break_minutes = r;

        String d = without_break_hours + " h";
        String e = without_break_minutes + " m";

        text1.setText(d);
        hmm.setText(e);





        Cursor res = dbHelper.getdata();
        SubtaskDetailAdapter adapter = new SubtaskDetailAdapter(this, subtaskList, this, this);
        lvTaskList.setAdapter(adapter);




        while (res.moveToNext() && res != null){




            SubtaskPartTwo subtaskPartTwo = new SubtaskPartTwo(res.getString(0), res.getInt(1), res.getInt(2), res.getInt(3), res.getInt(4), res.getInt(5), res.getInt(6), 0, 0, answer);
            //subtaskList.add(subtaskPartTwo);

            adapter.addANewSubTask(subtaskPartTwo);
        }



    }





}

Code for Interface :

package com.example.taskmasterv3;

import java.util.ArrayList;

public interface DataTransferInterface {

    public void onSetValues(ArrayList<SubtaskPartTwo> al);

} 
0 Answers
Related