2

我想Today从另一个活动向孩子添加一个项目到ExpandableListView. 我要添加它的活动名为LocHistory,这是向列表添加内容的代码:

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这里不是一个选项(但我不确定)。

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

编辑:

正如其他论坛上的一些人指出的那样,我现在正在使用SharedPreferences. 我正在使用这段代码:

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

4

1 回答 1

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特定键是否不为空来完成的)。

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

于 2013-12-17T09:47:32.717 回答