我正在链接仪表板中的一个按钮,这是一个简单的活动,可以通过打开包含从Firebase检索数据的PostListActivity来实现。但是,只要我运行我的应用程序并单击按钮,我的应用程序就会停止工作。我如何将PostListActivity的活动作为目标,以查看来自Firebase数据库的数据?
Dashboard.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
btn=(Button)findViewById(R.id.btn1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Dashboard.this, "Prevention Techniques", Toast.LENGTH_SHORT).show();
Intent intent= new Intent(Dashboard.this,PostsListActivity.class);
startActivity(intent);
}
});
btn1=(Button)findViewById(R.id.btn2) ;
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Dashboard.this, "Pest control", Toast.LENGTH_SHORT).show();
}
});
btn2=(Button)findViewById(R.id.btn) ;
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Dashboard.this, "Pest Detected", Toast.LENGTH_SHORT).show();
}
});
btn3=(Button)findViewById(R.id.btn4) ;
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Dashboard.this, "Pest Identication", Toast.LENGTH_SHORT).show();
}
});
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) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setItemIconTintList(null);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = 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.dashboard, 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);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.profile) {
// Handle the camera action
} else if (id == R.id.dashboard) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_tools) {
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
PostListActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_posts_list);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Prevention Techniques");
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager( this));
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Prevention Techniques");
}
//search data
private void firebaseSearch(String searchText){
String query = searchText.toLowerCase();
Query firebaseSearchQuery = mRef.orderByChild("search").startAt(searchText).endAt(searchText + "\uf0ff");
FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Model, ViewHolder>(
Model.class,
R.layout.row,
ViewHolder.class,
firebaseSearchQuery
) {
@Override
protected void populateViewHolder(ViewHolder viewHolder, Model model, int position) {
viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(),model.getImage());
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
viewHolder.setOnClickListener(new ViewHolder.ClickListener() {
@Override
public void onItemClick(View view, int position) {
String mTitle = getItem(position).getTitle();
String mDesc = getItem(position).getDescription();
String mImage = getItem(position).getImage();
Intent intent = new Intent(view.getContext(), PostDetailActivity.class);
intent.putExtra("title", mTitle);
intent.putExtra("description", mDesc);
intent.putExtra("image", mImage);
startActivity(intent);
}
@Override
public void onItemLongClick(View view, int position) {
}
});
return viewHolder;
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Model, ViewHolder>(
Model.class,
R.layout.row,
ViewHolder.class,
mRef
) {
@Override
protected void populateViewHolder(ViewHolder viewHolder, Model model, int position) {
viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(),model.getImage());
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
viewHolder.setOnClickListener(new ViewHolder.ClickListener() {
@Override
public void onItemClick(View view, int position) {
String mTitle = getItem(position).getTitle();
String mDet = getItem(position).getDetail();
String mImage = getItem(position).getImage();
Intent intent = new Intent(view.getContext(), PostDetailActivity.class);
intent.putExtra("title", mTitle);
intent.putExtra("description", mDet);
intent.putExtra("image", mImage);
startActivity(intent);
}
@Override
public void onItemLongClick(View view, int position) {
}
});
return viewHolder;
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
firebaseSearch(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
firebaseSearch(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.action_settings) {
//TODO
return true;
}
return super.onOptionsItemSelected(item);
}
}
ViewHolder.java
public class ViewHolder extends RecyclerView.ViewHolder {
View mView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mClickListener.onItemClick(view, getAdapterPosition());
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mClickListener.onItemLongClick(view,getAdapterPosition());
return true;
}
});
}
public void setDetails(Context ctx, String title, String description, String image) {
TextView mTitleTv = mView.findViewById(R.id.rTitleTv);
TextView mDtealiTv = mView.findViewById(R.id.rDescriptionTv);
ImageView mImageTv = mView.findViewById(R.id.rImageView);
mTitleTv.setText(title);
mDtealiTv.setText(description);
Picasso.get().load(image).into(mImageTv);
}
private ViewHolder.ClickListener mClickListener;
public interface ClickListener{
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
public void setOnClickListener(ViewHolder.ClickListener clickListener ){
mClickListener = clickListener;
}
}
Model.java
public class Model {
String title , image , description, detail;
public Model(){}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}