简体   繁体   English

如何将数据(标题,日期等)绑定到我从文件夹中检索的文件列表?

[英]How to tie up data (title, date etc) to a list of files I'm retrieving from a folder?

I save a list of files into a folder through my app, and when I want to retrieve it, I want to show the Title, date and some other information in each row. 我通过我的应用程序将文件列表保存到文件夹中,当我想要检索它时,我想在每行中显示标题,日期和一些其他信息。 How do I tie up these information with each file? 如何将这些信息与每个文件捆绑在一起?

You can always create an array. 您始终可以创建一个数组。 Let's say you are making an app which receives the contact information stored on your device. 假设您正在制作一个应用程序,它可以接收存储在您设备上的联系信息。 You can do it a bit like this: 你可以这样做:

TextView phoneNumber = (TextView) findViewById(R.id.phone_number);
TextView contactName = (TextView) findViewById(R.id.contact_name);

etc... 等等...

Then, you can set the arrays in your other activity. 然后,您可以在其他活动中设置数组。

String[] contact1;
contact1[0] = "Bob";
contact1[1] = "555-555-5555";

If you are planning to use intents, you have to specify which class you are getting your arrays from. 如果您计划使用意图,则必须指定从哪个类获取数组。 Once you set the arrays and define your TextViews, you can do this: 设置数组并定义TextView后,可以执行以下操作:

contactName.setText("Name: " + contact1[0])

For setting the contacts, you can do this in a loop by setting a number called i and resetting it at the end of each loop. 要设置联系人,可以通过设置一个名为i的数字并在每个循环结束时重置它来循环执行此操作。 This can shorten your lines of code. 这可以缩短您的代码行。 You can do the same with setting the value of the TextViews. 您可以通过设置TextViews的值来执行相同的操作。

If you have further questions, please feel free to reply! 如果您还有其他问题,请随时回复! I will do what I can to help. 我会尽力帮助你。

Regards, 问候,

Ani 阿尼

I suggest storing the date and description for all the list of files in the database. 我建议存储数据库中所有文件列表的日期和描述。

When you are storing the files in the folder at the same time make an entry in the database for a particular file. 当您将文件存储在文件夹中的同时,在数据库中为特定文件创建条目。 Next time you can pick up the details from the database. 下次您可以从数据库中获取详细信息。

Hope this will help you. 希望这会帮助你。

Refer this link to create Sqlite db in android 请参阅此链接以在android中创建Sqlite db

如何存储列表<object>在房间数据库中? (我在使用 DAO 查询检索列表时遇到问题)<div id="text_translate"><p> 我将 Device 对象存储在 Room 数据库中,并且在检索作为浮点列表的属性 (temp_values) 之一时遇到问题。 我已经按照此处找到的其他建议说您需要一个类型转换器,所以我在这里展示了。 当我尝试编译时,出现此错误:</p><blockquote><p> “警告:查询返回 java.lang.Float 未使用的某些列 [temp_values]。您可以在字段上使用 @ColumnInfo 注释来指定映射。您可以通过使用 @SuppressWarnings(RoomWarnings .CURSOR_MISMATCH)。查询返回的列:temp_values。java.lang.Float 中的字段:。"</p></blockquote><p> 问题在于 DAO 中的 getTempValues 查询,如果我将其注释掉,那么一切都编译得很好。 我在下面包含了设备 object、TemperatureListConverter 和我的 DAO。</p><pre> @Entity(tableName = "devices") @TypeConverters(TemperatureListConverter.class) public class Device implements Serializable { @PrimaryKey @NonNull @ColumnInfo(name = "serial_num") private String serialNum; @ColumnInfo(name = "temp_values") @TypeConverters(TemperatureListConverter.class) private List&lt;Float&gt; tempValues; public Device(String serialNum) { this.serialNum = serialNum; this.tempValues = new ArrayList&lt;&gt;(); } public String getSerialNum() { return serialNum; } public List&lt;Float&gt; getTempValues() { return tempValues; } public void setTempValues(List&lt;Float&gt; tempValues) { this.tempValues = tempValues; } }</pre><pre> public class TemperatureListConverter { private static final Gson gson = new Gson(); @TypeConverter public static List&lt;Float&gt; toTempList(String tempValuesString) { if (tempValuesString == null) { return Collections.emptyList(); } Type type = new TypeToken&lt;List&lt;Float&gt;&gt;() {}.getType(); return gson.fromJson(tempValuesString, type); } @TypeConverter public static String fromTempList(List&lt;Float&gt; tempValues) { return gson.toJson(tempValues); } }</pre><pre> @Dao @TypeConverters(TemperatureListConverter.class) public interface DeviceDao { @Query("SELECT * FROM devices") List&lt;Device&gt; getAllDevices(); @Query("SELECT * FROM devices WHERE serial_num =:serialNum") Device getDevice(String serialNum); @Query("SELECT temp_values FROM devices WHERE serial_num =:serialNum") List&lt;Float&gt; getTempValues(String serialNum); @Query("UPDATE devices SET temp_values =:tempValues WHERE serial_num =:serialNum") int setTempValues(String serialNum, List&lt;Float&gt; tempValues); @Insert void insert(Device... device); @Query("DELETE FROM devices WHERE serial_num =:serialNum") void deleteBySerial(String serialNum); }</pre><p> 编辑:我在这里添加了我的数据库 class 。</p><pre> @Database(entities = {Device.class}, version = 37, exportSchema = false) @TypeConverters(TemperatureListConverter.class) public abstract class DeviceDatabase extends RoomDatabase { private static final String DB_NAME = "devices_db"; private static DeviceDatabase deviceDb; // simple singleton public static DeviceDatabase getDeviceDb(Context context) { if (deviceDb == null) { deviceDb = Room.databaseBuilder(context, DeviceDatabase.class, DB_NAME).fallbackToDestructiveMigration().build(); } return deviceDb; } public abstract DeviceDao getDeviceDao(); public void addDevice(final Device device) { new Thread(new Runnable() { @Override public void run() { try { getDeviceDao().insert(device); } catch (Exception e) { e.printStackTrace(); } } }).start(); } public void removeDevice(final String serialNum) { new Thread(new Runnable() { @Override public void run() { try { getDeviceDao().deleteBySerial(serialNum); } catch (Exception e) { e.printStackTrace(); } } }).start(); } public Device getDevice(final String serialNum) { final Device[] finalDevice = new Device[1]; Thread thread = new Thread(new Runnable() { @Override public void run() { try { finalDevice[0] = getDeviceDao().getDevice(serialNum); } catch (Exception e) { e.printStackTrace(); } } }); thread.start(); try { thread.join(); } catch (Exception e) { e.printStackTrace(); } return finalDevice[0]; } public List&lt;Float&gt; getTempValues(final String serialNum) { final List&lt;Float&gt; finalTempValues = new ArrayList&lt;&gt;(); Thread thread = new Thread(new Runnable() { @Override public void run() { try { finalTempValues.addAll(getDeviceDao().getTempValues(serialNum)); } catch (Exception e) { e.printStackTrace(); } } }); thread.start(); try { thread.join(); } catch (Exception e) { e.printStackTrace(); } return finalTempValues; } public void setTempValues(final String serialNum, final List&lt;Float&gt; tempValues) { new Thread(new Runnable() { @Override public void run() { try { getDeviceDao().setTempValues(serialNum, tempValues); } catch (Exception e) { e.printStackTrace(); } } }).start(); } }</pre></div></object> - How to store List<Object> in Room database? (I'm having trouble retrieving the list with DAO query)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Maven .m2文件夹中设置几个设置文件? - How to set up several settings files in the maven .m2 folder? Firebase-从日期检索数据 - Firebase - retrieving data from a date 如何将文件下载到下载文件夹等 - How to download files to downloads folder, etc 如何使用JAVA设置文件的通用属性,例如标题,主题,注释,公司等 - How can I set generic attributes of Files like Title, Subject, Comments, Company..etc using JAVA 如何存储列表<object>在房间数据库中? (我在使用 DAO 查询检索列表时遇到问题)<div id="text_translate"><p> 我将 Device 对象存储在 Room 数据库中,并且在检索作为浮点列表的属性 (temp_values) 之一时遇到问题。 我已经按照此处找到的其他建议说您需要一个类型转换器,所以我在这里展示了。 当我尝试编译时,出现此错误:</p><blockquote><p> “警告:查询返回 java.lang.Float 未使用的某些列 [temp_values]。您可以在字段上使用 @ColumnInfo 注释来指定映射。您可以通过使用 @SuppressWarnings(RoomWarnings .CURSOR_MISMATCH)。查询返回的列:temp_values。java.lang.Float 中的字段:。"</p></blockquote><p> 问题在于 DAO 中的 getTempValues 查询,如果我将其注释掉,那么一切都编译得很好。 我在下面包含了设备 object、TemperatureListConverter 和我的 DAO。</p><pre> @Entity(tableName = "devices") @TypeConverters(TemperatureListConverter.class) public class Device implements Serializable { @PrimaryKey @NonNull @ColumnInfo(name = "serial_num") private String serialNum; @ColumnInfo(name = "temp_values") @TypeConverters(TemperatureListConverter.class) private List&lt;Float&gt; tempValues; public Device(String serialNum) { this.serialNum = serialNum; this.tempValues = new ArrayList&lt;&gt;(); } public String getSerialNum() { return serialNum; } public List&lt;Float&gt; getTempValues() { return tempValues; } public void setTempValues(List&lt;Float&gt; tempValues) { this.tempValues = tempValues; } }</pre><pre> public class TemperatureListConverter { private static final Gson gson = new Gson(); @TypeConverter public static List&lt;Float&gt; toTempList(String tempValuesString) { if (tempValuesString == null) { return Collections.emptyList(); } Type type = new TypeToken&lt;List&lt;Float&gt;&gt;() {}.getType(); return gson.fromJson(tempValuesString, type); } @TypeConverter public static String fromTempList(List&lt;Float&gt; tempValues) { return gson.toJson(tempValues); } }</pre><pre> @Dao @TypeConverters(TemperatureListConverter.class) public interface DeviceDao { @Query("SELECT * FROM devices") List&lt;Device&gt; getAllDevices(); @Query("SELECT * FROM devices WHERE serial_num =:serialNum") Device getDevice(String serialNum); @Query("SELECT temp_values FROM devices WHERE serial_num =:serialNum") List&lt;Float&gt; getTempValues(String serialNum); @Query("UPDATE devices SET temp_values =:tempValues WHERE serial_num =:serialNum") int setTempValues(String serialNum, List&lt;Float&gt; tempValues); @Insert void insert(Device... device); @Query("DELETE FROM devices WHERE serial_num =:serialNum") void deleteBySerial(String serialNum); }</pre><p> 编辑:我在这里添加了我的数据库 class 。</p><pre> @Database(entities = {Device.class}, version = 37, exportSchema = false) @TypeConverters(TemperatureListConverter.class) public abstract class DeviceDatabase extends RoomDatabase { private static final String DB_NAME = "devices_db"; private static DeviceDatabase deviceDb; // simple singleton public static DeviceDatabase getDeviceDb(Context context) { if (deviceDb == null) { deviceDb = Room.databaseBuilder(context, DeviceDatabase.class, DB_NAME).fallbackToDestructiveMigration().build(); } return deviceDb; } public abstract DeviceDao getDeviceDao(); public void addDevice(final Device device) { new Thread(new Runnable() { @Override public void run() { try { getDeviceDao().insert(device); } catch (Exception e) { e.printStackTrace(); } } }).start(); } public void removeDevice(final String serialNum) { new Thread(new Runnable() { @Override public void run() { try { getDeviceDao().deleteBySerial(serialNum); } catch (Exception e) { e.printStackTrace(); } } }).start(); } public Device getDevice(final String serialNum) { final Device[] finalDevice = new Device[1]; Thread thread = new Thread(new Runnable() { @Override public void run() { try { finalDevice[0] = getDeviceDao().getDevice(serialNum); } catch (Exception e) { e.printStackTrace(); } } }); thread.start(); try { thread.join(); } catch (Exception e) { e.printStackTrace(); } return finalDevice[0]; } public List&lt;Float&gt; getTempValues(final String serialNum) { final List&lt;Float&gt; finalTempValues = new ArrayList&lt;&gt;(); Thread thread = new Thread(new Runnable() { @Override public void run() { try { finalTempValues.addAll(getDeviceDao().getTempValues(serialNum)); } catch (Exception e) { e.printStackTrace(); } } }); thread.start(); try { thread.join(); } catch (Exception e) { e.printStackTrace(); } return finalTempValues; } public void setTempValues(final String serialNum, final List&lt;Float&gt; tempValues) { new Thread(new Runnable() { @Override public void run() { try { getDeviceDao().setTempValues(serialNum, tempValues); } catch (Exception e) { e.printStackTrace(); } } }).start(); } }</pre></div></object> - How to store List<Object> in Room database? (I'm having trouble retrieving the list with DAO query) 我在 WEB-INF/config 文件夹中有一个 system.properties 文件。 如何编写用于从该文件中检索数据的代码? - I have a system.properties file present in WEB-INF/config folder. How to write a code for retrieving data from this file? 不能为 child() 中的参数“pathString”传递 null? 这是我从 firebase 检索数据时遇到的错误 - Can't pass null for argument 'pathString' in child()? This the error I'm getting when retrieving data from firebase 如何从Android中的PHP服务器下载包含文件(pdf,png等)的文件夹 - how to download folder containing files(pdf,png etc) from php server in android android列出文件并将其从资产文件夹复制到应用程序数据 - android list and copy files from assets folder to app data 在运行时从Jar中检索和排序文件列表 - Retrieving and sorting list of files at runtime from Jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM