我已经在这里尝试了类似问题的一些建议,正如您在一些注释掉的代码中看到的那样。我是新人,所以你能帮帮我吗?如果您需要编码以尝试解决问题,请告诉我。提前谢谢。
我甚至试图做错误所说的但我真的不知道把GenericTypeIndicator放到哪里。
我收到此错误:
com.google.firebase.database.DatabaseException:类java.util.List 有泛型类型参数,请改用GenericTypeIndicator 在com.google.android.gms.internal.zzelw.zzb(未知来源) 在com.google.android.gms.internal.zzelw.zza(未知来源) 在com.google.android.gms.internal.zzelw.zzb(未知来源) 在com.google.android.gms.internal.zzelx.zze(未知来源) 在com.google.android.gms.internal.zzelw.zzb(未知来源) 在com.google.android.gms.internal.zzelw.zza(未知来源) 在com.google.firebase.database.DataSnapshot.getValue(未知来源) 在 com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29) ...
这是我的代码:
public class MainActivity extends AppCompatActivity {
@BindView(R.id.homeRecyclerView)
RecyclerView mHomeRecyclerView;
@BindView(R.id.emptyTextView)
TextView mEmptyTextView;
protected static final Query sInspirationQuery =
FirebaseDatabase.getInstance().getReference().child("Inspirations");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.mipmap.ic_launcher);
//mHomeRecyclerView.setHasFixedSize(true);
mHomeRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
attachRvAdapter();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
Drawable addInspirationDrawable = menu.findItem(R.id.addInspiration).getIcon();
Drawable userAccountDrawable = menu.findItem(R.id.userAccount).getIcon();
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.addInspiration) {
String className = "com.example.jessie.inspirationexchange.CreateActivity";
nextActivity(className);
} else if(id == R.id.userAccount) {
String className = "com.example.jessie.inspirationexchange.UserActivity";
nextActivity(className);
}
return super.onOptionsItemSelected(item);
}
private void nextActivity(String className) {
Intent intent = null;
try {
intent = new Intent(this, Class.forName(className));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
startActivity(intent);
}
public void attachRvAdapter() {
RecyclerView.Adapter adapter = newAdapter();
mHomeRecyclerView.setAdapter(adapter);
}
protected RecyclerView.Adapter newAdapter() {
FirebaseRecyclerOptions<Inspiration> options =
new FirebaseRecyclerOptions.Builder<Inspiration>()
.setQuery(sInspirationQuery, Inspiration.class)
.setLifecycleOwner(this)
.build();
return new FirebaseRecyclerAdapter<Inspiration, CardViewHolder>(options) {
@Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new CardViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.inspiration_card, parent, false));
}
@Override
protected void onBindViewHolder(@NonNull CardViewHolder holder, int position, @NonNull Inspiration model) {
holder.bind(model);
}
// SnapshotParser<Inspiration> mSnapshotParser = new SnapshotParser<Inspiration>() {
// @NonNull
// @Override
// public Inspiration parseSnapshot(@NonNull DataSnapshot snapshot) {
// GenericTypeIndicator<List<Inspiration>> t = new GenericTypeIndicator<List<Inspiration>>();
//
// return (Inspiration) snapshot.child("Inspirations").getValue(t);
// }
// };
@Override
public void onDataChanged() {
super.onDataChanged();
//GenericTypeIndicator<List<Inspiration>> t = new GenericTypeIndicator<List<Inspiration>>();
mEmptyTextView.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
}
};
}
}
我的模特:
public class Inspiration {
private String mTitle;
private String mBody;
private List<String> mCategoriesChose;
private String mAuthor;
public Inspiration() {
}
public Inspiration(String title, String body, List categories) {
mTitle = title;
mBody = body;
mCategoriesChose = categories;
}
public String getAuthor() {
return mAuthor;
}
public void setAuthor(String author) {
mAuthor = author;
}
public String getTitle() {
return mTitle;
}
public void setTitle(String title) {
mTitle = title;
}
public String getBody() {
return mBody;
}
public void setBody(String body) {
mBody = body;
}
public List<String> getCategoriesChose() {
return mCategoriesChose;
}
public void setCategoriesChose(List categoriesChose) {
mCategoriesChose = categoriesChose;
}
@Override
public String toString() {
return "Inspiration{" +
"mTitle='" + mTitle + '\'' +
", mBody='" + mBody + '\'' +
", mCategoriesChose=" + mCategoriesChose.toString() +
'}';
}
}
这是JSON的样子(只有1个对象)
{
"Inspirations" : {
"-L5un07HfHu0XT-XfLX2" : {
"author" : "User1",
"body" : "test 1",
"categoriesChose" : [ "Character", "Courage", "Determination" ],
"title" : "gov"
}
}
}
答案 0 :(得分:0)
您不能在主要对象中包含您尝试反序列化的列表和地图类型对象。这是因为Java类型擦除系统的工作方式。在运行时,通过反射无法知道列表或映射声明的泛型类型,这意味着它不知道哪些类型的对象可以放在那里。
当您尝试反序列化的顶级对象是一个通用列表时, getValue()
与<VirtualHost *:*>
ProxyPreserveHost On
ProxyPass "/" "http://123.123.123.12"
ProxyPassReverse "/" "http://123.123.123.12"
ServerName mydomain.com
</VirtualHost>
一起使用,但这是关于它的。如果它嵌入在任何地方,它就无法工作。错误消息在这一点上有点误导。您必须从快照中提取项目并自行创建对象。
答案 1 :(得分:0)
我通过删除数据库中的嵌套数组解决了这个问题。删除后,它就像一个魅力。如果有人想回答如何解决嵌套数组内部数据库的问题,请随时发帖。