Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

java - Unable to add the task to listview in my tasks app

So I am working on a tasks app. I am trying to set up a functionality, that might sound a little complicated, but I want to put it to work. Hear me out : User types in a task name, and then clicks on an 'add subtask ' button. That takes him to another activity, where he types in his subtask name (edit text), and also tells me the priority of the task, and the amount of time it will take (users answers this in the form of radio buttons). Then, he clicks on the 'done' button, from which he is taken back to the previous activity, where he entered the task name. What I want, is that the subtask name, along with symbols of the priority and time are shown in a list there. I transfer the data of the subtask activity using intents, then I have also made a subtask object, which accepts the subtask name, and boolean values of the radio buttons. I also made a custom adapter class for my listview. But the issue is, it is just not showing. This is my code :

The subtask object class :

public class subtask {

    private String subtaskName;
    private boolean priHigh;
    private boolean priMed;
    private boolean priLow;
    private boolean timeMore;
    private boolean timeMed;
    private boolean timeLess;

    public subtask(String subtaskName, boolean priHigh, boolean priMed, boolean priLow, boolean timeMore, boolean timeMed, boolean timeLess) {
        this.subtaskName = subtaskName;
        this.priHigh = priHigh;
        this.priMed = priMed;
        this.priLow = priLow;
        this.timeMore = timeMore;
        this.timeMed = timeMed;
        this.timeLess = timeLess;
    }

    public String getSubtaskName() {
        return subtaskName;
    }

    public boolean isPriHigh() {
        return priHigh;
    }

    public boolean isPriMed() {
        return priMed;
    }

    public boolean isPriLow() {
        return priLow;
    }

    public boolean isTimeMore() {
        return timeMore;
    }

    public boolean isTimeMed() {
        return timeMed;
    }

    public boolean isTimeLess() {
        return timeLess;
    }
}

The adapter class :

public class SubtaskAdapter extends ArrayAdapter<subtask> {


    private final Context context;
    private final ArrayList<subtask> values;


    public SubtaskAdapter(Context context, ArrayList<subtask> list) {
        super(context, R.layout.subtask_item, list);
        this.context = context;
        this.values = list;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.subtask_item, parent, false);

        TextView tvSubtaskName = rowView.findViewById(R.id.tvSubtaskName);
        ImageView ivPri = rowView.findViewById(R.id.ivPri);
        ImageView ivTime = rowView.findViewById(R.id.ivTime);

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

        if (values.get(position).isPriHigh())
        {
            ivPri.setImageResource(R.drawable.priority_high);
        }
        else if (values.get(position).isPriMed())
        {
            ivPri.setImageResource(R.drawable.priority_med);
        }
       else if (values.get(position).isPriLow())
        {
            ivPri.setImageResource(R.drawable.priority_low);
        }

        if (values.get(position).isTimeMore())
        {
            ivTime.setImageResource(R.drawable.time_symbol_more);
        }
        else if (values.get(position).isTimeMed())
        {
            ivTime.setImageResource(R.drawable.time_symbol_med);
        }
        else if (values.get(position).isTimeLess())
        {
            ivTime.setImageResource(R.drawable.time_symbol_less);
        }

        return rowView;
    }
}

Here I receive the input from the subtasks activity and try and put it in the listview :

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == ENTER_SUBTASK)
        {
            if (resultCode == RESULT_OK)
            {
                String subtaskName = data.getStringExtra("subtaskName");
                boolean priHigh = data.getBooleanExtra("priHigh", false);
                boolean priMed = data.getBooleanExtra("priMed", false);
                boolean priLow = data.getBooleanExtra("priLow", false);
                boolean timeMore = data.getBooleanExtra("timeMore", false);
                boolean timeMed = data.getBooleanExtra("timeMed", false);
                boolean timeLess = data.getBooleanExtra("timeLess", false);

                subtask subtask = new subtask(subtaskName, priHigh, priMed, priLow, timeMore, timeMed, timeLess);
                subtaskList.add(subtask);
                SubtaskAdapter adapter = new SubtaskAdapter(this, subtaskList);
                lvSubtasks.setAdapter(adapter);



            }

        }


    }

xml for the main activity :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    tools:context=".TaskInfo">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteY="163dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/floating_hint_taskname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:counterEnabled="true"
                app:counterMaxLength="20"
                app:counterTextColor="@color/white"
                app:hintTextAppearance="@style/FlotatingHintStyle">


                <EditText
                    android:id="@+id/etTaskName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="16dp"
                    android:ems="10"
                    android:fontFamily="@font/roboto"
                    android:hint="@string/name_your_task"
                    android:inputType="textPersonName"
                    android:maxLength="20"
                    android:textColor="@color/black"
                    android:textColorHint="#B8AEAE"
                    android:textSize="14sp" />

            </com.google.android.material.textfield.TextInputLayout>

            <ListView
                android:id="@+id/lvSubtasks"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <Button
                android:id="@+id/btnNewSubtask"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginTop="5dp"
                android:layout_marginRight="32dp"
                android:layout_marginBottom="16dp"
                android:fontFamily="@font/roboto"
                android:text="@string/add_subtask" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tvEnterTime"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_marginBottom="8dp"
                    android:fontFamily="@font/roboto"
                    android:text="@string/estimated_working_time"
                    android:textColor="#B8AEAE"
                    android:textSize="16sp" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/floating_hint_time_hrs"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:hintTextAppearance="@style/FlotatingHintStyle">


                        <EditText
                            android:id="@+id/etWorkingHrs"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="16dp"
                            android:layout_marginRight="16dp"
                            android:layout_weight="1"
                            android:ems="10"
                            android:fontFamily="@font/roboto"
                            android:hint="@string/hours"
                            android:inputType="number"
                            android:maxLength="2"
                            android:textColorHint="#B8AEAE"
                            android:textSize="14sp" />
                    </com.google.android.material.textfield.TextInputLayout>


                    <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/floating_hint_time_mins"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:hintTextAppearance="@style/FlotatingHintStyle">


                        <EditText
                            android:id="@+id/etWorkingMins"
                            android:layout_width="wrap_content"
                            an

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are many issues

  1. I would suggest to initialise adapter at start of your code and then use adapter.notifyDataSetChanged(); to populate new data.

  2. public class subtask, If you are using Java, your class names are not consistent with java standard programming style. Try renaming it to SubTask.

  3. You can show more entries by overriding the default getCount method

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

  1. The main problem in your code is
 if (resultCode == ENTER_SUBTASK)
    {
       if (resultCode == RESULT_OK)

You are using resultCode for both, You might need to change

resultCode == ENTER_SUBTASK 

to

requestCode == ENTER_SUBTASK

Further information can be found at

Getting a result from an activity


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...