将另一个活动中的项添加到ExpandableListView

时间:2013-09-04 19:41:10

标签: java android android-activity hashmap expandablelistview

我想将一个项目从另一个活动添加到Today ExpandableListViewLocHistory。我要添加它的活动名为static void addListData(final Context context) { List<NewsItem> list = listDataChild.get("Today"); NewsItem newsData = new NewsItem(); newsData = new NewsItem(); newsData.setHeadline("11.11111, 1.1111"); newsData.setSpeed("1.11KM/H"); newsData.setDirection("111"); newsData.setDate("11-1-1111 11:11:11"); list.add(0, newsData); listDataChild.put("Today", list); } ,这是一个向列表中添加内容的代码:

LocHistory

当我在同一个类(MainActivity)中调用该函数时,这是有效的。但是当我在public class MainActivity extends Activity { Button button2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button2 = (Button) this.findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener() { public void onClick(View v) { LocHistory.addListData(getBaseContext()); } }); } } 中这样称呼它时:

ExpandableListView

然后列表中没有添加任何内容。是否可以将其他活动中的项目添加到LocHistory?我想如果添加了类startActivity不会打开的内容,那么我认为SharedPreferences有意图不是一个选项(但我不确定) 。

(java源代码可以在这里找到: MainActivity.javaLocHistory.javaNewsItem.javaExpandableListAdapter.java)功能

修改

正如另一个论坛上的一些人指出的那样,我现在正在使用static void addListData (int TimeStamp, final String lat, final String lng, final String speed, final String direction, final Context context){ int todaystamp = startOf("today"); int yesterdaystamp = startOf("yesterday"); String Datetime = DateFormat.format("dd-MM-yyyy kk:mm:ss", new Date(TimeStamp * 1000L)).toString(); SharedPreferences pref = context.getSharedPreferences("myPrefs", MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); if (TimeStamp >= todaystamp) { editor.putString("Today", "*headline=" + lat + ", " + lng + ";speed=" + speed + ";direction=" + direction + ";date=" + Datetime + ";"); } else if (TimeStamp >= yesterdaystamp) { editor.putString("Yesterday", "*headline=" + lat + ", " + lng + ";speed=" + speed + ";direction=" + direction + ";date=" + Datetime + ";"); } else if (TimeStamp < yesterdaystamp) { editor.putString("Older", "*headline=" + lat + ", " + lng + ";speed=" + speed + ";direction=" + direction + ";date=" + Datetime + ";"); } editor.commit(); } 。我使用此代码:

SharedPreferences

但现在我遇到了一个问题,当我在同一个键上向SharedPreferences添加一个项目时,它会覆盖以前的数据。如何在不覆盖以前数据的情况下将数据添加到同一个密钥?是否可以首先获取数据然后将项目加入到数据之后将数据添加到{{1}}?

1 个答案:

答案 0 :(得分:2)

您上次编辑时可以尝试:

static void addListData(int TimeStamp, final String lat, final String lng,
        final String speed, final String direction, final Context context) {

    int todaystamp = startOf("today");
    int yesterdaystamp = startOf("yesterday");
    String Datetime = DateFormat.format("dd-MM-yyyy kk:mm:ss", new Date(TimeStamp * 1000L)).toString();

    String location = "*headline=" + lat + ", " + lng + ";speed=" + speed
            + ";direction=" + direction + ";date=" + Datetime + ";";

    SharedPreferences pref = context.getSharedPreferences("myPrefs",
            MODE_PRIVATE);
    SharedPreferences.Editor editor = pref.edit();

    if (TimeStamp >= todaystamp) {
        String today = pref.getString("Today",null);
        if (today != null) {
            StringBuilder str = new StringBuilder(today);
            str.insert(0, location + ", ");
            editor.putString("Today", str.toString());
        } else {
            editor.putString("Today", location);
        }
    } else if (TimeStamp >= yesterdaystamp) {
        String yesterday = pref.getString("Yesterday",null);
        if (yesterday != null) {
            StringBuilder str = new StringBuilder(yesterday);
            str.insert(0, location + ", ");
            editor.putString("Yesterday", str.toString());
        } else {
            editor.putString("Yesterday", location);
        }
    } else if (TimeStamp < yesterdaystamp) {
        String older = pref.getString("Older",null);
        if (older != null) {
            StringBuilder str = new StringBuilder(older);
            str.insert(0, location + ", ");
            editor.putString("Older", str.toString());
        } else {
            editor.putString("Older", location);
        }
    }

    editor.commit();
}

这将确保它不会覆盖密钥,但如果存在则附加。 (这是通过检查特定键的SharedPreferences是否为空来完成的。)

在这种情况下,我使用StringBuilder方法使用insert