如何在 Android 中动态添加 ListView 中的元素?


本示例演示了如何在 Android 中动态添加 ListView 中的元素。

步骤 1 − 在 Android Studio 中创建新项目,转到文件 ⇒ 新建项目,然后填写所有必需的详细信息以创建一个新项目。

步骤 2 − 将以下代码添加到 res/layout/activity_main.xml。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity" >

   <Button
      android:id="@+id/button1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/editText1"
      android:layout_centerHorizontal="true"
      android:text="Add Values to listView" />

   <EditText
      android:id="@+id/editText1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="26dp"
      android:ems="10"
      android:hint="Add elements listView" />

   <ListView
      android:id="@+id/listView1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/button1"
      android:layout_centerHorizontal="true" >
   </ListView>
</RelativeLayout>

步骤 3 − 将以下代码添加到 src/MainActivity.java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

   ListView listview;
   Button addButton;
   EditText GetValue;
   String[] ListElements = new String[] {
      "Android",
      "PHP",
      "Python",
   };
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      listview = findViewById(R.id.listView1);
      addButton = findViewById(R.id.button1);
      GetValue = findViewById(R.id.editText1);

      final List<String> ListElementsArrayList = new ArrayList<>(Arrays.asList(ListElements));
      final ArrayAdapter<String> adapter = new ArrayAdapter<>
      (MainActivity.this, android.R.layout.simple_list_item_1, ListElementsArrayList);
      listview.setAdapter(adapter);

      addButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            ListElementsArrayList.add(GetValue.getText().toString());
            adapter.notifyDataSetChanged();
         }
      });
   }
}

步骤 4 - 将以下代码添加到 androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="app.com.sample">

   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

让我们尝试运行你的应用程序。我假设你已经将实际 Android 移动设备连接到了计算机。要从 Android Studio 运行应用程序,请打开项目的某个活动文件,然后单击工具栏上的“运行” 播放图标 图标。选择你的移动设备作为选项,然后查看将显示默认屏幕的移动设备 –


更新日期:03-Jul-2020

3000+ 次查看

启动你的职业生涯

完成课程并获得认证

开始
广告
© . All rights reserved.