#java #android-studio
#Ява #android-студия
Вопрос:
[enter image description here][1] enter code here import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.Menu; import android.view.ViewGroup; import android.widget.RelativeLayout; import android.widget.TextView; import androidx.appcompat.widget.Toolbar; import com.example.shopingonline.Model.Products; import com.example.shopingonline.Model.Users; import com.example.shopingonline.Prevalent.Prevalent; import com.example.shopingonline.ViewHolder.ProductViewHolder; import com.firebase.ui.database.FirebaseRecyclerAdapter; import com.firebase.ui.database.FirebaseRecyclerOptions; import com.google.android.material.snackbar.Snackbar; import com.google.android.material.navigation.NavigationView; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.core.view.GravityCompat; import androidx.customview.widget.Openable; import androidx.navigation.NavController; import androidx.navigation.Navigation; import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; import androidx.drawerlayout.widget.DrawerLayout; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.example.shopingonline.databinding.ActivityHomeBinding; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.squareup.picasso.Picasso; import de.hdodenhof.circleimageview.CircleImageView; import io.paperdb.Paper; public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { private AppBarConfiguration mAppBarConfiguration; private DatabaseReference ProductsRef; private RecyclerView recyclerView; RecyclerView.LayoutManager layoutManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); ProductsRef = FirebaseDatabase.getInstance().getReference().child("Products"); Paper.init(this); Toolbar toolbar = findViewById(R.id.toolbar); toolbar.setTitle("Home"); 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); View headerView = navigationView.getHeaderView(0); TextView userNameTextView = headerView.findViewById(R.id.user_profile_name); CircleImageView profileImageView = headerView.findViewById(R.id.user_profile_image); userNameTextView.setText(Prevalent.currentOnlineUser.getName()); // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. mAppBarConfiguration = new AppBarConfiguration.Builder( R.id.nav_cart, R.id.nav_categories, R.id.nav_logout,R.id.nav_settings, R.id.nav_orders, R.id.nav_home) .setOpenableLayout(drawer) .build(); //NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_container); //NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration); // NavigationUI.setupWithNavController(navigationView, navController); recyclerView = findViewById(R.id.recycler_menu); recyclerView.setHasFixedSize(true); layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); } @Override protected void onStart() { super.onStart(); FirebaseRecyclerOptionslt;Productsgt; options = new FirebaseRecyclerOptions.Builderlt;Productsgt;() .setQuery(ProductsRef,Products.class) .build(); FirebaseRecyclerAdapterlt;Products, ProductViewHoldergt; adapter = new FirebaseRecyclerAdapterlt;Products, ProductViewHoldergt;(options) { @Override protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull Products model) { holder.txtProductName.setText(model.getPname()); holder.txtProductDescription.setText(model.getDescription()); holder.txtProductPrice.setText("Price = " model.getPrice()); Picasso.get().load(model.getImage()).into(holder.imageView); } @NonNull @Override public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items_layout, parent, false); ProductViewHolder holder = new ProductViewHolder(v); return holder; } }; recyclerView.setAdapter(adapter); adapter.startListening(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.home, menu); return true; } // @Override //public boolean onSupportNavigateUp() { //NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_container); // return NavigationUI.navigateUp(navController, mAppBarConfiguration) // || super.onSupportNavigateUp(); // } @Override public boolean onOptionsItemSelected(MenuItem item){ int id = item.getItemId(); return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.nav_cart) { } else if (id == R.id.nav_orders) { } else if (id == R.id.nav_categories) { } else if (id == R.id.nav_settings) { Intent intent = new Intent(HomeActivity.this,settinsActivity.class); startActivity(intent); } else if (id == R.id.nav_logout) { Paper.book().destroy(); Intent intent = new Intent(HomeActivity.this,MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); finish(); } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } } enter code here lt;?xml version="1.0" encoding="utf-8"?gt; lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.shopingonline"gt; lt;application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.ShopingOnline"gt; lt;activity android:name=".settinsActivity" android:exported="false" /gt; lt;activity android:name=".HomeActivity" android:exported="false" android:label="@string/title_activity_home" android:theme="@style/Theme.ShopingOnline.NoActionBar" /gt; lt;activity android:name=".AdminCategoryActivity" android:exported="false" /gt; lt;activity android:name=".AdminAddNewProductActivity" android:exported="false" /gt; lt;activity android:name=".RegisterActivity" android:exported="false" /gt; lt;activity android:name=".LoginActivity" android:exported="false" /gt; lt;activity android:name=".MainActivity" android:exported="true"gt; lt;intent-filtergt; lt;action android:name="android.intent.action.MAIN" /gt; lt;category android:name="android.intent.category.LAUNCHER" /gt; lt;/intent-filtergt; lt;/activitygt; lt;/applicationgt; lt;/manifestgt; enter code here lt;?xml version="1.0" encoding="utf-8"?gt; lt;menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:showIn="navigation_view"gt; lt;group android:checkableBehavior="single"gt; lt;item android:id="@ id/nav_cart" android:icon="@drawable/ic_cart" android:title="Import" android:iconTint="@android:color/white"/gt; lt;item android:id="@ id/nav_orders" android:icon="@drawable/ic_orders" android:title="Orders" android:iconTint="@android:color/white"/gt; lt;item android:id="@ id/nav_categories" android:icon="@drawable/ic_category" android:title="Categories" android:iconTint="@android:color/white"/gt; lt;item android:id="@ id/nav_settings" android:icon="@drawable/ic_settings" android:title="Settings" android:iconTint="@android:color/white"/gt; lt;item android:id="@ id/nav_logout" android:icon="@drawable/ic_logout"## Heading ## android:title="Logout" android:iconTint="@android:color/white"/gt; lt;/groupgt; lt;/menugt; this is the image and the code of the HomeActivity.java actually i'm doing a tutorial and i found some difficult because of the new version of android studio i add an Intent as you see in the onNavigationItemSelected(MenuItem item) methode to go to another activity but it doesn't work when i click in the button of the bar like logout or settings can you help me to found the problem please Thanks [1]: https://i.stack.imgur.com/27k0L.png
это изображение и код HomeActivity.java на самом деле я делаю учебник, и я обнаружил некоторые трудности из-за новой версии Android studio. Я добавляю намерение, как вы видите в методе onNavigationItemSelected(элемент меню), чтобы перейти к другому действию, но оно не работает, когда я нажимаю кнопку на панели, например, выход из системы или настройки. можете ли вы помочь мне найти проблему, пожалуйста, спасибо