添加的项目从另一个活动ExpandableListView [英] Adding a item from another activity to ExpandableListView

查看:98
本文介绍了添加的项目从另一个活动ExpandableListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个项目从另一个活动添加到子今天 ExpandableListView 。在这里我想补充它的活动被命名为 LocHistory ,这里是一个在code添加的东西到列表:

I want to add a item to the child Today from another activity to the ExpandableListView. The activity where I want to add it is named LocHistory, here is a the code to add something to the list:

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 <$ C C $>)。但是,当我把它在 MainActivity 是这样的:

This is working when I have call the function in the same class (LocHistory). But when I call it in MainActivity like this:

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 与意图是这里不是一个选项(但我不知道)。

Then there is nothing added to the list. Is it possible to add a item from another activity to ExpandableListView? I want if there's something added that the class LocHistory is not going to open, so I think startActivity with a intent is not a option here (but i'm not sure).

(Java源代码可以在这里找到: MainActivity.java LocHistory.java NewsItem.java ExpandableListAdapter.java

(The java sources can be found here: MainActivity.java, LocHistory.java, NewsItem.java and ExpandableListAdapter.java)

编辑:

由于在其他论坛上一些人指出的那样,我现在使用共享preferences 。我使用这个code:

As some guys on a other forum pointed out, I'm now using SharedPreferences. I'm using this code:

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();
}

但现在我坚持了一个问题,当我加入一个项目在同一个键共享preferences 它将覆盖previous数据。我怎样才能将数据添加到同一个密钥,而不覆盖previous数据?它是也许可以先获取数据,然后加入到项目后,该数据将数据添加到共享preferences

But now I'm stuck with one problem, when I add a item to the SharedPreferences on the same key it will overwrite the previous data. How can I add data to the same key without overwriting the previous data? Is it maybe possible to first get the data and then join the item to the data after that add the data to the SharedPreferences?

推荐答案

有关你上一次编辑,你可以试试这个:

For you last edit you can try this:

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();
}

这将确保它没有覆盖的关键,但追加如果它存在。 (这是通过检查是否共享preferences 不为空的特定按键来完成)。

This will ensure that it not overrides the key but append if it exists. (This is done by checking whether the SharedPreferences is not null for a specific key).

我用 的StringBuilder 中的<一个href="http://developer.android.com/reference/java/lang/StringBuilder.html#insert%28int,%20java.lang.String%29"相对=nofollow> 插入 在这种情况下,方法。

I use StringBuilder with the insert method in this case.

这篇关于添加的项目从另一个活动ExpandableListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆