我对Android中的Room及其POST和GET机制有疑问。我在这个网站的帮助下创建了一个带有回收视图的应用程序:https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#0作为教程,但是这个人的代码与我的代码之间的区别在于他使用了一个带有一个字符串的类,而我使用了一个带有4个类的类字符串。
这些字符串值应该是几个“编辑”文本视图文本的值。虽然他们应该在本教程中看到,但是他们应该从房间获取数据。我已经完成了教程,直到最后两个滑块,并且还没有理解我在下面的代码中应该更改的内容,以便我可以填充我的Room数据库类。
所以我可以从我的Create_Customer活动发布到房间,然后在我的主要活动中获取数据库并用数据填充recycleview。下面是我遇到问题的代码。
Create_Customer:
Customer customer = new Customer(data.getStringExtra(NewWordActivity.EXTRA_REPLY));
public void onClick(View view) {
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(mEditWordView.getText())) {
setResult(RESULT_CANCELED, replyIntent);
} else {
String word = mEditWordView.getText().toString();
replyIntent.putExtra(EXTRA_REPLY, word);
setResult(RESULT_OK, replyIntent);
}
finish();
}
主要活动:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == NEW_WORD_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
Customer customer = new Customer(data.getStringExtra(NewWordActivity.EXTRA_REPLY));
mWordViewModel.insert(word);
} else {
Toast.makeText(
getApplicationContext(),
R.string.empty_not_saved,
Toast.LENGTH_LONG).show();
}
}
我需要上面代码的帮助,这是我的 适配器:
package com.example.jenso.paperseller;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class PapperRecyclerAdapter extends RecyclerView.Adapter<PapperRecyclerAdapter.CustomerViewHolder> {
class CustomerViewHolder extends RecyclerView.ViewHolder{
private TextView textViewName;
private TextView textViewAddress;
private TextView textViewPhoneNumber;
private TextView textViewEmail;
private CustomerViewHolder(View itemView){
super(itemView);
textViewName = itemView.findViewById(R.id.nameTxt);
textViewAddress = itemView.findViewById(R.id.addressTxt);
textViewPhoneNumber = itemView.findViewById(R.id.PhoneNumberTxt);
textViewEmail = itemView.findViewById(R.id.emailTxt);
}
}
private List<Customer> mCustomers;
private Context context;
private final LayoutInflater mInflater;
public PapperRecyclerAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public CustomerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.list_item, parent, false);
return new CustomerViewHolder(itemView);
}
@Override
public void onBindViewHolder(CustomerViewHolder holder, int position) {
if(mCustomers != null){
Customer current = mCustomers.get(position);
holder.textViewName.setText(current.getFullName());
holder.textViewAddress.setText(current.getAddress());
holder.textViewPhoneNumber.setText(current.getPhonenumber());
holder.textViewEmail.setText(current.getEmail());
}else{
holder.textViewName.setText("Full name");
holder.textViewAddress.setText("Address");
holder.textViewPhoneNumber.setText("PhoneNumber");
holder.textViewEmail.setText("Email");
}
}
void setCustomer(List<Customer> customers){
mCustomers = customers;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
if(mCustomers != null){
return mCustomers.size();
}else{
return 0;
}
}
public class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View itemView) {
super(itemView);
}
}
}
他从哪里获取数据以及我应该如何使用它以便我可以用我得到的数据填充所有字符串?
答案 0 :(得分:0)
更改构造函数
public PapperRecyclerAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
到
public PapperRecyclerAdapter(Context context, ArrayList<Customer> myList) {
mInflater = LayoutInflater.from(context);
mCustomer = myList;
}
在此处将列表存储在全局变量中。
您现在可以使用它来填充您的值,在onBindViewHolder中您拥有列表中项目的位置。 Customer current = mCustomers.get(position);
为您的客户提供
编辑:您可以看到此行if(mCustomers != null){
您无法通过内部,因为您永远不会初始化您的mCustomers变量
您需要创建自动播放列表视图并将其绑定到它的ID
RecyclerView recyclerView = (RecyclerView) findViewbyId(R.id.yourrecyclerid)
然后,您需要创建适配器并将其添加到回收站
PapperRecyclerAdapter adapter = new PapperRecyclerAdapter(getContext(), yourArrayListOfCustomer);
recyclerview.setAdapter(adapter);
答案 1 :(得分:0)
我自己遇到了同样的问题,我想从另一个editText获取另一个文本并将其显示在RecyclerView上,而我刚刚解决了它。我不知道是否有更好的解决方案,请与我们分享。
下面是我的代码:
ListAdapter:
public class MaterialListAdapter extends RecyclerView.Adapter<MaterialListAdapter.ViewHolder> {
class ViewHolder extends RecyclerView.ViewHolder {
private final TextView firstWordItemView;
private final TextView secondWordItemView;
private ViewHolder(View itemView) {
super(itemView);
firstWordItemView = itemView.findViewById(R.id.textView);
secondWordItemView = itemView.findViewById(R.id.textView2);
}
}
private final LayoutInflater mInflater;
private List<MaterialEntity> mMaterial; // Cached copy of words
public MaterialListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.recyclerview_material_item, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (mMaterial != null) {
MaterialEntity current = mMaterial.get(position);
holder.firstWordItemView.setText(current.getMaterialName());
holder.secondWordItemView.setText(current.getMaterialBrand());
} else {
// Covers the case of data not being ready yet.
holder.firstWordItemView.setText("No Word");
holder.secondWordItemView.setText("No Word");
}
}
public void setMaterial(List<MaterialEntity> materials){
mMaterial = materials;
notifyDataSetChanged();
}
// getItemCount() is called many times, and when it is first called,
// mWords has not been updated (means initially, it's null, and we can't return null).
@Override
public int getItemCount() {
if (mMaterial != null)
return mMaterial.size();
else return 0;
}
}
NewMaterialActivity(又称NewWordActivity):
public class NewMaterialActivity extends AppCompatActivity {
public static final String EXTRA_REPLY = "com.example.android.materiallistsql.REPLY";
public static final String EXTRA_REPLY2 = "com.example.android.materiallistsql.REPLY2";
private EditText mEditWordView;
private EditText mEditWordView2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_material);
mEditWordView = findViewById(R.id.edit_word);
mEditWordView2 = findViewById(R.id.edit_word2);
final Button button = findViewById(R.id.button_save);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(mEditWordView.getText())||
TextUtils.isEmpty(mEditWordView2.getText())) {
setResult(RESULT_CANCELED, replyIntent);
} else {
String word = mEditWordView.getText().toString();
String word2 = mEditWordView2.getText().toString();
replyIntent.putExtra(EXTRA_REPLY, word);
replyIntent.putExtra(EXTRA_REPLY2, word2);
setResult(RESULT_OK, replyIntent);
}
finish();
}
});
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private MaterialViewModel mMaterialViewModel;
public static final int NEW_MATERIAL_ACTIVITY_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, NewMaterialActivity.class);
startActivityForResult(intent, NEW_MATERIAL_ACTIVITY_REQUEST_CODE);
}
});
RecyclerView recyclerView = findViewById(R.id.recyclerview);
final MaterialListAdapter adapter = new MaterialListAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mMaterialViewModel = ViewModelProviders.of(this).get(MaterialViewModel.class);
mMaterialViewModel.getAllMaterials().observe(this, new Observer<List<MaterialEntity>>() {
@Override
public void onChanged(@Nullable final List<MaterialEntity> materials) {
// Update the cached copy of the words in the adapter.
adapter.setMaterial(materials);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == NEW_MATERIAL_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
MaterialEntity material = new MaterialEntity(data
.getStringExtra(NewMaterialActivity.EXTRA_REPLY),
data.getStringExtra(NewMaterialActivity.EXTRA_REPLY2));
mMaterialViewModel.insertMaterial(material);
} else {
Toast.makeText(
getApplicationContext(),
R.string.empty_not_saved,
Toast.LENGTH_LONG).show();
}
}
}