gpt4 book ai didi

android - 如何从 Android LiveData> 获取简单的 List

转载 作者:行者123 更新时间:2023-11-30 05:05:56 26 4
gpt4 key购买 nike

我是 Android 的新手,通过开发简单的应用程序来学习相同的知识,该应用程序由我使用 android Room 数据库访问的单个客户表组成。

客户实体类是

@Entity(tableName = "Customers")
public class CustomerEntity {
@PrimaryKey(autoGenerate = true)
private int customerId;
private String customerName;
private String customerAddress;
private String customerZipCode;
private String customerEMailId;
}

Customer Dao 接口(interface)是

@Dao
public interface CustomerDao {
@Insert
public void insertCustomer( CustomerEntity customerEntity );
@Update
public void updateCustomer( CustomerEntity customerEntity );
@Delete
public void deleteCustomer( CustomerEntity customerEntity) ;
@Query("SELECT * FROM Customers")
LiveData<List<CustomerEntity>> getAllCustomers();
@Query("SELECT * FROM Customers WHERE customerZipCode == :givenZipCode ")
LiveData<List<CustomerEntity>> getGivenZipCodeCustomer( String givenZipCode);
@Query("SELECT * FROM Customers WHERE customerZipCode == :givenZipCode ")
List<CustomerEntity> getGivenZipCodeCustomerList( String givenZipCode);
@Query("SELECT * FROM Customers WHERE customerZipCode == :givenZipCode ")
Cursor getGivenZipCodeCustomerCursor(String givenZipCode
}

Customer Repository 类(部分显示)

public List<CustomerEntity> getGivenZipCodeCustomersList(CustomerEntity customerEntity){
CustomerRepository.CustomerZipCodeAsyncTask customerZipCodeAsyncTask ;
customerZipCodeAsyncTask = new CustomerRepository.CustomerZipCodeAsyncTask( customerDao );
givenZipCodeCustomersList = customerZipCodeAsyncTask.doInBackground( customerEntity );
return givenZipCodeCustomersList ;
}

private static class CustomerZipCodeAsyncTask extends AsyncTask< CustomerEntity ,
Void , List < CustomerEntity > > {
private CustomerDao customerDao;
private CustomerZipCodeAsyncTask ( CustomerDao customerDao ){
this.customerDao = customerDao;
}

@Override
protected List < CustomerEntity > doInBackground(CustomerEntity... customerEntities) {
String zipCode = customerEntities[0].getCustomerZipCode();
return ( customerDao.getGivenZipCodeCustomerList( zipCode ) ) ;
}
}

当我尝试从应用程序的其他部分获取客户列表时,我收到消息

"java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time."

另一方面,如果我尝试通过执行另一个异步进程来获取客户列表,该进程成功返回 LiveData 列表,但当我在 LiveData 上使用 getValue() 时返回 null。

在我执行此任务的应用程序部分,我不希望检索到的列表会发生变化,也不需要将其呈现给用户。所以我不需要观察这个列表。我需要一个简单的列表,我可以从中访问列表项并进一步处理它们。

我使用的是 Android Studio 3.4 Canary 9,androidx room_version = "2.1.0-alpha03", androix lifecycle_version = "2.0.0"

最佳答案

通过在 LiveData 上使用 getValue(),您没有使用 LiveData 的全部功能。在您的 CustomerRepository 类中,将发布的方法更改为:

public LiveData<List<CustomerEntity>> getGivenZipCodeCustomersList(CustomerEntity customerEntity) {
String zipCode = customerEntity.getCustomerZipCode();
return customerDao.getGivenZipCodeCustomerList(zipCode);
}

那么你的 Activity 可能看起来像这样

public class MainActivity extends AppCompatActivity {

private CustomerRepository customerRepository;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

customerRepository = ... //init your repo here
CustomerEntity customerEntity = ... //init your customer entity here

customerRepository.getGivenZipCodeCustomersList(customerEntity).observe(this, new Observer<List<CustomerEntity>> {
@Override
public void onChanged(@Nullable List<CustomerEntity> customerEntities) {
//do stuff with your customerEntities here
}
});

//.. more code
}
}

这样做,每当您的客户匹配到您的数据库中 CustomerEntity 中的邮政编码更改时,onChanged 就会触发,以便您可以自动处理更改。

有关详细信息,请参阅 documentation如何使用 LiveData

希望对您有所帮助!

关于android - 如何从 Android LiveData<List<T>> 获取简单的 List<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54604602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com