无法将任务添加到我的任务应用程序的列表视图中



所以我正在开发一个任务应用程序。我正在尝试设置一个功能,听起来可能有点复杂,但我想让它发挥作用。听我说:用户键入任务名称,然后单击"添加子任务"按钮。这将他带到另一个活动,在那里他键入子任务名称(编辑文本(,还"告诉"我任务的优先级和所需的时间(用户以单选按钮的形式回答(。然后,他点击"完成"按钮,从中返回到上一个活动,在那里他输入了任务名称。我想要的是,子任务名称,以及优先级和时间的符号显示在那里的列表中。我使用intents传输子任务活动的数据,然后我还创建了一个子任务对象,它接受子任务名称和单选按钮的布尔值。我还为我的listview创建了一个自定义适配器类。但问题是,它只是没有表现出来。这是我的代码:

子任务对象类:

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;
}
}

适配器类别:

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;
}
}

在这里,我接收来自子任务活动的输入,并尝试将其放在列表视图中:

@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用于主要活动:

<?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"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:ems="10"
android:fontFamily="@font/roboto"
android:hint="@string/minutes"
android:inputType="number"
android:maxLength="2"
android:textColorHint="#B8AEAE"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>

</LinearLayout>
<TextView
android:id="@+id/tvMaxTimeWithoutBreak"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:fontFamily="@font/roboto"
android:text="@string/time_you_can_work_for_without_a_break"
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_hours"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/FlotatingHintStyle">

<EditText
android:id="@+id/etWorkingHours"
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_minutes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/FlotatingHintStyle">

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

</LinearLayout>
<TextView
android:id="@+id/tvBreakDuration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:fontFamily="@font/roboto"
android:text="@string/break_duration"
android:textColor="#B8AEAE"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/ivLeft"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_weight="1"
android:clickable="true"
android:longClickable="false"
app:srcCompat="@drawable/arrow_left" />
<TextView
android:id="@+id/tvBreakTime"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="10"
android:textColor="@color/white"
android:textSize="24sp" />
<ImageView
android:id="@+id/ivRight"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_weight="1"
android:clickable="true"
android:longClickable="false"
app:srcCompat="@drawable/arrow_right" />
</LinearLayout>
</LinearLayout>

</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

附带说明一下,您的viewInflation逻辑是错误的,当前您没有回收视图,因此在SubtaskAdapter类中进行以下修改

public SubtaskAdapter(Context context, ArrayList<subtask> list) {
//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;
}
@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.subtask_item, parent, false);
}
//use convertView to refer the childviews to populate it with data
TextView tvSubtaskName = convertView.findViewById(R.id.tvSubtaskName);
ImageView ivPri = convertView.findViewById(R.id.ivPri);
ImageView ivTime = convertView.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 the view you inflated
return convertView;
}
//to keep adding the new subtasks try the following
public addANewSubTask(subtask newSubTask){
ArrayList<subtask> newvalues = new ArrayList<subtask>(this.values);
newvalues.add(newSubTask);
this.values = newvalues
notifyDataSetChanged()
}

onActivityResult中生成以下参数

subtask subtask = new subtask(subtaskName, priHigh, priMed, priLow, timeMore, timeMed, timeLess);
adapter.addANewSubTask(subtask)

存在许多问题

  1. 我建议在代码开始时初始化适配器,然后使用adapter.notifyDataSetChanged();来填充新数据。

  2. public class subtask,如果您使用的是Java,那么您的类名与Java标准编程风格不一致。尝试将其重命名为SubTask。

  3. 您可以通过覆盖默认的getCount方法来显示更多条目

@覆盖公共int getCount(({return values.size((;}

  1. 代码中的主要问题是
if (resultCode == ENTER_SUBTASK)
{
if (resultCode == RESULT_OK)

您同时使用resultCode,您可能需要更改

resultCode == ENTER_SUBTASK 

requestCode == ENTER_SUBTASK

更多信息可在上找到

从活动中获取结果

最新更新