0

I want to pass texts from here Information.class to tab1 or 2 or 3 =====> Tablayout/ViewPager and I want every tab to have his own data example: id 1 data for tab1 id 2 data for tab2 .......

this my sqlite class:

public class DataBaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "big.db";
public static final String TABLE_NAME = "images";
public static final String NAME = "name";
public static final String PLACE = "place";

public DataBaseHelper(Context context) {

    super(context, DATABASE_NAME, null, 1);
    SQLiteDatabase db = this.getWritableDatabase();

}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_IMAGES_TABLE = "CREATE TABLE images ( " +
            "ID" +"INTEGER PRIMARY KEY AUTOINCREMENT, " +

            "name TEXT, " +
            "place TEXT )";

    db.execSQL(CREATE_IMAGES_TABLE);
}


@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP TABLE IF EXISTS images");
    this.onCreate(db);
}

public void insertentry(String name, String place) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(NAME, name);
    contentValues.put(PLACE, place);

    db.insert(TABLE_NAME, null, contentValues);
}



}

Tab1.class

public class Tab1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tab1, container, false);

    ImageView imageView = (ImageView) v.findViewById(R.id.img1);
    String photoPath = Environment.getExternalStorageDirectory() + "/Download/image1.jpg";
    Bitmap bitmap1 = BitmapFactory.decodeFile(photoPath);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    final Bitmap b = BitmapFactory.decodeFile(photoPath, options);

    imageView.setImageBitmap(b);

    return v;

}

Information.class

public class Information extends AppCompatActivity {

EditText text1,text2 ;
Button btn;
DataBaseHelper dataBaseHelper;

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





    text1 = (EditText) findViewById(R.id.txt1);
    text2 = (EditText) findViewById(R.id.txt2);
    ImageView oo= (ImageView)findViewById(R.id.imageView99);

    btn = (Button) findViewById(R.id.btn1);
    Log.d("CLick", "123 INFO SAVED");
    addData();

    dataBaseHelper = new DataBaseHelper(this);


    Bundle extras = getIntent().getExtras();
    Bitmap bmp = (Bitmap) extras.getParcelable("Bitmap");
    oo.setImageBitmap(bmp);

}


public void addData(){
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dataBaseHelper.insertentry(text1.getText().toString(),text2.getText().toString());
            Toast.makeText(getApplicationContext(),"doneeeee",
                    Toast.LENGTH_LONG).show();

}

my tablayout/viewpager class that contains tab1-2-3;

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




            tabLayout = (TabLayout) findViewById(R.id.tablayout1);
            tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
            tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
            tabLayout.addTab(tabLayout.newTab().setText("Tab3"));


            tabLayout.setTabGravity(tabLayout.GRAVITY_FILL);


            viewPager = (ViewPager) findViewById(R.id.viewpager1);
            Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());

            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
            viewPager.setAdapter(adapter);
            tabLayout.setOnTabSelectedListener(this);
            viewPager.getCurrentItem();
            viewPager.setOffscreenPageLimit(3);


            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab1);
            fab.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    int position = tabLayout.getSelectedTabPosition();
                    switch (position) {

                        case 0:
                            File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                            String pictureName = "image1" + ".jpg";
                            //  imageName(pictureName);
                            File imageFile = new File(file, pictureName);
                            Uri pictureUri = Uri.fromFile(imageFile);

                            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            i.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
                            startActivityForResult(i, 0);

                            break;

                        case 1:
                            File file2 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                            String pictureName2 = "image2" + ".jpg";
                            File imageFile2 = new File(file2, pictureName2);
                            Uri pictureUri2 = Uri.fromFile(imageFile2);
                            Intent ii = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            ii.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri2);
                            startActivityForResult(ii, 1);
                            break;

                        case 2:
                            File file3 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                            String pictureName3 = "image3" + ".jpg";
                            File imageFile3 = new File(file3, pictureName3);
                            Uri pictureUri3 = Uri.fromFile(imageFile3);
                            Intent iii = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            iii.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri3);
                            startActivityForResult(iii, 2);
                            break;
                    }
                }
            });
    }


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    imageView1 = (ImageView) findViewById(R.id.img1);
    imageView2 = (ImageView) findViewById(R.id.img2);
    imageView3 = (ImageView) findViewById(R.id.img3);


    if (requestCode == 0 && resultCode == RESULT_OK) {

        String photoPath = Environment.getExternalStorageDirectory() + "/Download/image1.jpg";
        galleryAddPic(photoPath);
        Bitmap bitmap1 = BitmapFactory.decodeFile(photoPath);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        final Bitmap b = BitmapFactory.decodeFile(photoPath, options);

        imageView1.setImageBitmap(b);
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView1.buildDrawingCache();
                Bitmap image = imageView1.getDrawingCache();

                Bundle extras = new Bundle();
                Intent o = new Intent(Layout1.this, Information.class);
                extras.putParcelable("Bitmap", image);
                o.putExtras(extras);

                startActivity(o);

            }
        });


    } else if (requestCode == 1 && resultCode == RESULT_OK) {

        String photoPath = Environment.getExternalStorageDirectory() + "/Download/image2.jpg";
        galleryAddPic(photoPath);

        Bitmap bitmap2 = BitmapFactory.decodeFile(photoPath);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        final Bitmap b2 = BitmapFactory.decodeFile(photoPath, options);
        imageView2.setImageBitmap(b2);
        imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView2.buildDrawingCache();
                Bitmap image = imageView2.getDrawingCache();

                Bundle extras = new Bundle();
                Intent o = new Intent(Layout1.this, Information.class);
                extras.putParcelable("Bitmap", image);
                o.putExtras(extras);
                startActivity(o);

            }
        });


    } else if (requestCode == 2 && resultCode == RESULT_OK) {


        String photoPath = Environment.getExternalStorageDirectory() + "/Download/image3.jpg";
        galleryAddPic(photoPath);
        Bitmap bitmap3 = BitmapFactory.decodeFile(photoPath);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        final Bitmap b3 = BitmapFactory.decodeFile(photoPath, options);
        imageView3.setImageBitmap(b3);
        imageView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView3.buildDrawingCache();
                Bitmap image = imageView3.getDrawingCache();

                Bundle extras = new Bundle();
                Intent o = new Intent(Layout1.this, Information.class);
                extras.putParcelable("Bitmap", image);
                o.putExtras(extras);
                startActivity(o);

            }
        });


    }

}
private void galleryAddPic(String photoPath) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(photoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}





@Override
public void onTabSelected(TabLayout.Tab tab) {


    viewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnselected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabReselected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());

}
  • before posting the question please tell what you have tried so far – M.Yogeshwaran Sep 08 '16 at 10:36
  • i tried intent a = new intent(....this,tab1.class) a.putExtra("aaa",editText.getText().toString()); startActivity(a); then recall from tab1 or tablayout 1 b.setText(getInent().getExtras().getString("aaa"); but i cant put TextView and display on it so this code not working even if i use TextView a= (TextView ) v.findViewById(R.id.img1); .getExtras() Is the problem –  Sep 08 '16 at 10:40
  • http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android take a look at it i might help you – M.Yogeshwaran Sep 08 '16 at 10:41
  • tried this one too and i want every tab its own info(text) –  Sep 08 '16 at 10:44
  • own info means please post your question clearly – M.Yogeshwaran Sep 08 '16 at 10:45
  • my app is about taking picture then put it in sqlite database with 2 texts then recall it so if someone opens the app can see the pic in everytab with info and can change it with retake new camera picture then save new info on it so it change sry for my english –  Sep 08 '16 at 10:47
  • when i press save button i want to return to tab1 with displaying Name,Place on just on Tab1 on Tab2 will be different Name,Place and if close the app and reopen it Name and place of everypictures stays until i change it again from infromation.class this is my problem –  Sep 08 '16 at 11:07
  • some one can help me here http://stackoverflow.com/questions/39406081/how-to-display-every-tabfragment-its-own-data-in-textview-from-sqlite –  Sep 09 '16 at 13:05

1 Answers1

0

I don't understand your concern that why you want to transfer sqlite data from activity to fragment, while you can get data from sqlite database in fragment. But if you want to transfer data from activity then use parcelable class and send object of that class with bundle, get values from that object in fragment.

Atiqul Alam
  • 11
  • 1
  • 4
  • my app is about taking picture then put it in sqlite database with 2 texts then recall it so if someone opens the app can see the pic in everytab with info and can change it with retake new camera picture then save new info on it so it change sry for my english –  Sep 08 '16 at 10:46
  • i am putting the information and saving it in sqlite i dont know how display it on tab1 –  Sep 08 '16 at 10:52
  • If you want to transfer data from activity then you need to understand communication between activity and fragment. see this link- http://techblogon.com/communication-between-activity-and-fragment-example/ – Atiqul Alam Sep 08 '16 at 10:53
  • i want to display the things that i write in infromation.class which safes it in sql into tab1 let me update my issue and add new class –  Sep 08 '16 at 10:59
  • when i press save button i want to return to tab1 with displaying Name,Place on just on Tab1 on Tab2 will be different Name,Place and if close the app and reopen it Name and place of everypictures stays until i change it again from infromation.class this is my problem –  Sep 08 '16 at 11:08
  • some one can help me here http://stackoverflow.com/questions/39406081/how-to-display-every-tabfragment-its-own-data-in-textview-from-sqlite –  Sep 09 '16 at 13:05