Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android ArrayList:将多个值存储到配置文件中_Java_Android_Xml_Arraylist - Fatal编程技术网

Java Android ArrayList:将多个值存储到配置文件中

Java Android ArrayList:将多个值存储到配置文件中,java,android,xml,arraylist,Java,Android,Xml,Arraylist,我有多个变量,我们称它们为varA,varB,varC,和varD。按下按钮时,这些4个变量将一起存储在ArrayList中。这将创建一个可由变量varA查看的条目。所有条目都可以在列表视图中查看,该视图通过阵列适配器完成 onCreate ArrayAdapter<String> profileAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList); p

我有多个变量,我们称它们为
varA
varB
varC
,和
varD
。按下按钮时,这些
4个变量将一起存储在ArrayList中。这将创建一个可由变量varA查看的条目。所有条目都可以在
列表视图
中查看,该视图通过
阵列适配器
完成

onCreate

ArrayAdapter<String> profileAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
    profileList.setAdapter(profileAdapter);
ArrayAdapter profileAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,arrayList);
setAdapter(profileAdapter);
当按下按钮时

// Add data to a new profile in @+id/profileList ListView
ArrayList<String> arrayList= new ArrayList<>();
arrayList.add(varA);
System.out.println("varA's");
System.out.println(arrayList);
((ArrayAdapter)profileList.getAdapter()).notifyDataSetChanged();
//将数据添加到@+id/profileList列表视图中的新配置文件
ArrayList ArrayList=新的ArrayList();
arrayList.add(varA);
System.out.println(“varA's”);
System.out.println(arrayList);
((ArrayAdapter)profileList.getAdapter()).notifyDataSetChanged();
布局

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/overviewLayout">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/profileList"/>
    </RelativeLayout>


我对显示已添加项的列表视图有问题。此外,我还试图使用
TinyDB
存储这些项目,以便以后可以引用它们,因为按键时使用的ArrayList只是临时的。我也很难弄清楚如何设置它,以便每次按下按钮时,这4个变量将保持在一个“配置文件”中,这样它们就不会与以后可能添加的其他数据混淆。

为了避免重复存储值,请检查它是否已经存在:

    if(!arrayList.contains(varA)){
      arrayList.add(varA); 
    }
对于listview更新问题,请使用notifyItemInserted(position)而不是notifyDataSetChanged()


据我所知,您希望显示一个列表,该列表在一个条目中包含4个数据,并且只显示其中的第一个数据

如果是,您可以创建一个自定义的
模型
,这样会更容易。这将在一个对象中存储数据:

ProfileModel
    |
    |-- varA
    |-- varB
    |-- varC
    '-- varD
您的自定义模型可能如下所示:

public class ProfileModel {

    private String varA;
    private float varB;
    private int varC;
    private String varD;

    // getters
    ...
    // setters
    ...

    public ProfileModel() { } 
}
在开始时初始化配置文件列表:

ArrayList<ProfileModel> list = new ArrayList<>();
由于您希望用其他数据填充适配器,而不是简单的
字符串
,而是自定义模型,因此我更喜欢使用自定义适配器。创建一个并不是那么难,因为有时候,您可能希望在更新时有不同的行为,我也更喜欢使用自定义的
notifyDataSetChanged
方法。
下面是一个简单的适配器,其布局与您使用的适配器相同:

public class ProfileAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<ProfileModel> listProfiles;

    public ProfileAdapter(Context context, ArrayList<ProfileModel> listProfiles) {
        super();
        this.context = context;
        this.listProfiles = listProfiles;
    }

    private class ViewHolder {
        private TextView textVarA;
    }

    @Override
    public int getCount() {
        return listProfiles != null ? listProfiles.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return listProfiles.get(i); // will return the profile model from position
    }

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

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder;

        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context)
                    .inflate(android.R.layout.simple_list_item_1, null);
            holder.textVarA = (TextView) view.findViewById(android.R.id.text1);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        ProfileModel profile = listProfiles.get(i);
        holder.textVarA.setText(profile.getVarA()); // get varA value

        return view;
    }

    public void addEntry(ProfileModel newEntry) {
        listProfiles.add(newEntry);
        this.notifyDataSetChanged();
    }
}
要更新此适配器,请执行以下操作:

// create a list of object to the datas
ProfileModel profile = new ProfileModel();
profile.setVarA(varA);
profile.setVarB(varB);
profile.setVarC(varC);
profile.setVarD(varD);
profileAdapter.addEntry(profile); // this will call notifyDataSetChanged
最后,我不太了解TinyDB,但我看到它使用了一些JSON文件来存储数据。因此,您可以创建一个方法来将数据转换为JSONArray,其名称为
varA
,并且具有值
varB、varC、varD
。有许多例子可以说明如何将数据转换为JSON(如使用将模型转换为JSON格式)


希望这会有用。

请详细描述,您有哪些问题?首先,我尝试使用((ArrayAdapter)profileList.getAdapter()).notifyDataSetChanged();更新listview,但listview仅在重新构建应用程序后更新。如何刷新列表视图?
public class ProfileAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<ProfileModel> listProfiles;

    public ProfileAdapter(Context context, ArrayList<ProfileModel> listProfiles) {
        super();
        this.context = context;
        this.listProfiles = listProfiles;
    }

    private class ViewHolder {
        private TextView textVarA;
    }

    @Override
    public int getCount() {
        return listProfiles != null ? listProfiles.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return listProfiles.get(i); // will return the profile model from position
    }

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

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder;

        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context)
                    .inflate(android.R.layout.simple_list_item_1, null);
            holder.textVarA = (TextView) view.findViewById(android.R.id.text1);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        ProfileModel profile = listProfiles.get(i);
        holder.textVarA.setText(profile.getVarA()); // get varA value

        return view;
    }

    public void addEntry(ProfileModel newEntry) {
        listProfiles.add(newEntry);
        this.notifyDataSetChanged();
    }
}
ProfileAdapter profileAdapter = new ProfileAdapter(this, list);
profileList.setAdapter(profileAdapter);
// create a list of object to the datas
ProfileModel profile = new ProfileModel();
profile.setVarA(varA);
profile.setVarB(varB);
profile.setVarC(varC);
profile.setVarD(varD);
profileAdapter.addEntry(profile); // this will call notifyDataSetChanged