我正在创建一个应用程序,其中只有一个用户(管理员)可以在应用程序中发布,而其他用户只能阅读新闻。 我已禁止在Firebase中为用户编写内容,但我只想向管理员用户显示添加按钮
这是main_menu xml代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.ekene.blogzone.MainActivity">
<item
android:id="@+id/action_shto"
android:icon="@drawable/shto"
app:showAsAction="always"
android:title="Shto"
/>
<item
android:id="@+id/dil"
android:icon="@drawable/dil"
app:showAsAction="always" />
这是MainActivity代码:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private LinearLayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setBackgroundDrawable(new
ColorDrawable(getResources().getColor(R.color.white)));
//Shfaqë Welcome Screen vetëm pas instalimit të aplikacionit
Thread t = new Thread(new Runnable() {
@Override
public void run() {
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
boolean isFirstStart = getPrefs.getBoolean("firstStart", true);
if (isFirstStart) {
final Intent i = new Intent(MainActivity.this,
Welcome.class);
runOnUiThread(new Runnable() {
@Override public void run() {
startActivity(i);
}
});
SharedPreferences.Editor e = getPrefs.edit();
e.putBoolean("firstStart", false);
e.apply();
finish();
}
}
});
t.start();
recyclerView = (RecyclerView) findViewById(R.id.rcView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
layoutManager = new LinearLayoutManager(MainActivity.this,
RecyclerView.HORIZONTAL, false);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
mDatabase =
FirebaseDatabase.getInstance().getReference().child("Tech_News");
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener()
{
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth)
{
if (mAuth.getCurrentUser()==null)
{
Intent kyqjaIntent = new Intent(MainActivity.this,
KyqjaActivity.class);
kyqjaIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(kyqjaIntent);
}
}
};
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
FirebaseRecyclerAdapter<TechNews, TechNewsViewHolder>
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<TechNews,
TechNewsViewHolder>(
TechNews.class,
R.layout.card_items,
TechNewsViewHolder.class,
mDatabase
) {
@Override
protected void populateViewHolder(final TechNewsViewHolder
viewHolder, final TechNews model, int position) {
final String post_key = getRef(position).getKey().toString();
mDatabase.child(post_key).addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String titulli_postit = (String)
dataSnapshot.child("Titulli").getValue();
String pershkrimi_postit = (String)
dataSnapshot.child("Përshkrimi").getValue();
String fotografia_postit = (String)
dataSnapshot.child("fotografiaUrl").getValue();
String emri = (String)
dataSnapshot.child("Përdoruesi").getValue();
viewHolder.setTitulli(titulli_postit);
viewHolder.setPershkrimi(pershkrimi_postit);
viewHolder.setFotoUrl(getApplicationContext(),
fotografia_postit);
viewHolder.setEmri(emri);
viewHolder.mView.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view)
{
Intent postihapurActivity = new
Intent(MainActivity.this, PostiHapurActivity.class);
postihapurActivity.putExtra("PostiID",
post_key);
startActivity(postihapurActivity);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class TechNewsViewHolder extends RecyclerView.ViewHolder
{
View mView;
public TechNewsViewHolder(View itemView)
{
super(itemView);
mView = itemView;
}
public void setTitulli(String titulli)
{
TextView titulli_postit =
mView.findViewById(R.id.posti_titulli_txtview);
titulli_postit.setText(titulli);
}
public void setPershkrimi(String pershkrimi)
{
TextView pershkrimi_postit =
mView.findViewById(R.id.posti_pershkrimi_txtview);
pershkrimi_postit.setText(pershkrimi);
}
public void setFotoUrl (Context ctx, String fotoUrl)
{
ImageView foto_posti = mView.findViewById(R.id.posti_fotoja);
Picasso.with(ctx).load(fotoUrl).into(foto_posti);
}
public void setEmri (String emri)
{
TextView emri_posti = mView.findViewById(R.id.posti_emri);
emri_posti.setText(emri);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_shto)
{
startActivity(new Intent(MainActivity.this, PostiActivity.class));
}
else if (id == R.id.dil)
{
mAuth.signOut();
Intent dilIntent = new Intent(MainActivity.this, KyqjaActivity.class);
dilIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dilIntent);
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
您可以在setVisibile(true)
方法中使用onCreateOptionsMenu
方法隐藏特殊操作,如下所示:
MenuItem addAction = menu.findItem(R.id.add_action).setVisible(true);
答案 1 :(得分:0)
我建议您创建一个保存用户类型的变量:例如,在注册期间,如果用户是admin,则将用户类型变量存储为“ Admin”,然后检查用户类型是否等于“ Admin” ”显示添加按钮,否则不要