Линейный вид в ScrollView с адаптером массива, увеличивающимся, а не уменьшающимся для новых элементов

#android #android-layout #android-fragments

#Android #android-макет #android-фрагменты

Вопрос:

У меня есть проект Java Android, над которым я работаю, в котором у меня есть LinearLayout, управляемый адаптером массива. Моя проблема заключается в том, что когда я добавляю новый элемент в адаптер массива и обновляю представление, список элементов растет вверх, а не вниз, как видно на изображениях ниже.

Почему это сейчас растет? и как я могу это сделать? Код следует за фотографиями введите описание изображения здесь

введите описание изображения здесь

Основная деятельность

 <?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"
    tools:context=".MainActivity"
    >

    <AnalogClock
        android:id="@ id/clockMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.1" />

    <TextView
        android:id="@ id/dateMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/clockMain"
         />

    <TextView
        android:id="@ id/titleMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/annie_use_your_telescope"
        android:text="@string/app_name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/dateMsg"
        app:layout_constraintVertical_bias="0.05"
        android:textSize="30sp"
        android:textColor="@color/colorPrimaryDark"
        />

    <ListView
        android:id="@ id/taskList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintVertical_bias="0.5"
        android:nestedScrollingEnabled="true"

        />
    <Button
        android:id="@ id/addNewTask"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_new_task"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintVertical_bias="0.95"
        />
</androidx.constraintlayout.widget.ConstraintLayout>
  

Список задач

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content"

    >
    <TextView
        android:id="@ id/taskDesc"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:textSize="15sp"
        android:text="testing task"
        android:layout_weight="1"
        />
    <TextView
        android:id="@ id/taskDate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="this is bull shit"
        android:layout_weight="1"
        />
    <TextView
        android:id="@ id/taskTime"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="some more bullish"
        android:layout_weight="1"

        />

</LinearLayout>
  

инфлятор

 @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflater.inflate(R.layout.task_list, null);
        TextView taskDesc = (TextView) view.findViewById(R.id.taskDesc);
        TextView taskDate = (TextView) view.findViewById(R.id.taskDate);
        TextView taskTime = (TextView)view.findViewById(R.id.taskTime);

        taskDesc.setText(tasks.get(i).description);
        taskDate.setText(tasks.get(i).taskDate.toString());
        taskTime.setText(tasks.get(i).taskTime.toString());

        return view;
    }
  
 import java.util.ArrayList;

public class TaskAdapter extends BaseAdapter {
    Context context;
    LayoutInflater inflater;
    ArrayList<Task> tasks;

    public TaskAdapter(Context applicationContext, ArrayList<Task> tasks) {
        this.context = applicationContext;
        this.tasks = new ArrayList<>(tasks);
        inflater = (LayoutInflater.from(applicationContext));
    }
    @Override
    public int getCount() {
        return tasks.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflater.inflate(R.layout.task_list, null);
        TextView taskDesc = (TextView) view.findViewById(R.id.taskDesc);
        TextView taskDate = (TextView) view.findViewById(R.id.taskDate);
        TextView taskTime = (TextView)view.findViewById(R.id.taskTime);

        taskDesc.setText(tasks.get(i).description);
        taskDate.setText(tasks.get(i).taskDate.toString());
        taskTime.setText(tasks.get(i).taskTime.toString());

        return view;
    }
}

  

Ответ №1:

Можете ли вы добавить используемый вами адаптер массива (как вы вставляете данные в arrayadapter) и куда вы передаете массив в listview.

Комментарии:

1. есть мысли …?

2. Извините, я не смог ответить за последние несколько дней. Ваша проблема в правильном макете? не логика адаптера и любых других вещей. как будто разница между Whats next и списком уменьшается, верно?