Android Studio 中 ArrayLists 的适配器究竟是如何工作的?

如何解决Android Studio 中 ArrayLists 的适配器究竟是如何工作的?

我正在尝试在 Android Studio 中构建一个待办事项列表应用程序,该应用程序将列表保存为文本文件,然后当您再次打开应用程序时,如果有存储任务的文本文件,它将初始化应用程序中的数组列表使用这些数据。

在我不得不用存储的文件更新 arraylist 之前,它与 arraylist 及其适配器都运行良好。现在它打印但不是打印带有任务的字符串而是打印一个奇怪的内部内容,例如“com.example.todolist.MyData@fe7bcc9”

我环顾四周,并不能确定我是否可以使用同一个适配器两次(更新它?),而且通常任何有关您使用它的经验的信息都会非常有帮助。

主要java代码:

package com.example.todolist;

import android.content.Context;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.EditText;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import java.io.*;


import java.io.OutputStreamWriter;
import java.util.ArrayList;

 public class MainJava extends AppCompatActivity implements AdapterView.OnItemClickListener {

     // 1. Some variables and definitions
        ListView listView;
        ArrayList<MyData> arrayList = new ArrayList<>();
        MyAdapter adapter,adapter2;
        private EditText taskEntered;
        private int itemPos;
        private final String file = "list.txt";
        private String line;
        private OutputStreamWriter out;


     // 2. Method: The classic ON CREATE
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            // locate the widgets: edit text (to get string) and list view (to put string)
            taskEntered = (EditText) findViewById(R.id.enterTask);
            listView = findViewById(R.id.listView);

            // attach listener to list view
            listView.setOnItemClickListener(this);

            taskEntered.setText("");

            //hide title and icon in action bar  EDIT TO HIDE SAVE AND CLOSE
            ActionBar actionBar = getSupportActionBar();
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayUseLogoEnabled(false);

            //open output stream
            //try {
               // out = new OutputStreamWriter(openFileOutput(file,MODE_PRIVATE)); // also try MODE_APPEND
          //  } catch (IOException e) {}

            readFromFile();

        }

     // 3. Method: ON CREATE OPTIONS MENU
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.menu,menu);
         return true;
     }


     // 4. Method: ON OPTIONS SELECTED
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {

         int itemID = item.getItemId();  // get id of menu item picked

         switch (itemID) {

             case R.id.add :
                 addNewTask();   // prints entered task in list view
                 return true;

             case R.id.delete :
                 deleteTask();
                 return true;

             case R.id.update :
                 updateTask();
                 return true;

             case R.id.save :
                 saveToFile();
                 return true;

             case R.id.close :
                 saveToFile();
                 finish();

             default: super.onOptionsItemSelected(item);
         }
         return false;
     }


    // 5. Method: PRINT NEW TASK in list view and ADD NEW TASK to arrayList
     public void addNewTask() {
         // get string from the edit text
         String taskString = taskEntered.getText().toString();

         // create ArrayList values
         arrayList.add(new MyData(arrayList.size() +1,taskString));
         // TESTING: arrayList.add(new MyData(2," Robert"));

         //create custom adapter and connect to ListView
         adapter = new MyAdapter(this,arrayList);
         listView.setAdapter(adapter);

         taskEntered.setText("");                          // clear edit text bar after adding item

     }

     // 5. Method:
     public void addNewTask(String string) {

         // create ArrayList values
         arrayList.add(new MyData(arrayList.size() +1,string));

         //create custom adapter and connect to ListView
         adapter = new MyAdapter(this,arrayList);
         listView.setAdapter(adapter);
     }

     // 6. Method: Listener,when you click on an item in list view it does this:

     // listener is passed into the position of the item that was clicked?

     public void onItemClick(AdapterView<?> parent,View v,int position,long id) {

            itemPos = position;
            // you click on an item in list view
         String task = arrayList.get(position).getName();      // get name of task
         int numTask = arrayList.get(position).getNum();        // get number of task

         // set edit text field to show the item you just clicked
         taskEntered.setText(numTask + " " + task);
     }

     // 7. Method: Delete a task

     public void deleteTask() {

         // remove the arrayList item in the position that was clicked
         arrayList.remove(itemPos);

         // loop through items and reassign numbers
         for (int i=0; i < arrayList.size(); i++){
             arrayList.get(i).setNum(i+1);
         }

         //create custom adapter and connect to ListView
         adapter = new MyAdapter(this,arrayList);
         listView.setAdapter(adapter);

         taskEntered.setText("");                                     // clearing edit text widget
     }

     // 7. Method: Update a task

     public void updateTask() {

         // get string from the edit text
         String taskString = taskEntered.getText().toString();

         String updatedString= taskString.substring(2);  // so that the task number is not repeated

         arrayList.get(itemPos).setName(updatedString);

         //create custom adapter and connect to ListView
         adapter = new MyAdapter(this,arrayList);
         listView.setAdapter(adapter);

         taskEntered.setText("");                                     // clearing edit text widget
     }

     // 8. Method: Save contents of app to a file

     public void saveToFile() {
         try {
             // writing!

             //open output stream try catch
             try {
                 out = new OutputStreamWriter(openFileOutput(file,MODE_PRIVATE)); // also try MODE_APPEND
             } catch (IOException e) {}

             // loop through items in ArrayList
             for (int i=0; i < arrayList.size(); i++){
                 line = arrayList.get(i).toString();        // grab each line from the to do list
                 out.write(line + " \n");             // write each line on TEXT file
             }
             out.close();                           // close output stream

         } catch (IOException e) {
             Log.e("IOTest",e.getMessage());
         }
     }

     // 9. Method: Read from file

     public void readFromFile() {

         // check if file already exists,if not - finish,if yes - open stream and update app with that

            // A) if file doesn't exist leave method
            if ( !fileExists(this,"list.txt") ){
                return;
            }

            // B) if file already exists,open a stream,read it,copy it to ToDoList app
            else {
                arrayList.clear();
                try {

                    // reading!
                    InputStream in = openFileInput(file);            // open stream for reading from file
                    InputStreamReader isr = new InputStreamReader(in);
                    BufferedReader reader = new BufferedReader(isr);
                    String str = null;

                    // loop to read lines from file and print in app
                    while ((str = reader.readLine()) != null) {      // str = each line in the text file OR MAYBE THIS STRING IS THE RAW CODE FROM EACH LINE

                        addNewTask(str);
                        //str.toString();
                        //arrayList.add(new MyData(arrayList.size() +1,str));    // create ArrayList value of the string in the text file
                       // adapter = new MyAdapter(this,arrayList);            // create custom adapter and connect to ListView
                        //listView.setAdapter(adapter);
                    }

                    reader.close();               //close input stream

                } catch (IOException e) {
                    Log.e("IOTest",e.getMessage());
                }
            }
     }

     // 10. Method: Check if file exists

     public boolean fileExists(Context context,String filename) {
         File file = context.getFileStreamPath(filename);
         if(file == null || !file.exists()) {
             return false;
         }
         return true;
     }


    }

Adapter.java 代码:

package com.example.todolist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;

/*
 ADAPTER
  inflate xml file called row.xml
  get references (id's) for widgets in row.xml
  add each value on arrayList to its widget
 */

public class MyAdapter extends ArrayAdapter {
    private Context context;
    private ArrayList<MyData> arrayList;
    private TextView listNum,name,contactNum;

    // calling 3 arg constructor of parent class and storing values stored in
    public MyAdapter(Context context,ArrayList<MyData> arrayList) {
        super(context,arrayList);
        this.context = context;
        this.arrayList = arrayList;
    }

    // all the action is in this getView method
    // calls layout inflater class and gives it the layout of the row
    @Override
    public View getView(int position,View convertView,ViewGroup parent) {

        // call inflater to create a View from an xml layout file
        convertView = LayoutInflater.from(context).inflate(R.layout.row,parent,false);

        // get references for row widgets
        listNum = convertView.findViewById(R.id.listNum);   // id´s of widgets in MyAdapter.java
        name = convertView.findViewById(R.id.task);

        // ArrayList provides values that go along in a row when getView is finished it goes through entire ArrayList and
        listNum.setText(" " + arrayList.get(position).getNum());
        name.setText(arrayList.get(position).getName());
        return convertView;
    }
}

我的数据代码:

package com.example.todolist;

public class MyData {

    private int listNum;
    private String name;

    //constructors
    public MyData(int num,String name) {
        this.listNum = num;
        this.name = name;
    }

    //accessors and mutators
    public int getNum() {
        return listNum;
    }
    public void setNum(int num) {
        this.listNum = num;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }


}

解决方法

请在您的 MyData 类中添加此方法。

@Override
public String toString() {
return listNum+","+ name;
}

这是完整的课程,只需复制并粘贴即可

public class MyData {
    private int listNum;
    private String name;
    //constructors
    public MyData(int num,String name) {
        this.listNum = num;
        this.name = name;
    }
    //accessors and mutators
    public int getNum() {
        return listNum;
    }
    public void setNum(int num) {
        this.listNum = num;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
   @Override
    public String toString() {
        return listNum+","+ name;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-