我有我的应用程序,需要在列表视图中显示有关单击的项目的信息。但是,那是存储在我的Firebase数据库中的数据。该数据库如下所示:
现在,我想单击列表视图项,以便在其他活动中显示与之相对应的信息。
用于存储Listview和Firebase的MainActivity的代码为:
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;
public static String ComplaintID;
public static String StatusImg;
public static String TicketCat;
public static String SubCat;
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 = mFirebaseDatabase.getReference().child("Ticket");
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<Ticket> arrayOfTicket = new ArrayList<>();
adapter = new TicketAdapter(this, arrayOfTicket);
listView.setAdapter(adapter);
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();
assert ticket != null;
}
@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 = FirebaseDatabase.getInstance().getReference();
final DatabaseReference TicketCategory = mDatabaseReference.child("Ticket").child("ticketCategory");
TicketCategory.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String TCategory = dataSnapshot.child("ticketCategory").getValue(String.class);
TicketCat = TCategory;
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Ticket ticket = new Ticket();
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 ComplaintDetail extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complaint_detail);
TextView tvComplaintID = (TextView)findViewById(R.id.complaintid);
TextView tvTicketCat = (TextView)findViewById(R.id.TicketHeading);
TextView tvSubCat = (TextView)findViewById(R.id.Subheading);
//tvComplaintID.setText(MainActivity.StatusImg);
tvTicketCat.setText( MainActivity.TicketCat);
// tvSubCat.setText(MainActivity.SubCat);
final Button butt = findViewById(R.id.review);
butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(ComplaintDetail.this, Review.class);
startActivity(intent);
}
});
final Button button = findViewById(R.id.close);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(ComplaintDetail.this, "This ticket is closed!!", Toast.LENGTH_SHORT).show();
}
});
}}
我要显示以下子项: ticketCategory,子类别和状态。 我将不胜感激任何帮助。 谢谢!!
[ EDIT ]
public class Ticket {
String ticketCategory;
String name;
String complaintID;
String subcategory;
String priority;
String status;
private String comments;
private String cannedReply;
public Ticket(){}
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 setTicketCategory(String ticketCategory) {
this.TicketCategory = ticketCategory;
}
public void setName(String name) {
this.Name = name;
}
public void setSubcategory(String subcategory) {
this.Subcategory = subcategory;
}
public void setPriority(String priority) {
this.priority = priority;
}*/
public void setStatus(String status) {
this.status = status;
}
public String getTicketCategory() {
return ticketCategory;
}
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 String getComplaintID() {
return complaintID;
}
public static ArrayList<Ticket> getTicket()
{
ArrayList<Ticket> tickets = new ArrayList<Ticket>();
return tickets;
}}
答案 0 :(得分:0)
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);
intent.putExtra("ticket", ticket);
startActivity(intent);
}
});
我在意图中提到的票证对象是您从fireabse收到的对象,您需要使一个对象成为可仿品才能将其发送到另一个活动,因此请确保使其可仿造。 下面是带有可实现接口的模型类:-
public class Ticket implements Parcelable {
private String ticketCategory;
private String name;
private String complaintID;
private String subcategory;
private String priority;
private String status;
private String comments;
private String cannedReply;
public Ticket() {
}
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 String getTicketCategory() {
return ticketCategory;
}
public void setTicketCategory(String ticketCategory) {
this.ticketCategory = ticketCategory;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getComplaintID() {
return complaintID;
}
public void setComplaintID(String complaintID) {
this.complaintID = complaintID;
}
public String getSubcategory() {
return subcategory;
}
public void setSubcategory(String subcategory) {
this.subcategory = subcategory;
}
public String getPriority() {
return priority;
}
public void setPriority(String priority) {
this.priority = priority;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
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;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.ticketCategory);
dest.writeString(this.name);
dest.writeString(this.complaintID);
dest.writeString(this.subcategory);
dest.writeString(this.priority);
dest.writeString(this.status);
dest.writeString(this.comments);
dest.writeString(this.cannedReply);
}
protected Ticket(Parcel in) {
this.ticketCategory = in.readString();
this.name = in.readString();
this.complaintID = in.readString();
this.subcategory = in.readString();
this.priority = in.readString();
this.status = in.readString();
this.comments = in.readString();
this.cannedReply = in.readString();
}
public static final Parcelable.Creator<Ticket> CREATOR = new Parcelable.Creator<Ticket>() {
@Override
public Ticket createFromParcel(Parcel source) {
return new Ticket(source);
}
@Override
public Ticket[] newArray(int size) {
return new Ticket[size];
}
};
}
因此,我有set和get方法并实现了parceable,即使该类成为parceable,以便您可以发送整个对象。现在在您将要接收的其他活动中,您可以使用下面的get方法>
首先通过onCreate中的getIntent()获取意图,然后声明对象Ticket。
ticket = getIntent().getParcelableExtra("Ticket");
在需要该对象的位置,您可以像下面这样直接访问它
ticket.getSubcategory();
ticket.getTicketCategory();
ticket.getStatus();
所以,这就是询问我是否没有得到任何东西以及它是否不起作用。
答案 1 :(得分:0)
只需将您的票证类设置为可序列化,然后将其传递给Bundel并在其他活动(如波纹管)中进行解析
首先像下面这样更改Ticket类的定义
public class Ticket implements Serializable {
然后在更改您的点击代码后,如下所示
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);
intent.putExtra("ticket", ticket);
startActivity(intent);
}
});
最后解析并在下面的活动中获取对象,例如波纹管
Ticket ticket= (Ticket) getIntent().getSerializableExtra("ticket");