【问题标题】:Adding a item from another activity to ExpandableListView将另一个活动中的项目添加到 ExpandableListView
【发布时间】:2013-09-08 11:12:15
【问题描述】:

我想将另一个活动中的子项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.java, LocHistory.java, NewsItem.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

【问题讨论】:

  • 您没有在listlistDataChild 中看到新项目?还是在适配器中看不到新项目?
  • @Luksprog 我在子Today 下的列表中看不到它(所以我认为那是适配器(?))。但是当我在函数addListData 中记录listDataChild 时,它似乎被添加到Today 下的列表中(只有当我第一次打开类LocHistory 并关闭它时,日志才有效)。
  • 我仍然不明白你想要做什么以及你是如何测试你当前的实现的。从MainActivityLocHistory 类调用该方法应该没有问题,成功的NewsItem 将添加到Today 键下的列表中。如果您在该方法结束时打印地图,您应该会在其中看到新项目。请记住,只有在运行 LocHistory 活动时才初始化地图和列表。
  • @Luksprog prepareListData 函数在onCreate 方法中调用。是否该函数会覆盖由函数 addListData 创建的 listDataChild
  • @Luksprog 我使用SharedPreferences 添加了一个可能的解决方案。但是现在我遇到了一个问题,当我在同一个键上向SharedPreferences 添加一个项目时,它会覆盖以前的数据。如何在不覆盖以前数据的情况下将数据添加到同一个键?是否可以先获取数据,然后将项目加入数据,然后将数据添加到SharedPreferences

标签: java android android-activity hashmap expandablelistview


【解决方案1】:

对于你上次的编辑,你可以试试这个:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多