Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.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
Android 无法正确地从Hashmap获取数据_Android_Listview_Hashmap_Android Listview - Fatal编程技术网

Android 无法正确地从Hashmap获取数据

Android 无法正确地从Hashmap获取数据,android,listview,hashmap,android-listview,Android,Listview,Hashmap,Android Listview,我有一个HashMap的Arraylist。每个HashMap元素包含两列:列名和相应的值。此HashMap将添加到带有3TextView的ListView中 我按如下方式填充ArrayList,然后将其分配给适配器以显示它: ArrayList<HashMap<String, String>> list1 = new ArrayList<HashMap<String, String>>(); HashMap<String, String&g

我有一个
HashMap
Arraylist
。每个
HashMap
元素包含两列:列名和相应的值。此
HashMap
将添加到带有3
TextView
ListView

我按如下方式填充
ArrayList
,然后将其分配给适配器以显示它:

ArrayList<HashMap<String, String>> list1 = new ArrayList<HashMap<String, String>>();
HashMap<String, String> addList1;
for (int i = 0; i < count; i++) {

addList1 = new HashMap<String, String>();
addList1.put(COLUMN1, symbol[i]);
addList1.put(COLUMN2, current[i]);
addList1.put(COLUMN3, change[i]);

list1.add(addList1);

RecentAdapter adapter1 = new RecentAdapter(CompanyView.this,
                    CompanyView.this, list1);
listrecent.setAdapter(adapter1);
        }
i、 e.当我在单击第一个列表项后记录获取的字符串时,我得到以下几个输出之一:

{1=ABC,2=123,3=1}

{First=ABC,Second=123,Third=1}

{1=123,0=ABC,2=1}

甚至 {27=123,28=1,26=ABC}

最初我使用:

int pos1 = item.indexOf("1=");
int pos2 = item.indexOf("2=");
int pos3 = item.indexOf("3=");

String symbol = item.substring(pos1 + 2,pos1 - 2).trim();
String current = item.substring(pos2 + 2, pos3 - 2).trim();
String change = item.substring(pos3 + 2, item.length() - 1).trim();
对于第四种情况,我必须使用:

int pos1 = item.indexOf("26=");
int pos2 = item.indexOf("27=");
int pos3 = item.indexOf("28=");

String symbol = item.substring(pos1 + 3, item.length() - 1).trim();
String current = item.substring(pos2 + 3, pos3 - 3).trim();
String change = item.substring(pos3 + 3, pos1 - 3).trim();
因此,我在
symbol
中获得
ABC
,依此类推

但是,通过这种方法,应用程序完全失去了可靠性

我也试过了

while (myVeryOwnIterator.hasNext()) {

key = (String) myVeryOwnIterator.next();
value[ind] = (String) addList1.get(key);
                }
但它没有给出适当的价值。相反,它返回随机符号,例如ABC、PQR或XYZ

我做错什么了吗


提前谢谢

HashMap的put函数不按特定顺序插入值。因此,最好的方法是将HashMap的键集放在ArrayList中,并使用ArrayList索引检索值

ArrayList<HashMap<String, String>> list1 = new ArrayList<HashMap<String, String>>();
HashMap<String, String> addList1;
ArrayList<String> listKeySet;
for (int i = 0; i < count; i++) {

addList1 = new HashMap<String, String>();
addList1.put(COLUMN1, symbol[i]);
addList1.put(COLUMN2, current[i]);
addList1.put(COLUMN3, change[i]);

listKeySet.add(COLUMN1);
listKeySet.add(COLUMN2);
listKeySet.add(COLUMN3);

list1.add(addList1);

RecentAdapter adapter1 = new RecentAdapter(CompanyView.this,
                CompanyView.this, list1);
listrecent.setAdapter(adapter1);
}

这里,arraylist listKeySet仅用于保留HashMap键插入的顺序。将数据放入HashMap时,将键插入ArrayList。

HashMap的put函数不会按特定顺序插入值。因此,最好的方法是将HashMap的键集放在ArrayList中,并使用ArrayList索引检索值

ArrayList<HashMap<String, String>> list1 = new ArrayList<HashMap<String, String>>();
HashMap<String, String> addList1;
ArrayList<String> listKeySet;
for (int i = 0; i < count; i++) {

addList1 = new HashMap<String, String>();
addList1.put(COLUMN1, symbol[i]);
addList1.put(COLUMN2, current[i]);
addList1.put(COLUMN3, change[i]);

listKeySet.add(COLUMN1);
listKeySet.add(COLUMN2);
listKeySet.add(COLUMN3);

list1.add(addList1);

RecentAdapter adapter1 = new RecentAdapter(CompanyView.this,
                CompanyView.this, list1);
listrecent.setAdapter(adapter1);
}

这里,arraylist listKeySet仅用于保留HashMap键插入的顺序。将数据放入HashMap时,请将键插入ArrayList。

我认为为此使用
HashMap
不是一个好主意。我将实现类来封装您的数据,如

class myData {
    public String Column1;
    public String Column2;
    public String Column3;
    // better idea would be making these fields private and using 
    // getters/setters, but just for the sake of example these fields
    // are left public

    public myData(String col1, String col2, String col3){
        Column1 = col1;
        Column2 = col2;
        Column3 = col3;
    }
}
像这样使用它

ArrayList<myData> list1 = new ArrayList<myData>();
for (int i = 0; i < count; i++) {
    list1.add(new myData(symbol[i], current[i], change[i]));
}
//no need to create new adapter on each iteration, btw
RecentAdapter adapter1 = new RecentAdapter(CompanyView.this,
                CompanyView.this, list1);
listrecent.setAdapter(adapter1);
ArrayList list1=新的ArrayList();
for(int i=0;i

当然,您需要在适配器中进行更改,以使用myData而不是
HashMap

我认为为此使用
HashMap
不是一个好主意。我将实现类来封装您的数据,如

class myData {
    public String Column1;
    public String Column2;
    public String Column3;
    // better idea would be making these fields private and using 
    // getters/setters, but just for the sake of example these fields
    // are left public

    public myData(String col1, String col2, String col3){
        Column1 = col1;
        Column2 = col2;
        Column3 = col3;
    }
}
像这样使用它

ArrayList<myData> list1 = new ArrayList<myData>();
for (int i = 0; i < count; i++) {
    list1.add(new myData(symbol[i], current[i], change[i]));
}
//no need to create new adapter on each iteration, btw
RecentAdapter adapter1 = new RecentAdapter(CompanyView.this,
                CompanyView.this, list1);
listrecent.setAdapter(adapter1);
ArrayList list1=新的ArrayList();
for(int i=0;i
当然,您需要在适配器中进行更改以使用myData,而不是
HashMap