gpt4 book ai didi

android - 使用来自 Firebase 控制台的特定数据填充 ListView

转载 作者:行者123 更新时间:2023-11-29 23:49:42 24 4
gpt4 key购买 nike

我需要有关从 Firebase 控制台显示数据的帮助。所以场景是我使用一个名为“TicketSubmitted”的单独类将一些数据放在控制台中。我需要在“MainActivity”的 ListView 中显示数据的一些子项。 MainActivity 的模型类称为“Ticket”, ListView 的适配器称为“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;

}}

我只想在 ListView 中以设定的方式显示 TicketCategory、Subcategory 和优先级,但我无法这样做。此外,当我引入新的 childEventListener 时,出现此错误并且应用程序崩溃:

Screenshot

我是 android 编程的新手,所以任何输入都将不胜感激......

谢谢!!

最佳答案

解决这个错误:

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

关于android - 使用来自 Firebase 控制台的特定数据填充 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51022749/

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