如何在 Android 的 ListView 中向链表插入元素?

androidmobile developmentapps/applications

本示例演示如何在 Android 的 ListView 中向链表插入元素。

步骤 1在 Android Studio 中创建一个新项目,依次点击"文件">"新建项目",并填写所有必需信息以创建一个新项目。

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

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayoutxmlns: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"
   android:orientation = "vertical">
   <EditText
      android:id = "@+id/name"
      android:layout_width = "match_parent"
      android:hint = "Enter Name"
      android:layout_height = "wrap_content" />
   <LinearLayout
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content">
      <Button
         android:id = "@+id/save"
         android:text = "Save"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" />
      <Button
         android:id = "@+id/refresh"
         android:text = "Refresh"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" />
   </LinearLayout>
   <ListView
      android:id = "@+id/listView"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content">
   </ListView>
</LinearLayout>

在上面的代码中,我们将名称设置为编辑文本框,当用户点击保存按钮时,数据将被存储到数组列表中。点击刷新按钮即可查看列表视图的更改。

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

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.LinkedList;
public class MainActivityextends AppCompatActivity {
   Button save, refresh;
   EditTextname;
   ArrayAdapterarrayAdapter;
   LinkedList<String>link_list;
   private ListViewlistView;
   @Override
   protected void onCreate(Bundle readdInstanceState) {
      super.onCreate(readdInstanceState);
      setContentView(R.layout.activity_main);
      link_list = new LinkedList<String>();
      name = findViewById(R.id.name);
      listView = findViewById(R.id.listView);
      findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            arrayAdapter.notifyDataSetChanged();
            listView.invalidateViews();
            listView.refreshDrawableState();
         }
      });
      findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if (!name.getText().toString().isEmpty()) {
               link_list.add(name.getText().toString());
               arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, link_list);
               listView.setAdapter(arrayAdapter);
               Toast.makeText(MainActivity.this, "Inserted", Toast.LENGTH_LONG).show();
            } else {
               name.setError("Enter NAME");
            }
         }
      });
   }
}

让我们尝试运行您的应用程序。我假设您已将 Android 移动设备连接到计算机。要从 Android Studio 运行应用程序,请打开项目中的一个 Activity 文件,然后单击工具栏中的"运行"图标 。选择您的移动设备作为选项,然后选中您的移动设备,它将显示您的默认屏幕。 –

在上面的结果中,我们将名称插入到链表中,并在列表视图中显示该名称。


相关文章