我需要有关从Firebase控制台显示数据的帮助。因此,场景是我使用一个名为“ TicketSubmitted”的单独类在控制台中放置了一些数据。我需要在“ MainActivity”的列表视图中显示数据的某些子项。 MainActivity的模型类称为“ Ticket”,列表视图的适配器称为“ TicketAdapter”
MainActivity的代码为:
package com.android.example.ithelpdesk;
//imports
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
ActionBarDrawerToggle toggle;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mDatabaseReference;
private ChildEventListener mChildEventListener;
private FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
public TicketAdapter adapter;
private final static int RC_SIGN_IN = 2;
public String mUsername;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
FirebaseUser user = mFirebaseAuth.getCurrentUser();
mUsername = user.getUid();
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = mFirebaseAuth.getCurrentUser();
if (user != null) {
onSignedInInitialized(user.getDisplayName());
} else {
onSignedOutCleanup();
startActivityForResult(
getInstance()
.createSignInIntentBuilder()
.setIsSmartLockEnabled(false)
.setAvailableProviders(Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build()))
.build(),
RC_SIGN_IN);
}
}
};
final ListView listView = (ListView) findViewById(R.id.lvTicket);
final ArrayList<String> arrayOfTicket = new ArrayList<>()
mDatabaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Ticket ticket = (Ticket)dataSnapshot.getValue(Ticket.class);
adapter.add(ticket);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
}
);mDatabaseReference.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
}
});
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(view.getContext(), ComplaintDetail.class);
startActivity(intent);
}
});
final Button button = (Button) findViewById(R.id.complaintbutton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, TicketCategory.class);
startActivity(intent);
}
}
);
}
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
toggle.syncState();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.action_settings:
{
AuthUI.getInstance().signOut(this);
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
private void detachDatabaseReadListener()
{
if(mChildEventListener != null)
{
mDatabaseReference.removeEventListener(mChildEventListener);
mChildEventListener = null;
}
}
@Override
protected void onResume()
{
super.onResume();
mFirebaseAuth.addAuthStateListener(mAuthStateListener);
}
@Override
protected void onPause()
{
super.onPause();
mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
}
private void onSignedInInitialized(String username)
{
mUsername = username;
// attachDatabaseReadListener();
}
private void onSignedOutCleanup()
{
mUsername = null;
}
模型类票证:
public class Ticket {
public String TicketCategory;
// public String Name;
public String Subcategory;
public String priority;
private String status;
private String comments;
private String cannedReply;
public Ticket (String TicketCategory, String Subcategory, String priority, String status, String comments, String cannedReply)
{
this.TicketCategory = TicketCategory;
// this.Name = Name;
this.Subcategory = Subcategory;
this.priority = priority;
this.status = status;
this.comments = comments;
this.cannedReply = cannedReply;
}
public void setStatus(String status) {
this.status = status;
}
public String getTicketCategory() {
return TicketCategory;
}
//public String getName(){return Name;}
public String getSubcategory() {
return Subcategory;
}
public String getPriority() {
return priority;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getCannedReply() {
return cannedReply;
}
public void setCannedReply(String cannedReply) {
this.cannedReply = cannedReply;
}
public String getStatus() {
return status;
}
public static ArrayList<Ticket> getTicket()
{
ArrayList<Ticket> tickets = new ArrayList<Ticket>();
return tickets;
}}
适配器TicketAdapter的代码为:
public class TicketAdapter extends ArrayAdapter<Ticket> {
TicketAdapter(Context context, ArrayList<Ticket> tickets) {
super(context, 0, tickets);
}
@SuppressLint("SetTextI18n")
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
Ticket ticket = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.tickets, parent, false);
}
TextView tvCategory = (TextView) convertView.findViewById(R.id.TicketCategory);
TextView tvSubcategory = (TextView) convertView.findViewById(R.id.Subcategory);
TextView tvPriority = (TextView) convertView.findViewById(R.id.priority);
assert ticket != null;
tvCategory.setText(ticket.TicketCategory);
tvSubcategory.setText(ticket.Subcategory);
tvPriority.setText(ticket.priority);
return convertView;
}}
我只想在列表视图中以设置的方式显示TicketCategory,Subcategory和优先级,但我无法这样做。另外,当我引入一个新的childEventListener时,也会出现此错误,并且应用程序崩溃:
我是android编程的新手,所以任何输入都会受到赞赏...
谢谢!
答案 0 :(得分:1)
要解决此错误:
com.google.firebase.database.DatabaseException: Found two getters or fields with conflicting case sensitivity for property: subcategory
请更改以下代码行:
public String TicketCategory;
// public String Name;
public String Subcategory;
public String priority;
到
private String TicketCategory;
// private String Name;
private String Subcategory;
private String priority;
不要忘记在您的Ticket
类中添加无参数构造函数。
public Ticket() {} //Needed for Firebase
答案 1 :(得分:0)
首先,您的日志说您需要重命名Ticket
字段,例如(不能以大写开头):
public String ticketCategory;
public String subcategory;
然后,您必须将adapter
与arrayOfTicket
实例化,然后将adapter
设置为ListView
(+不要忘记在出现以下情况时致电adapter.notifyDataSetChanged()
您在列表中添加/删除对象):
final ListView listView = (ListView) findViewById(R.id.lvTicket);
final ArrayList<Ticket> arrayOfTicket = new ArrayList<>();
adapter = new TicketAdapter(this, arrayOfTicket); // Instanciate your adapter
listView.setAdapter(adapter); // Set the adapter to your ListView
mDatabaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Ticket ticket = (Ticket)dataSnapshot.getValue(Ticket.class);
adapter.add(ticket);
adapter.notifyDataSetChanged(); // Notify for changes to reload the list
}
...
答案 2 :(得分:0)
我想您的问题在于引用数据的方式,您似乎没有引用必须列出数据的实际节点。
让我们说保存数据的节点称为Tickets
所以代替这个:
mDatabaseReference.addChildEventListener(new ChildEventListener() {....
执行此操作:
DatabaseReference tickets_ref = mDatabaseReference.child("Tickets");
tickets_ref.addChildEventListener(new ChildEventListener() {....
代替此:
final ArrayList<String> arrayOfTicket = new ArrayList<>();
执行此操作:
final ArrayList<Ticket> arrayOfTicket = new ArrayList<>();
此后:
final ArrayList<Ticket> arrayOfTicket = new ArrayList<>();
像这样初始化适配器:
adapter = new TicketAdapter(context,arrayOfTicket);
,而不是这个:
adapter.add(ticket);
执行此操作:
arrayOfTicket.add(ticket);
adapter.notifyDataSetChanged();