在 Android Studio 中向列表视图添加自定义行

Posted

技术标签:

【中文标题】在 Android Studio 中向列表视图添加自定义行【英文标题】:Adding custom rows to listview in Android Studio 【发布时间】:2021-08-27 17:37:35 【问题描述】:

在我的 CT_Locations 活动中,我想在单击“添加断点”按钮时将自定义 XML 行 (row_location.xml) 添加到列表视图。 XML 文件有一个标签、一个 AutoCompleteTextView 和一个“取消”ImageButton。我已经创建了一个 Breakpoint 类和一个 BreakpointAdapter,但是当我单击按钮时,它并没有像预期的那样添加一行。 难道我做错了什么?谢谢!


activity_ct_locations_filters.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_
android:layout_
tools:context=".CT_Locations">

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/feefef"
    android:layout_
    android:layout_
    android:layout_marginTop="12dp"
    android:background="#ECECEC"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/listview">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_
        android:layout_
        android:background="#D5D5D5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">

        <AutoCompleteTextView
            android:id="@+id/destino"
            android:layout_
            android:layout_
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <TextView
        android:id="@+id/textView11"
        android:layout_
        android:layout_
        android:text="To"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.046"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.548" />

</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/sdwdw"
    android:layout_
    android:layout_
    android:background="#ECECEC"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.264">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_
        android:layout_
        android:background="#D5D5D5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <AutoCompleteTextView
            android:id="@+id/origem"
            android:layout_
            android:layout_
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <TextView
        android:id="@+id/textView10"
        android:layout_
        android:layout_
        android:text="From"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.048"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.548" />

</androidx.constraintlayout.widget.ConstraintLayout>

<ImageButton
    android:id="@+id/homeBtnShowTransports2"
    android:layout_
    android:layout_
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@android:drawable/ic_dialog_dialer" />

<ImageButton
    android:id="@+id/returnBtn2"
    android:layout_
    android:layout_
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@android:drawable/ic_menu_revert" />

<TextView
    android:id="@+id/CT_LocationFiltersTextView"
    android:layout_
    android:layout_
    android:layout_marginTop="28dp"
    android:text="Create Trip"
    android:textSize="28sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/filtersBtn1"
    android:layout_
    android:layout_
    android:layout_marginTop="44dp"
    android:text="Filter by"
    android:textSize="12sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/CT_LocationFiltersTextView" />

<Button
    android:id="@+id/searchResultsBtn"
    android:layout_
    android:layout_
    android:text="Search Results"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.489"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.939" />

<ImageButton
    android:id="@+id/swapBtn"
    android:layout_
    android:layout_
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.954"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.143"
    app:srcCompat="@android:drawable/ic_menu_rotate" />

<Button
    android:id="@+id/addBreakpoint"
    android:layout_
    android:layout_
    android:text="add Breakpoint"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.497"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/feefef"
    app:layout_constraintVertical_bias="0.101" />

<ListView
    android:id="@+id/listview"
    android:layout_
    android:layout_
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.49"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/sdwdw"
    app:layout_constraintVertical_bias="0.027" />

</androidx.constraintlayout.widget.ConstraintLayout>

CT_Locations.java

public class CT_Locations extends AppCompatActivity 

private Intent data;
private String depDate;
private String depTime;
private String arrTime;
private boolean bus;
private boolean train;
private boolean metro;
private String order;

private int numOfBreakpoints;
private ListView listView;
ArrayList<Breakpoint> breakpoints;
ArrayAdapter<String> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_ct_locations_filters);

    AutoCompleteTextView location2_1 = findViewById(R.id.origem);
    AutoCompleteTextView location2_2 = findViewById(R.id.destino);
    // update origin location with the main activity current location
    location2_1.setText(MainActivity.currentLocation);

    numOfBreakpoints = 0;
    listView = findViewById(R.id.listview);
    breakpoints = new ArrayList<Breakpoint>();

    BreakpointAdapter adapter = new BreakpointAdapter(this, breakpoints);
    ListView listView = (ListView) findViewById(R.id.listview);
    listView.setAdapter(adapter);

    Button filtersBtn = (Button) findViewById(R.id.filtersBtn1);
    filtersBtn.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            Intent goToFilters = new Intent(getApplicationContext(), CT_Filters.class);
            startActivityForResult(goToFilters,1);
        
    );

    ImageButton swapBtn = (ImageButton) findViewById(R.id.swapBtn);
    swapBtn.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            String location1 = location2_1.getText().toString();
            String location2 = location2_2.getText().toString();
            location2_1.setText(location2);
            location2_2.setText(location1);
        
    );

    Button addBreakpoint = (Button) findViewById(R.id.addBreakpoint);
    addBreakpoint.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
           //add xml row to the listview
            Breakpoint bk = new Breakpoint();
            adapter.add(bk);
        
    );

    Button searchResultsBtn = (Button) findViewById(R.id.searchResultsBtn);
    searchResultsBtn.setOnClickListener(new View.OnClickListener() 
        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void onClick(View v) 
            String location1 = location2_1.getText().toString();
            String location2 = location2_2.getText().toString();
            if (location1 != "" && location2 != "") 

                CT_SearchResults.origem = location1;
                CT_SearchResults.destino = location2;
                Intent goToResults = new Intent(getApplicationContext(), CT_SearchResults.class);
                startActivity(goToResults);

             else 
                System.out.println("Falta parametros");
            
        
    );



row_location.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_
android:layout_>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/constraintLayout4"
    android:layout_
    android:layout_
    android:layout_marginStart="24dp"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="29dp"
    android:layout_marginEnd="24dp"
    android:layout_marginRight="24dp"
    android:background="#ECECEC"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/rwgwrgwgwrgw"
        android:layout_
        android:layout_
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="28dp"
        android:layout_marginRight="28dp"
        android:text="Breakpoint"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/constraintLayout2"
        app:layout_constraintHorizontal_bias="0.048"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.49" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout2"
        android:layout_
        android:layout_
        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        android:background="#D5D5D5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/rwgwrgwgwrgw"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <ImageButton
            android:id="@+id/deleteBreakpoint"
            android:layout_
            android:layout_
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.936"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.485"
            app:srcCompat="@android:drawable/ic_menu_close_clear_cancel" />

        <AutoCompleteTextView
            android:id="@+id/locationAutoComplete"
            android:layout_
            android:layout_
            android:completionHint="Insert a location"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.175"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.52" />
    </androidx.constraintlayout.widget.ConstraintLayout>

  </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Breakpoint.java

public class Breakpoint 
public String location;

public String getLocation() 
    return location;


public void setLocation(String location) 
    this.location = location;


BreakpointAdapter.java

public class BreakpointAdapter extends ArrayAdapter<Breakpoint> 

public BreakpointAdapter(Context context, ArrayList<Breakpoint> breakpoints) 
    super(context, R.layout.row_location, breakpoints);


@Override
public View getView(int position, View convertView, ViewGroup parent) 
    Breakpoint breakpoint = getItem(position);
    if (convertView == null) 
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_location, parent, false);
    
    return convertView;


【问题讨论】:

【参考方案1】:

我已经有一段时间没有使用 ListView 或编码了,所以我可能错了。

向适配器添加项目后,您应该调用:

adapter.notifyDataSetChanged()

在你的情况下:

Button addBreakpoint = (Button) findViewById(R.id.addBreakpoint);
addBreakpoint.setOnClickListener(new View.OnClickListener() 
    @Override
    public void onClick(View v) 
       //add xml row to the listview
        Breakpoint bk = new Breakpoint();
        adapter.add(bk);
        adapter.notifyDataSetChanged() // Notify the adapter an item has been added
    
);

如果不对,试试这个:

Button addBreakpoint = (Button) findViewById(R.id.addBreakpoint);
addBreakpoint.setOnClickListener(new View.OnClickListener() 
    @Override
    public void onClick(View v) 
       //add xml row to the listview
        Breakpoint bk = new Breakpoint();
        breakpoints.add(bk);
        adapter.notifyDataSetChanged(); // Notify the adapter a list item has been added
    
);

【讨论】:

感谢您的回复!问题似乎是最初设置为 0dp 的列表视图的高度。我增加了大小,行开始填充列表视图。添加新行时如何增加列表视图的大小?如果用户还没有点击“添加断点”按钮,我想从this 更改为this。

以上是关于在 Android Studio 中向列表视图添加自定义行的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio:如何将列表视图中的项目添加到列表视图 onClick

列表视图和文本输入在 Java Android Studio 中工作异常

如何从列表视图中从 sqlite 检索数据单击并查看不同的活动 android studio

更改 android studio 中的 textview 样式

Python Django:在视图中向对象添加属性还是制作数据字典更好?

在android中向画布添加点而不刷新整个画布