简体   繁体   English

如何清除哈希映射<string,string>值?

[英]How to clear the hash map<string,string> values?

I want to clear all the hash map values before the values are assigned to hash map from db.For eg., hashmap<string,string> demo is my hashmap in that i have added the following values from db.The result response from hashmap is {A=1, B=2, C=3, D=4, E=5} .After view loads I want to insert this in above hashmap {A=test, B=demo, C=sample, D=empty, E=null} .For this how can I clear the first values before inserting the second set of values.And also I have tried by using demo.clear() it is not working.How can I clear the 1st list of values before adding the 2nd one.Suggest some solutions to solve the problem. 我希望清除所有哈希映射值,然后将值从db分配给哈希映射。例如, hashmap<string,string> demo是我的hashmap,因为我已经从db添加了以下值。来自hashmap的结果响应是{A=1, B=2, C=3, D=4, E=5} 。视图加载后我想在上面的hashmap中插入它{A=test, B=demo, C=sample, D=empty, E=null} 。为此,如何在插入第二组值之前清除第一个值。我也尝试使用demo.clear()它不起作用。如何清除第一个值列表之前添加第二个。提出一些解决方案来解决问题。

This is the code to retrieve data from db. 这是从db检索数据的代码。

public HashMap<String, String> getdata() {
    try {
        HashMap<String, String> map = new HashMap<String, String>();
        Log.e("getdata", "Started");
        String selectQuery = "SELECT * FROM tablename";
        SQLiteDatabase database = this.getWritableDatabase();
        Cursor cursor = database.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                map.put(cursor.getString(cursor.getColumnIndex("colmn1")),
                        cursor.getString(cursor.getColumnIndex("colmn2")));
            } while (cursor.moveToNext());
        }
        cursor.close();
        return map;

    } catch (Exception e) {
        e.printStackTrace();
        Log.e("getdata", "Ended");
    }
    return null;

}

In this demo only the response coming as I explained above.And then am doing this 在这个演示中,只有我上面解释的响应。然后我正在做这个

if(demo.isEmpty())
        {
//code for correct condition
        }
else
    {
     demo.clear();
    }

And passing the values to hash map by this way. 并通过这种方式将值传递给哈希映射。

demo = dbhelper.getdata();

And My Logcat exceptions: 和我的Logcat例外:

09-06 12:48:53.656: W/dalvikvm(5071): threadid=1: thread exiting with uncaught exception (group=0x40fb0258)
09-06 12:48:53.660: W/System.err(5071): java.lang.RuntimeException: Unable to start activity ComponentInfo{packagename/packagename.listdata}: java.lang.NullPointerException
09-06 12:48:53.660: W/System.err(5071):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
09-06 12:48:53.660: W/System.err(5071):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
09-06 12:48:53.660: W/System.err(5071):     at android.app.ActivityThread.access$600(ActivityThread.java:134)
09-06 12:48:53.660: W/System.err(5071):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
09-06 12:48:53.660: W/System.err(5071):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 12:48:53.660: W/System.err(5071):     at android.os.Looper.loop(Looper.java:154)
09-06 12:48:53.660: W/System.err(5071):     at android.app.ActivityThread.main(ActivityThread.java:4624)
09-06 12:48:53.661: W/System.err(5071):     at java.lang.reflect.Method.invokeNative(Native Method)
09-06 12:48:53.661: W/System.err(5071):     at java.lang.reflect.Method.invoke(Method.java:511)
09-06 12:48:53.661: W/System.err(5071):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
09-06 12:48:53.661: W/System.err(5071):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
09-06 12:48:53.661: W/System.err(5071):     at dalvik.system.NativeStart.main(Native Method)
09-06 12:48:53.661: W/System.err(5071): Caused by: java.lang.NullPointerException
09-06 12:48:53.661: W/System.err(5071):     at packagename.listdata.onCreate(listdata.java:231)
09-06 12:48:53.661: W/System.err(5071):     at android.app.Activity.performCreate(Activity.java:4479)
09-06 12:48:53.661: W/System.err(5071):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
09-06 12:48:53.661: W/System.err(5071):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)

hMap.clear() always worked fine for me. hMap.clear()总是对我很好。

Still 仍然

hMap.put("A","test");

will automatically override the existing value for key "A". 将自动覆盖键“A”的现有值。

If you still wish to clear values, 如果您仍希望清除值,

You can use 您可以使用

hMap = new HashMap<String,String>();

which will initialize a new hashmap in hMap . 这将在hMap初始化一个新的hashmap。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM