انجمن سایت کلیدستان


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
مشکل در بخش علاقه مندی برنامه اندرویدی

مشکل در بخش علاقه مندی برنامه اندرویدی

#1
سلام خدمت اساتید گرامی . 

من یه برنامه نوشتن که برای یادگیری لغاته . وقتی کاربر لغتی رو به بخش علاقه مندی اضافه می کنه لغت نمایش داده نمیشه و باید برنامه رو مجدد اجرا کرد تا لغت انتخاب شده به بخش علاقه مندی اضافه/ حذف بشه . همچنین بعد از هر بار اجرا لیست لغات تکرار میشه ( مثلا بار دوم از هر کدوم 2 سری بار سوم از هرکدوم 3 سری و ... ) ایجاد میشه . ممنون میشم راهنماییم کنید . 


کد:
package farmani.com.essentialwordsforielts.mainPage;


import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import farmani.com.essentialwordsforielts.R;
import farmani.com.essentialwordsforielts.search.ActivitySearch;

public class MainActivity extends AppCompatActivity {

public static Context context;
public static ArrayList<Structure> list = new ArrayList<>();
public static ArrayList<Structure> favorite = new ArrayList<>();
DrawerLayout drawerLayout;
NavigationView navigationView;
ImageView hamburger;
SQLiteDatabase database;
String destPath;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.navigation_activity_main);

   if (Build.VERSION.SDK_INT >= 23) {


       if (ContextCompat.checkSelfPermission(MainActivity.this,
               Manifest.permission.READ_EXTERNAL_STORAGE) !=
               PackageManager.PERMISSION_GRANTED) {

           ActivityCompat.requestPermissions(MainActivity.this
                   , new String[]{Manifest.permission.READ_EXTERNAL_STORAGE
                           , Manifest.permission.WRITE_EXTERNAL_STORAGE}
                   , 1);

       } else if (ContextCompat.checkSelfPermission(MainActivity.this,
               Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
               PackageManager.PERMISSION_GRANTED) {

           ActivityCompat.requestPermissions(MainActivity.this
                   , new String[]{Manifest.permission.READ_EXTERNAL_STORAGE
                           , Manifest.permission.WRITE_EXTERNAL_STORAGE}
                   , 1);

       } else {

           setupDB();
           selectList();
           selectFavorite();



           Toast.makeText(MainActivity.this, "You grandet earlier",
Toast.LENGTH_LONG).show();

       }


   }

   if (!favorite.isEmpty()){
       favorite.clear();
       selectFavorite();
   } else if (!list.isEmpty()){
       list.clear();
       selectList();
   }

   context = getApplicationContext();
   setTabOption();

   drawerLayout = findViewById(R.id.navigation_drawer);
   navigationView = findViewById(R.id.navigation_view);
   hamburger = findViewById(R.id.hamburger);
   hamburger.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           drawerLayout.openDrawer(Gravity.START);
       }
   });

   navigationView.setNavigationItemSelectedListener(new
  NavigationView.OnNavigationItemSelectedListener() {
       @Override
       public boolean onNavigationItemSelected(@NonNull MenuItem item) {
           int id = item.getItemId();
           if (id == R.id.exit) {
               AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                       MainActivity.this);

               alertDialog.setTitle(R.string.exit);

               alertDialog.setMessage(R.string.exit_ask);
               alertDialog.setCancelable(false);

               alertDialog.setPositiveButton(R.string.yes,
                       new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int
                                   which) {
                               finish();
                           }
                       });

               alertDialog.setNegativeButton(R.string.no,
                       new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int
                                   which) {

                               dialog.cancel();
                           }
                       });

               alertDialog.show();
           }

           if (id == R.id.search) {
               Intent intent = new Intent(MainActivity.this,
   ActivitySearch.class);
               MainActivity.this.startActivity(intent);
           }

           return true;
       }
   });


   }

  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[]
 permissions, @NonNull int[] grantResults) {
   switch (requestCode) {
       case 1: {

           if (grantResults.length >= 2 && grantResults[0] ==
                   PackageManager.PERMISSION_GRANTED && grantResults[1] ==
                   PackageManager.PERMISSION_GRANTED) {


               Toast.makeText(MainActivity.this, "Access granted",
                       Toast.LENGTH_LONG).show();

           }

       }
   }
 }


   @Override
   public void onBackPressed() {
       if (drawerLayout.isDrawerOpen(Gravity.START)) {
       drawerLayout.closeDrawer(Gravity.START);
       } else {
       AlertDialog.Builder alertDialog = new AlertDialog.Builder(
               MainActivity.this);

       alertDialog.setTitle(R.string.exit);

       alertDialog.setMessage(R.string.exit_ask);
       alertDialog.setCancelable(false);

       alertDialog.setPositiveButton(R.string.yes,
               new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                       finish();
                   }
               });

       alertDialog.setNegativeButton(R.string.no,
               new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {

                       dialog.cancel();
                   }
               });

       alertDialog.show();
   }
}


   private void setTabOption() {
      ViewPager viewPager = findViewById(R.id.viewpager);
      viewPager.setAdapter(new AdapterFragment(getSupportFragmentManager(),
           context));
      TabLayout tabStrip = findViewById(R.id.tabs);
      tabStrip.setupWithViewPager(viewPager);
   }

   private void setupDB() {
   try {
       destPath =
               Environment.getExternalStorageDirectory().getAbsolutePath()
   + "/ielts/";
       File file = new File(destPath);
       if (!file.exists()) {
           file.mkdirs();
           file.createNewFile();
           CopyDB(getBaseContext().getAssets().open("md_book.db"),
                   new FileOutputStream(destPath + "/md_book.db"));
       }
   } catch (IOException e1) {
       e1.printStackTrace();
   }
}

   @Override
   protected void onResume() {
   super.onResume();
   if (!favorite.isEmpty()){
       favorite.clear();
       selectFavorite();
   } else if (!list.isEmpty()){
       list.clear();
       selectList();
   }
}

private void CopyDB(InputStream inputStream, OutputStream outputStream)
       throws IOException {
   byte[] buffer = new byte[1024];
   int length;
   while ((length = inputStream.read(buffer)) > 0) {
       outputStream.write(buffer, 0, length);
   }
   inputStream.close();
   outputStream.close();
}

private void selectFavorite() {

   database = SQLiteDatabase.openOrCreateDatabase(destPath + "/md_book.db",
           null);
   Cursor cursor = database.rawQuery("SELECT * FROM main WHERE fav = 1",
           null);
   while (cursor.moveToNext()) {
       String word = cursor.getString(cursor.getColumnIndex("word"));
       String definition =
               cursor.getString(cursor.getColumnIndex("definition"));
       String trans = cursor.getString(cursor.getColumnIndex("trans"));
       String img = cursor.getString(cursor.getColumnIndex("img"));
       int id = cursor.getInt(cursor.getColumnIndex("id"));

       Structure struct = new Structure(word, definition, trans, img, id);
       struct.setWord(word);
       struct.setDefinition(definition);
       struct.setTrans(trans);
       struct.setImg(img);
       struct.setId(id);
       favorite.add(struct);
   }

}

   private void selectList() {

   database = SQLiteDatabase.openOrCreateDatabase(destPath + "/md_book.db",
           null);
   Cursor cursor = database.rawQuery("SELECT * FROM main", null);
   while (cursor.moveToNext()) {
       String word = cursor.getString(cursor.getColumnIndex("word"));
       String definition =
               cursor.getString(cursor.getColumnIndex("definition"));
       String trans = cursor.getString(cursor.getColumnIndex("trans"));
       String img = cursor.getString(cursor.getColumnIndex("img"));
       int id = cursor.getInt(cursor.getColumnIndex("id"));

       Structure struct = new Structure(word, definition, trans, img, id);
       struct.setWord(word);
       struct.setDefinition(definition);
       struct.setTrans(trans);
       struct.setImg(img);
       struct.setId(id);
       list.add(struct);
   }

}

}
پاسخ
 سپاس شده توسط شماره مجازی امارات ، تلگرام ضد فیلتر 2023


پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان