How to Display Every Tab(Fragment) its own Data in TextView from SQlite

坚强是说给别人听的谎言 提交于 2019-12-20 05:13:11

问题


How I can Display Different TextView on each Tab (each tab its own Data) example: if my db is empty and begun to enter my Name _ Place Displays :Tab 1 = takes NAME_PLACE just from ID_1 , Tab 2 = ID_2 , Tab 3 = ID_3 from db then if i rentered on tab1 to change the Name_Place Tab 1= ID_1 takes just changes id 1 changes or row

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

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 static final String KEY_ID = "id";


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);
    db.close();
}
   public DbResponse getData() {
   DbResponse obj = new DbResponse();
   SQLiteDatabase db = this.getWritableDatabase();
   Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
   if (cursor.getCount() > 0) {
       cursor.moveToNext();
       obj.name = cursor.getString(cursor.getColumnIndex(NAME));
       obj.place = cursor.getString(cursor.getColumnIndex(PLACE));

   }

   cursor.close();
   return obj;
 }
   }

TAb1 class : every class has same code

   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);

            DataBaseHelper db = new DataBaseHelper(getActivity());
            DbResponse response = db.getData();
            TextView textView = (TextView)v.findViewById(R.id.txt3);
            textView.setText(response.name+" - "+response.place);

    imageView.setImageBitmap(b);

    return v;

}
}

来源:https://stackoverflow.com/questions/39406081/how-to-display-every-tabfragment-its-own-data-in-textview-from-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!