首页 > 解决方案 > Android Save动态添加的文本网格视图

问题描述

我在我的项目中实现了这个例子:

http://camposha.info/source/aandroid-data-passing-from-textbox-to-second-activity

所以,现在我想保存文本,我已经通过edittext添加了。这可能吗?

或者我可以用列表视图更改网格视图吗?我想输入文本,文本将显示在第二个活动中。如果它在网格视图或列表视图中。我不知道是否可以使用列表视图来做到这一点。如果是这样,请帮助我:)

我是初学者,所以感谢您的帮助。

古斯塔夫

标签: androidlistviewgridviewsave

解决方案


I want to save the text, I've added through the edittext. Is this possible?

Yes, You can use SharedPreferences, SQLite Database and other ways. I suggest you read this link which completely explains how to preserve data in android. You can modify DataHolder class, so this class can save and load data from one of these storage, and use it to save texts in MainActivity and load them in SecondActivity.

And I did not understand the second part of your question! Do you want to show data in a ListView instead of GridView? If so, then you can replace that GridView with a ListView in ActivitySecond.xml like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.tutorials.hp.txttoactivitygrid.SecondActivity">
    <!-- Replace with this -->
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>
</RelativeLayout>

And then in your SecondActivity.java replace GridView gv; with ListView ls; and then change

gv= (GridView) findViewById(R.id.gv);
gv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, DataHolder.spacecrafts));

with

lv= (ListView) findViewById(R.id.lv);
lv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, DataHolder.spacecrafts));

The idea is to replace every code which uses GridView to use ListView instead .I think this modification is enough and should work. If it doesn't let me know so I can help you. Also you can read about ListView and how to use it here. (You can find lots of tutorials when you search for it on the internet :D)


推荐阅读