如何将字符串转换为列表(Android)?

huangapple go评论74阅读模式
英文:

How to convert String to List (Android)?

问题

I tried to use a Toast to display List<DataItem>.toString(), but the toast only shows [{name='phone',value='081345678912'}] , and does not show all the data that I want.

我尝试使用 Toast 来显示 List<DataItem>.toString(),但 Toast 只显示了 [{name='phone',value='081345678912'}],并没有显示我想要的所有数据。

I want it to show this:

我希望它显示如下内容:

[{name='email',value='akuganteng@gmail.com'}, {name='userpwd',value='123456'}, {name='userid',value='Aku Ganteng'}, {name='phone',value='081345678912'}]

Toast List<DataItem> :

Toast List<DataItem>

List<DataItem> dataItemList = new ArrayList<>();
DataItem dataItem = new DataItem();
dataItem.setName("email");
dataItem.setValue(getEmail);
dataItemList.add(dataItem);

dataItem = new DataItem();
dataItem.setName("userpwd");
dataItem.setValue(getPassword);
dataItemList.add(dataItem);

dataItem = new DataItem();
dataItem.setName("userid");
dataItem.setValue(getFullName);
dataItemList.add(dataItem);

dataItem = new DataItem();
dataItem.setName("phone");
dataItem.setValue(getPhoneNumber);
dataItemList.add(dataItem);

Toast.makeText(this, dataItemList.toString(), Toast.LENGTH_SHORT).show();

POJO DataItem :

POJO DataItem:

public class DataItem {

    @SerializedName("name")
    private String name;

    @SerializedName("value")
    private String value;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "{" +
                "name = '" + name + '\'' +
                ",value = '" + value + '\'' +
                "}";
    }
}

Please note that I've corrected the code for creating the dataItemList and setting the values for each DataItem object based on your desired output.

英文:

I tried to use a Toast to display List<DataItem>.toString(), but the toast only shows [{name='phone',value='081345678912'}] , and does not show all the data that I want.

I want it to show this:

[{name='email',value='akuganteng@gmail.com'}, {name='userpwd',value='123456'}, {name='userid',value='Aku Ganteng'}, {name='phone',value='081345678912'}]

Toast List<DataItem> :

List<DataItem> dataItemList = new ArrayList<>();
DataItem dataItem = new DataItem();
dataItem.setName("email");
dataItem.setName("userpwd");
dataItem.setName("userid");
dataItem.setName("phone");
dataItem.setValue(getEmail);
dataItem.setValue(getPassword);
dataItem.setValue(getFullName);
dataItem.setValue(getPhoneNumber);
dataItemList.add(dataItem);

Toast.makeText(this, dataItemList.toString(), Toast.LENGTH_SHORT).show();

POJO DataItem :

public class DataItem{

    @SerializedName("name")
    private String name;

    @SerializedName("value")
    private String value;

    public void setName(String name){
	    this.name = name;
    }

    public String getName(){
	    return name;
    }

    public void setValue(String value){
	    this.value = value;
    }

    public String getValue(){
	    return value;
    }

    @Override
    public String toString(){
	    return 
		    "{" + 
		    "name = '" + name + '\'' + 
		    ",value = '" + value + '\'' + 
		    "}";
	}
}

答案1

得分: 2

你初始化的List仅包含一个名为DataItem的元素,其值正在被替换。要实现你描述的功能,你需要为每个所需的数据创建一个DataItem,如下所示:

List<DataItem> dataItemList = new ArrayList<>();

// 邮箱数据项
DataItem emailDataItem = new DataItem();
emailDataItem.setName("email");
emailDataItem.setValue(getEmail);
dataItemList.add(emailDataItem);

// 用户密码数据项
DataItem userpwdDataItem = new DataItem();
userpwdDataItem.setName("userpwd");
userpwdDataItem.setValue(getPassword);
dataItemList.add(userpwdDataItem);

// 用户ID数据项
DataItem useridDataItem = new DataItem();
useridDataItem.setName("userid");
useridDataItem.setValue(getFullName);
dataItemList.add(useridDataItem);

// 电话数据项
DataItem phoneDataItem = new DataItem();
phoneDataItem.setName("phone");
phoneDataItem.setValue(getPhoneNumber);
dataItemList.add(phoneDataItem);

Toast.makeText(this, dataItemList.toString(), Toast.LENGTH_SHORT).show();
英文:

The List that you have initialized contains only a single DataItem element whose values are being replaced. To achieve what you have described, you need a DataItem for each of the required data as follows --

List&lt;DataItem&gt; dataItemList = new ArrayList&lt;&gt;();

// email Data Item
DataItem emailDataItem = new DataItem();
emailDataItem.setName(&quot;email&quot;);
emailDataItem.setValue(getEmail);
dataItemList.add(emailDataItem);

// userpwd Data Item
DataItem userpwdDataItem = new DataItem();
userpwdDataItem.setName(&quot;userpwd&quot;);
userpwdDataItem.setValue(getPassword);
dataItemList.add(userpwdDataItem);

// userid Data Item
DataItem useridDataItem = new DataItem();
useridDataItem.setName(&quot;userid&quot;);
useridDataItem.setValue(getFullName);
dataItemList.add(useridDataItem);

// phone Data Item
DataItem phoneDataItem = new DataItem();
phoneDataItem.setName(&quot;phone&quot;);
phoneDataItem.setValue(getPhoneNumber);
dataItemList.add(phoneDataItem);

Toast.makeText(this, dataItemList.toString(), Toast.LENGTH_SHORT).show();

答案2

得分: 1

您的DataItem包含一个字符串name和一个字符串value。因此,当您使用相同的DataItem实例(DataItem dataItem = new DataItem();)时,您只是不断地覆盖namevalue,直到最后一组是唯一剩下的值(phone和电话号码)。

由于所有这些值都属于同一组,我建议重新设计您的类以更准确地模拟您的数据。首先,将DataItem类重命名为更具描述性的名称(比如User)。然后,在User类内部,创建实例成员和getter/setter方法:

private String email;
private String userPassword; // 不过,我不太喜欢这个名字,后面会解释**
private String userId;
private String phoneNumber;

// 在这里添加getter和setter方法

现在,您可以直接在单个User实例上设置每个属性:

user.setEmail(someEmail);
user.setUserPassword(somePassword);
user.setUserId(someUserId);
user.setPhoneNumber(somePhoneNumber);

**我建议您详细了解更好的将密码存储在内存中的方法。请参阅此Stack Overflow问题及其答案以获取更多详细信息。

英文:

Your DataItem contains a single String name and a single String value. So when you use the same DataItem instance (DataItem dataItem = new DataItem();) all you're doing is overwriting name and value until the last combination are the only values left (phone and the phone number).

Since all these values belong in a single group, I'd recommend redesigning your class to be a more accurate model of your data. First, rename your DataItem class to be more descriptive (like User). Then, within that User class, create instance members and getters/setters for:

private String email;
private String userPassword; // I don&#39;t like this though, read below**
private String userId;
private String phoneNumber;

// add getters and setters here

Now you can set each of these properties directly on a single instance of User:

user.setEmail(someEmail);
user.setUserPassword(somePassword);
user.setUserId(someUserId);
user.setPhoneNumber(somePhoneNumber);

** I'd recommend you read up on better ways to store passwords in memory though. See this Stack Overflow question and its answers for more details.

huangapple
  • 本文由 发表于 2020年8月8日 13:24:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63312129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定