Call Image from Gallery/ImageButton to Canvas (DrawingView) - android

can anyone help me to solve this problem, how to call image from gallery/imagebutton to canvas for drawing?
this is my snippet code :
buah1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(), BelajarMewarnai.class);
startActivity(intent);
}
});
If anyone need a project I will send my project to email.

Actually i wanted to post the link in the comment for u but u seemed to bit less fimiliar with android(i thinking so), So i am posting my code to do that. First choose on which button click u want the user to be guided to gallery(only default gallery and not anything else, u may get a null pointer if u choose pic from any other).
On that button click u do this:
private void onClickOfButton(View v){
Intent galleryIntent=new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(Intent.createChooser(galleryIntent, "pic-select"), 10);//(10 its just a request code, u can give ur own, but same should there at receiving end )
}
Since u have started an activity for result, so u should be awaiting for a result from that started activity, so that activity will bring u the image and in ur activity u just collect it and put it into ur image view like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==10 && resultCode==Activity.RESULT_OK){
try{
Uri selectImageUri=data.getData();
String selectedImagePath=getPath(selectImageUri);
Bitmap pic=BitmapFactory.decodeFile(selectedImagePath);
if(pic!=null){
yourImageView.setImageBitmap(pic);
}
}catch(NullPointerException ex){
Toast.makeText(getActivity().getBaseContext(), "Go to default gallery area and choose a pic", Toast.LENGTH_LONG).show();
}
}
else
super.onActivityResult(requestCode, resultCode, data);
}
The getPath method:
private String getPath(Uri uri) {
if( uri == null ) {
return null;
}
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}
So this will do ur job...

try this:
buah1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(), BelajarMewarnai.class);
Bundle bundle = new Bundle();
//Get the image name you clicked on
String imageName = getResources().getResourceName(imageId);;
//Send the image name as a string, which you want to load in the other activity
bundle.putString("image_to_load", imageName.split("/")[1].toString());
intent.putExtras(bundle);
startActivity(intent);
}
});
then you can get your image name in the next activity in onCreate like this:
//Here you can get the image name you already clicked in the previous activity to load in your canvas
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String image_name = bundle.getString("image_to_load")
}
then you can set the image name to your imageView like this:
//Here you can get all resources in res folder
Resources res = getResources();
//define a new string which takes the image name you get from extras in the above code
String mDrawableName = image_name;
//Now you have the image name you clicked in theprevious activity, and ResID is to get the resource id from its name
int resID = res.getIdentifier(mDrawableName, "drawable", getPackageName());
//Here you can get the drawable from the resourceID you obtained in the above line
Drawable drawable = res.getDrawable(resID );
YOUR_IMAGE is the ImageView of the canvas you want to load the image inside in order to draw or whatever you want to do
drawView.setBackgroundDrawable(drawable);
I hope this will help..

Related

problem i'm getting (random order) after select images from gallery

after select multi images from gallery on order selection the images goes to the recycle view on random order not as I select on specific order or sort. for example if I select B then A then C but the code always bring them with random sort or order like A C B and sometimes C A B. . the order seems undefined at first I thought android sort them based on date and time added to the gallery or what their name i mean alphabet letter but then I realize the order seems undefined. i'm not sure if I need to add listener with array track the user selections. any suggestion
ArrayList imagesUriArrayList;
public static final int slect_photo = 100;
Bitmap bitmap1 = null;
RecyclerView rec;
LinearLayoutManager lnm;
Adopter_Browse list;
ArrayList<MyCard> mylist;
public void Onclick_View(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, slect_photo);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (data.getData() != null) {//data.getData() != null) {
imagesUriArrayList = null;
imagesUriArrayList = new ArrayList();
try{
for (int i = 0; i < data.getClipData().getItemCount(); i++) {
imagesUriArrayList.add(data.getClipData().getItemAt(i).getUri());
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
}
try {
for (int i = 0; i < imagesUriArrayList.size(); i++) {
bitmap1 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), (Uri) imagesUriArrayList.get(i));
mylist.add(new MyCard(count_s, bitmap1));
}
} catch (IOException e) {
e.printStackTrace();
}
lnm = new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false);
list = new Adopter_Browse(mylist);
rec.setLayoutManager(lnm);
list.notifyDataSetChanged();
rec.setAdapter(list);
}
I found out that using onActivityResult that provided by google will always return the order of your selection on miss , so maybe a bug or need improve .
I got two option :
1- using ItemTouchHelper library ... helped me to solve my problem !
2- gradle source form githup !
I share this maybe some one can get relief !

Android take photo and send it as an attachment to an email, imageview reset on rotation

I need a little or much help here.
I created a fragment that takes a photo and passes it as an attachment on the email that I send.
The fragment works but it has some bugs, please take a look:
I tried almost every code and example to take a photo and manage to put it on an imageview but in vain it does not work, because I have a samsung mobile and samsung mobiles work different. So I copied onActivityResult method from another guy's tutorial.(If there is a simpler please way be my guest)
The application crashes when I try to send the email for a second time. The first time when I press the send button it sends the email, but when I press it again, it crashes.
For a weird reason the email works only with the gmail application that I have installed on my samsung. I have another mail client (the default) but when I choose it, the app crashes.
I use onsavedinstansestate but when rotate the device, again, the app crashes.
I believe that my questions 2 and 4 are a matter of the saveInstancestate because if on every refresh of the fragment, everything is reset (imageviews etc).
Here I write my contact fragment, kindly take a look
public class contact extends Fragment {
private String dataImported;
private TextView txt;
private ImageView imgThumbNail;
private Bitmap bmap;
String mCurrentPhotoPath;
private static final int CAMERA_IMAGE_CAPTURE = 0;
private static final int EMAIL_SEND = 1;
private Uri uriThumbnailImage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//Save the thumbnail
if (uriThumbnailImage != null){
imgThumbNail.buildDrawingCache();
bmap = imgThumbNail.getDrawingCache();
outState.putParcelable("savedImage", bmap);
}
}
private void dispatchTakePictureIntent(){
Intent TakePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(TakePictureIntent, CAMERA_IMAGE_CAPTURE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e(getTag(), "onCreateView 3");
txt = (TextView) getActivity().findViewById(R.id.txt_fragment3);
imgThumbNail = (ImageView) getActivity().findViewById(R.id.imageThumbnail);
if (container == null){
return null;
}
return inflater.inflate(R.layout.fragment3_layout,
container,false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
txt = (TextView) getActivity().findViewById(R.id.txt_fragment3);
Log.e(getTag(), "onActivityCreated 3");
if ((savedInstanceState != null) && (savedInstanceState.getParcelable("savedImage") != null))
{
bmap = (Bitmap) savedInstanceState.getParcelable("savedImage");
System.out.println(bmap);
imgThumbNail = (ImageView) getActivity().findViewById(R.id.imageThumbnail);
imgThumbNail.setImageBitmap(bmap);
}
//create onClickListener for the email
email();
//create onClickListener for the photo
takephoto();
if (this.dataImported == null)
txt.setText("Στείλτε μας την δικιά σας γλυκιά συνταγή!");
else
txt.setText(this.dataImported);
}
private void takephoto() {
ImageButton btnTakePhoto =(ImageButton) getActivity().findViewById(R.id.btn_takePhoto);
btnTakePhoto.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_IMAGE_CAPTURE)
{
// Describe the columns you'd like to have returned. Selecting from the Thumbnails
String[] projection = {
MediaStore.Images.Thumbnails._ID, //The columns we want
MediaStore.Images.Thumbnails.IMAGE_ID,
MediaStore.Images.Thumbnails.KIND,
MediaStore.Images.Thumbnails.DATA
};
String selection = MediaStore.Images.Thumbnails.KIND + "=" + MediaStore.Images.Thumbnails.MINI_KIND;
String sort = MediaStore.Images.Thumbnails._ID + " DESC";
#SuppressWarnings("deprecation")
Cursor myCursor = getActivity().managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, selection, null, sort);
long imageId = 01;
long thumbnailImageId = 01;
String thumbnailPath = "";
try{
myCursor.moveToFirst();
imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
}finally{myCursor.close();}
//Create new Cursor to obtain the file Path for the large image
String[] largeFileProjection = {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA
};
String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
myCursor = getActivity().managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort);
String largeImagePath = "";
try{
myCursor.moveToFirst();
//This will actually give the file path location of the image.
largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
}finally{myCursor.close();}
// These are the two URI's you'll be interested in. They give a handle to the actual images
Uri uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageId));
uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(thumbnailImageId));
//I connect image to the imageView and show it on the screen
imgThumbNail = (ImageView) getActivity().findViewById(R.id.imageThumbnail);
imgThumbNail.setImageURI(uriThumbnailImage);
}//if
}
private void email() {
final EditText onomaSintagis = (EditText) getActivity().findViewById(R.id.txt_onomaSintagis_send);
final EditText onomaPelati = (EditText) getActivity().findViewById(R.id.txt_CustomerName_send);
final EditText sintagiPelati = (EditText) getActivity().findViewById(R.id.txt_Sintagi_send);
ImageButton btnSendEmail =(ImageButton) getActivity().findViewById(R.id.btn_sendEmail);
btnSendEmail.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Send email", "");
String[] TO = {"info#urweb.eu"};
String[] BCC = {"t.itzaris#gmail.com"};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("message/rfc822");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_BCC, BCC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Αποστολή Συνταγής:"+onomaSintagis.getText()+" Από τον/την:"+onomaPelati.getText());
emailIntent.putExtra(Intent.EXTRA_TEXT, sintagiPelati.getText());
//I get the uriThumbnailImage(path of the photo) and i put it on the intent
Uri uri = Uri.parse(uriThumbnailImage.toString());
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
try{
startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."), EMAIL_SEND);
}catch(android.content.ActivityNotFoundException ex){
Toast.makeText(getActivity(), "Sorry, There is no email application installed.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Use Custom camera, it can provide a more compelling experience for your users with special features. check this
http://developer.android.com/guide/topics/media/camera.html#custom-camera
For tutorial
http://capycoding.blogspot.in/2012/06/custom-camera-application.html
and send the saved file as an attachment using path.

Control back button functionality

From the given ListView in picture, I select the contact to send sms and move to another activity (second picture). And from this sms Activity picture. When I press back hardware button available on phone, I should move where I want. I don't want a default Activity to be displayed. How can I do this?
public class SendSms extends Activity{
ListView listView;
List<RowItems> rowItems;
CustomListViewAdapter adapter;
String toNumbers = "";
String separator;
int i=0,j,count=0,count1; String a[]=new String[5];
String number,val2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new4);
listView = (ListView) findViewById(R.id.list);
rowItems = new ArrayList<RowItems>();
val2=getIntent().getStringExtra("name");
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String rawContactId = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor c = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
// ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.PHOTO_ID
}, ContactsContract.Data.RAW_CONTACT_ID + "=?", new String[] { rawContactId }, null);
if (c != null) {
if (c.moveToFirst()) {
String number = c.getString(0);
//int type = c.getInt(1);
String name = c.getString(1);
int photoId = c.getInt(2);
Bitmap bitmap = queryContactImage(photoId);
RowItems p=new RowItems(bitmap, number, name);
rowItems.add(p);
}
adapter = new CustomListViewAdapter(this,
R.layout.new5, rowItems);
listView.setAdapter(adapter);
c.close();
}
}
}
}
private Bitmap queryContactImage(int imageDataRow) {
Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] {
ContactsContract.CommonDataKinds.Photo.PHOTO
}, ContactsContract.Data._ID + "=?", new String[] {
Integer.toString(imageDataRow)
}, null);
byte[] imageBytes = null;
if (c != null) {
if (c.moveToFirst()) {
imageBytes = c.getBlob(0);
}
c.close();
}
if (imageBytes != null) {
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
} else {
return null;
}
}
public void showResult(View v) {
// String result = "";
String result1="";
for (RowItems p : adapter.getBox()) {
if (p.chck){
// result += "\n" + p.getTitle();
result1 +="\n" +p.getDesc();
a[i]=p.getTitle();
i++;
count++;
}
count1=count;
}
Toast.makeText(this, result1+"\n", Toast.LENGTH_LONG).show();
}
public void send(View v) throws IOException {
int value=a.length-count1;
for(j=0;j<a.length-value;j++){
if(android.os.Build.MANUFACTURER.equalsIgnoreCase("Samsung")){
separator = ",";
} else
separator = ";";
number = a[j];
toNumbers= toNumbers +number +separator;
}
toNumbers = toNumbers.substring(0, toNumbers.length());
Uri sendSmsTo = Uri.parse("smsto:" + toNumbers);
String smsValue = "help iam in danger..i am presently at \n"+val2;
if(val2==null){
Toast.makeText(SendSms.this, "wait.....", Toast.LENGTH_LONG).show();
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
intent.putExtra("sms_body", smsValue);
intent.setData(sendSmsTo);
startActivity(intent);
}
Use startAtctivityForResult, and in onActivityResult check the resultCode. If it is Activity.RESULT_CANCELLED, the user 'backed out' of the SMS activity. Due to the implementation of the SMS activity being outside of your control, this might not be the only case when the result code says it has been cancelled.
I think you should, when the Activity picture is displayed, try to call the onBackPressed method.
// back button pressed method
#Override
public void onBackPressed() {
super.onBackPressed();
// new intent to call an activity that you choose
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
// finish the activity picture
this.finish();
}
If you don't want to call an external Activity, you can hide the layout calling by the Intent as below:
// back button pressed method
#Override
public void onBackPressed() {
super.onBackPressed();
// your layout with the picture is displayed, hide it
if(MySecondView.getVisibility() == View.VISIBLE)
MySecondView.setVisibility(View.Gone);
// display the layout that you want
MyDefaultView.setVisibility(View.VISIBLE);
}
As I said below, try with a boolean that you initialize to false at the beginning of your Class. When you start the Intent.ACTION_VIEW, make this boolean to true:
public class Foo extends Activity {
private boolean isSms = false;
// some stuff
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
// ...
startActivity(intent);
// make your boolean to true
isSms = true;
// create the onBackPressed method
#Override
public void onBackPressed() {
// check if the user is on the sms layout
if(isSms)
// do something
else
// do something else like finish(); your activity
}
}
Or you can try to use the onKeyDown method (see here: onKeyDown (int keyCode, KeyEvent event)) as below:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0) {
// dome some stuff
return true;
}
return super.onKeyDown(keyCode, event);
}
You can see the diffence between these two methods here: onBackPressed() not working and on the website that I talk in the comments below.
Hope this will be useful.
UPDATE:
A better way should make an startActivityForResult to start the Intent.ACTION_VIEW:
startActivityForResult(intent, 1);
And then, you return the cancel code like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode != 1 && resultCode == RESULT_CANCELED) {
// do something
}
}
This is #FunkTheMonk, just above, who tells the good choice!
Add this method to the class to handle device back button.
#Override
public void onBackPressed() {
// do something
}
Please note that it requires API Level 5 or higher.
You have to overide go back behaviour of hardware back button by calling a method name onbackpressed();
#Override
public void onBackPressed() {
super.onBackPressed();
// set intent to activity where you want to move on after back press
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
// also clean the current activity from stack.
youractivityname.this.finish();
}

Call onActivityResult for contact in OnCreate() Android

I got this code from another question but I don't know how to call this onActivityResult() class in my onCreate() activity to display the first contact from my phone. Also, what does "if (requestCode == RQS_PICKCONTACT){" and "RQS_PICKCONTACT" stand for? Could someone please clarify?
public class MainActivity extends Activity {
Button buttonReadContact;
TextView textPhone;
final int RQS_PICKCONTACT = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonReadContact = (Button)findViewById(R.id.readcontact);
textPhone = (TextView)findViewById(R.id.phone);
buttonReadContact.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//Start activity to get contact
/*final Uri uriContact = ContactsContract.Contacts.CONTENT_URI;
Intent intentPickContact = new Intent(Intent.ACTION_PICK, uriContact);
startActivityForResult(intentPickContact, RQS_PICKCONTACT);
*/
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, RQS_PICKCONTACT);
}});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == RESULT_OK) {
if(requestCode == RQS_PICKCONTACT) {
Uri returnUri = data.getData();
Cursor cursor = getContentResolver().query(returnUri, null, null, null, null);
if (cursor.moveToNext()) {
int columnIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactID = cursor.getString(columnIndex_ID);
int columnIndex_HASPHONENUMBER = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
String stringHasPhoneNumber = cursor.getString(columnIndex_HASPHONENUMBER);
if(stringHasPhoneNumber.equalsIgnoreCase("1")){
Cursor cursorNum = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactID,
null,
null);
//Get the first phone number
if(cursorNum.moveToNext()){
int columnIndex_number = cursorNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String stringNumber = cursorNum.getString(columnIndex_number);
textPhone.setText("0"+stringNumber);
}
} else {
textPhone.setText("NO Phone Number");
}
} else {
Toast.makeText(getApplicationContext(), "NO data!", Toast.LENGTH_LONG).show();
}
}
}
}
When you call startActivityForResult(intent,requestCode)
onActivityResult is called when user comes back to calling activity with
requestCode
//You can start multiple activities by calling startActivityForResult so this value is to differentiate between them
resultCode
//This value is set by the called activity to indicate whether the intended operation was a success or not.
data
//this is an object of type Intent which contains data returned by called activity.
in your code when this part is executed:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, RQS_PICKCONTACT);
New activity is started and when user comes back from that activity by selecting a contact onActivityResult is called
onActivityResult is called after u startIntent or u select an contact.
RQS_PICK_CONTACT u can change as u want. like 2 , 3,4 or another number.
it just identity for requestCode in onActivityResult so u can do as u need.

Android - Passing database and id through activities

I have 2 android activity's
One where I have a room list, i.e. list of rooms.
The other the room contents. Where all the content of a selected room is displayed.
All I want is for the room id(r_id) to be passed through the room list class into the room overview class where I can retreive all its contents from the database.
I've been trying to figure out where i'm going wrong, I think its around line 94 - 101:
selectedRoom = (String) ((TextView) view).getText();
Cursor c2 = sampleDB.rawQuery("SELECT * FROM tbl_roomDesc WHERE roomName =?", new String[] {selectedRoom});
if (c2 != null ) {
if (c2.moveToFirst()) {
do {
r_id = c2.getString(c2.getColumnIndex("r_id"));
}while (c2.moveToNext());
}
}
c2.close();
Can anyone help me figure out what i'm doing wrong here?
Here is the source for the room list
Here is the source for the room overview
Thanks in advance!
To pass data between to Intents, use the putExtras() method.
Example:
Intent i = new Intent(...);
i.putExtras("roomid", "10");
startActivity(i);
final int roomId = r_id;
next = (ImageButton) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
CreateBuilding.val = "Retrieve";
sampleDB.close();
Intent intent2 = new Intent(RoomList.this, TabLayoutActivity.class);
intent2.putExtra("roomId", roomId);
setResult(RESULT_OK, intent2);
finish();
startActivityForResult(intent2, 0);
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
int roomId = data. getIntExtra("roomId", 0);
}
}
}
If I have not misunderstood you put the roomId inside the Intent and retrieve it inside the onActivityResult

Categories

Resources