android - Android Save动态添加的文本网格视图
问题描述
我在我的项目中实现了这个例子:
http://camposha.info/source/aandroid-data-passing-from-textbox-to-second-activity
所以,现在我想保存文本,我已经通过edittext添加了。这可能吗?
或者我可以用列表视图更改网格视图吗?我想输入文本,文本将显示在第二个活动中。如果它在网格视图或列表视图中。我不知道是否可以使用列表视图来做到这一点。如果是这样,请帮助我:)
我是初学者,所以感谢您的帮助。
古斯塔夫
解决方案
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)
推荐阅读
- python-3.x - Fernet 加密无法正确解密 JSON 文件
- php - SMTP 适用于 apache 2.2/php54 但不适用于 apache 2.4/php736
- php - 如何指定 if 条件语句中返回的变量函数?
- node.js - Vega nodejs为折线图创建“趋势线”
- postgresql - 左连接到默认值表
- javascript - 等待元素出现在 jQuery 中
- python - 如何在 Python DataFrame 中的确定行之前添加一个空行?
- zip - amcharts 导出为 Zip 文件
- node.js - 我需要在每个需要数据库集合数据的文件中使用 mongoose.connect(mongoose://localhost)
- java - 为什么以下代码出现错误,任何人都可以解释一下吗?