Problem: I cannot pass the rating value/data from the rating bar to another intent. It only shows me this instead of the value/data from the rating bar.
Rating: MainActivity#79b604
I think the error is that it did not properly get the value from the rating bar or it did not pass the value correctly. Here is my code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
String rating_float = "0.0";
private TextView txtRatingValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
rating_float = String.valueOf(rating);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Context context = getApplicationContext();
CharSequence text = "";
Intent intent_rating = new Intent(this, SecondActivity.class);
intent_rating.putExtra("rating_float", toString());
startActivity(intent_rating);
return super.onOptionsItemSelected(item);
}}
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
private TextView Msg;
String PassedValue = null;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
PassedValue = getIntent().getStringExtra("rating_float");
Msg = (TextView) findViewById(R.id.Msg);
Msg.setText("Dimension: 479\nFormat: JPEG\nSize: 360x360\nRating: " + PassedValue);
}
Questions:
Am I doing the right way on getting the value for the rating bar?
How to pass the value/data from the rating bar into the next activity/intent and how to get it from the SecondActivity.java?
That is because you pass the result of MainActivity.toString() to your second activity
Instead of
intent_rating.putExtra("rating_float", toString());
you should presumably write
intent_rating.putExtra("rating_float", this.rating_float);
You are receiving it well but not sending what you have to.
Just use this:
intent_rating.putExtra("rating_float", rating_float);
First you need to save your value into the putExtra of the intent you use to go to the another activity
intent_rating.putExtra("rating_float", rating_float);
to get the value in the SecondActivity you just need to do this wherever you need the value.
Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
String rating_value =(String) b.get("rating_float");
}
Simple and Clear Solution
//1stActivity
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
float rating = rate.getRating();
Intent intent = new Intent(ContactActivity.this,RatingIntentSOF.class);
Bundle bundle = new Bundle();
bundle.putFloat("totalRating",rating);
intent.putExtras(bundle);
startActivity(intent);
//2nd Activity, get the data of 1st Activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating_intent_sof);
TextView textView = (TextView) findViewById(R.id.text);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
float totalRating = bundle.getFloat("totalRating");
textView.setText(String.valueOf(totalRating));
}
Related
I need to pass data from Activity "MainCalendar" to fragment "AddTaskFragment" in Activity "Add". I have a button in a fragment which opens an Activity "MainCalendar", where I select a date in the calendar, and then I need to send this date into a fragment.
When I click on Choose Button, there is opens a MainCalendar, where user need to choose date, after that, the activity closed, and than I want to put a date from MainCalendar to the text of "Choose Button".
Add Activity with a fragment,
MainCalendar Activity:MainCalendar
MainCalendar.class:
public class MainCalendar extends AppCompatActivity {
private CalendarView Calendar;
private String date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_calendar);
Calendar = (CalendarView) findViewById(R.id.calendarView);
Calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
#Override
public void onSelectedDayChange(#NonNull CalendarView view, int year, int month, int dayOfMonth) {
date = (dayOfMonth+"/"+month+"/"+year);
System.out.println(date);
Intent intent = new Intent(MainCalendar.this, Add.class);
intent.putExtra("DATE_KEY", date);
finish();
}
});
}
Add.class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
bottomNav.setSelectedItemId(R.id.add);
Button notes_button = findViewById(R.id.notes_btn);
Button tasks_button = findViewById(R.id.tasks_btn);
Button goals_button = findViewById(R.id.goals_btn);
RadioButton rb = findViewById(R.id.choose_day_btn);
String date = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
//The key argument here must match that used in the other activity
date = extras.getString("DATE_KEY");
}
AddTasksFragment frag = new AddTasksFragment();
Bundle bundle = new Bundle();
bundle.putString("DATE_KEY", date);
frag.setArguments(bundle);
Fragment fragment1 = new AddTasksFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,fragment1).commit();
bottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.notes:
startActivity(new Intent(getApplicationContext(), Notes.class));
overridePendingTransition(0,0);
return true;
case R.id.add:
return true;
case R.id.tasks:
startActivity(new Intent(getApplicationContext(), Tasks.class));
overridePendingTransition(0,0);
return true;
case R.id.goals:
startActivity(new Intent(getApplicationContext(), Goals.class));
overridePendingTransition(0,0);
return true;
case R.id.statistics:
startActivity(new Intent(getApplicationContext(), Statistics.class));
overridePendingTransition(0,0);
return true;
}
return false;
}
});
Fragment fragment = new AddTasksFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,fragment).commit();
tasks_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new AddTasksFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,fragment).commit();
}
});
notes_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new AddNotesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,fragment).commit();
}
});
goals_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new AddGoalsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,fragment).commit();
}
});
//
AddTaskFragment.class, which is in Add.class:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.add_tasks_fragment, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
subtask_btn = (Button) view.findViewById(R.id.add_subtask_btn);
subtask_name = (EditText) view.findViewById(R.id.subtask_name);
task_name = (EditText) view.findViewById(R.id.task_name);
lin_lay = (LinearLayout) view.findViewById(R.id.linear_layout);
sb_lay = (LinearLayout) view.findViewById(R.id.subtask_lay);
apply_btn = (Button) view.findViewById(R.id.apply_btn);
rgDay = (RadioGroup) view.findViewById(R.id.date_group);
rgPriority = (RadioGroup) view.findViewById(R.id.priority_group);
todayBtn = (RadioButton) view.findViewById(R.id.today_btn);
tomorrowBtn = (RadioButton) view.findViewById(R.id.tomorrow_btn);
chooseDayBtn = (RadioButton) view.findViewById(R.id.choose_day_btn);
lowPrBtn = (RadioButton) view.findViewById(R.id.low_btn);
mediumPrBtn = (RadioButton) view.findViewById(R.id.medium_btn);
highPrBtn = (RadioButton) view.findViewById(R.id.high_btn);
firebaseAuth = FirebaseAuth.getInstance();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
dataBase = FirebaseDatabase.getInstance().getReference(TASKS_KEY);
this.getResources().getDisplayMetrics();
subtask_btn.setOnClickListener(this);
apply_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
writeTask();
}
});
chooseDayBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), MainCalendar.class);
startActivity(intent);
}
});
String data;
data = getActivity().getIntent().getStringExtra(DATE_KEY);
}
#Override
public void onClick(View v) {
addView();
}
private void writeTask() {
String Task;
Task = task_name.getText().toString();
dataBase.push().setValue(Task);
String[] subTasks = new String[sb_lay.getChildCount()];
for (int i = 0; i < sb_lay.getChildCount(); i++) {
View subtaskView = sb_lay.getChildAt(i);
EditText editTextName = (EditText) subtaskView.findViewById(R.id.subtask_name);
subTasks[i] = editTextName.getText().toString();
dataBase.child(Task).child("Subtasks").push().setValue(subTasks[i]);
}
dataBase.child(Task).push().setValue(getDay());
}
private String getDay(){
String day=null;
if(todayBtn.isChecked()){
Date currentTime = Calendar.getInstance().getTime();
day = currentTime.toString();
}
else if(tomorrowBtn.isChecked()){
Date dt = new Date();
Calendar c = Calendar.getInstance();
c.setTime(dt);
c.add(Calendar.DATE, 1);
dt = c.getTime();
day = dt.toString();
}else if(chooseDayBtn.isChecked()) {
day = getArguments().getString("DATE_KEY");
System.out.println(" Date is choosed ----------------------------------------------------------------------------" + day);
chooseDayBtn.setText(day);
}
return day;
}
private void addView(){
final View subtaskView = getLayoutInflater().inflate(R.layout.subtask_raw, null, false);
EditText editText = (EditText)subtaskView.findViewById(R.id.subtask_name);
ImageView imageClose = (ImageView)subtaskView.findViewById(R.id.remove_subtask);
imageClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeView(subtaskView);
}
});
sb_lay.addView(subtaskView);
String day;
day = getArguments().getString("DATE_KEY");
System.out.println(" Date is choosed " + day);
}
private void removeView(View view){
sb_lay.removeView(view);
}
As I understood, you Have to activities (Activity A and Activity B) and you have a fragment in Activity B. What you want to do is sending some data from activity A to that fragment for whatever reason.
Let's have a note first, You can't directly send data to any fragment of activity without launching it. So in order to send this data to the fragment you have to pass it from Activity A to Activity B as a first step and then send it from Activity B to the Fragment as a second step.
The type of data is very important. If you are sending a custom type (a model java class) you have to define it as Parcelable or if it is a primitive type such as int or float so you can send it directly
this is how to do it in Java
ActivityA.java
class ActivityA extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityA);
// define your data here
int data = 5;
// define an intent to send the data to the second activity
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("DATA_KEY", data);
startActivity(intent);
}
}
Then what we need to do is
receiving the data in the Activity B
Inflating the fragment
pass the data to the fragment
class ActivityB extends Activity{
Int data;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityB);
Bundle extras = getIntent().getExtras();
if (extras != null) {
//The key argument here must match that used in the other activity
data = extras.getInt("DATA_KEY");
}
// Fragment instance
FragmentClass frag = new FragmentClass();
Bundle bundle = new Bundle();
bundle.putInt("DATA_KEY", data );
frag.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);
fragmentTransaction.commit();
}
}
and then receive it the same way we received it in the Activity B
I am attempting to pass an object from one activity to another. I have tried using just intent and how with bundle but I am not sure what is wrong. I have looked at similar solutions here and that is where I got most of my code for this, but it seems that my copy and paste does not work.
This is my main class
public class MainActivity extends AppCompatActivity {
Item item;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNewItem();
Button buttonOne = findViewById(R.id.itemButton);
buttonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ViewItemDetails.class);
Bundle bundle = new Bundle();
bundle.putSerializable("item", item);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
void createNewItem(){
item=new Item("Pixel 4","https://google.com",1000.00);
}
}
This is the activity I am trying to go to:
public class ViewItemDetails extends AppCompatActivity {
Intent intent= getIntent();
Bundle bundle= intent.getExtras();
Item item = (Item) bundle.getSerializable("item");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_item_details);
setStrings();
setButtons();
}
void setStrings() {
try {
TextView nameTextView = findViewById(R.id.itemNameid);
nameTextView.setText(item.getItemName());
TextView itemInitTV = findViewById(R.id.initalPriceNumID);
itemInitTV.setText(Double.toString(item.getInitPrice()));
TextView itemCurrTV = findViewById(R.id.currentPriceNumid);
itemCurrTV.setText(Double.toString(item.getCurrentPrice()));
}catch (NullPointerException e){
//do noting
}
}
void setButtons(){
Button buyButton = findViewById(R.id.buyButtonid);
buyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri uriUrl = Uri.parse(item.getWebaddress());
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
});
Button refreshButton= findViewById(R.id.refrechId);
refreshButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Double newPrice= new GenerateNewPrice().GenerateNewPrice(item.getWebaddress());
Toast toast = Toast.makeText(getApplicationContext(),Double.toString(newPrice), Toast.LENGTH_SHORT);
toast.show();
}
});
Button editButton= findViewById(R.id.editid);
editButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast toast = Toast.makeText(getApplicationContext(),"Attempt to edit", Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
This is the object I am attempting to pass between activities.
public class Item implements Serializable {
String itemName, webaddress;
Double initPrice, CurrentPrice;
public Item(String itemName, String webaddress, Double initPrice) {
this.itemName = itemName;
this.webaddress = webaddress;
this.initPrice = initPrice;
}
public String getItemName() {
return itemName;
}
public String getWebaddress() {
return webaddress;
}
public Double getInitPrice() {
return initPrice;
}
public Double getCurrentPrice() {
return CurrentPrice;
}
}
When I run the app on my phone I click the button and then the app closes.
Thank you for your help. If needed I can add more code. I have seen similar questions here, but they have not worked for me. I got similar code from those posts but have not solved my solution.
I appreciate any feedback that is give.
Thank you for your time.
For now and for future help on SOF, remember, Error logs are always helpful in that kind of scenario.
Though, Here are some points..
You should follow the Activity lifecycle rule, Getting the data in onCreate() will be a good idea.
You should use Parcelable instead of Serializable. It is much more efficient.
your initialisation of bundle and item is wrong in the ViewItemDetails.java activity. Try to initialise inside the onCreate method and let us know..
Seems like you have some bug, it should work, you can check out one of the project work example
//creating the budle
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//String selectedItem = (String) adapterView.getItemAtPosition(i);
//getting dat from text view and converting to string
// String textview =((TextView)view.findViewById(R.id.textViewName)).getText().toString();
//Toast.makeText(DriverInfo.this,"Driver id "+driver_id,Toast.LENGTH_SHORT).show();
Bundle bundle=new Bundle();
bundle.putString("DriverID",driver_id);
bundle.putString("Route",loc_src);
bundle.putString("Dest",loc_dest);
bundle.putString("location_address",location_address);
Toast.makeText(DriverInfo.this, "Driver ID="+driver_id, Toast.LENGTH_SHORT).show();
Intent intent=new Intent(DriverInfo.this, DirverCurrentLoc.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
//Getting the bundle data
String lat, lng, realtime,src,dest;
location_address=intent.getStringExtra("location_address");
driver_id=intent.getStringExtra("DriverID");
src=intent.getStringExtra("Route");
dest=intent.getStringExtra("Dest");
You have to initialize this code in onCreate() method like below :
Item item;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_item_details);
Intent intent= getIntent();
Bundle bundle= intent.getExtras();
item = (Item) bundle.getSerializable("item");
setStrings();
setButtons();
}
You can use GSON to convert your Object into a JSON format
Intent intent = new Intent(this, ProjectActivity.class);
intent.putExtra("item", new Gson().toJson(item));
startActivity(intent);
then in your second activity, make sure you initialize the bundle correctly in onCreate() lifecycle.
Item item;
#Override
protected void onCreate(Bundle savedInstanceState) {
.........
Bundle extras = getIntent().getExtras();
if (extras != null) {
item = new Gson().fromJson(extras.getString("item"), Item.class);
}
Note: I read this in an article that google recommends GSON when passing objects on bundle.
The code below is intended to pass the data, using bundle, collected on signing up in Signup.java to ViewProfile.java which displays the data. On checking for a key in the bundle, it returns true, however, the bundle is null when checked in ViewProfile.java. Help will b appreciated.
Signup.java
public class Signup extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
final EditText n=(EditText)findViewById(R.id.editText3);
final EditText u=(EditText)findViewById(R.id.editText4);
final EditText p=(EditText)findViewById(R.id.editText5);
final EditText c=(EditText)findViewById(R.id.editText6);
Button s=(Button)findViewById(R.id.button4);
final Userdatabase udb=new Userdatabase(this);
s.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String name=n.getText().toString();
String email=u.getText().toString();
String password=p.getText().toString();
String phone=c.getText().toString();
boolean b=udb.insertuser(name,email,password);
if(b==true) {
Intent i = new Intent(Signup.this, MainActivity.class);
Bundle bundle=new Bundle();
bundle.putString("NAME",name);
bundle.putString("ID",email);
bundle.putString("PHONE",phone);
i.putExtras(bundle);
startActivity(i);
}
else
Toast.makeText(getApplicationContext(),"Please try again",Toast.LENGTH_SHORT).show();
}
});
}
}
ViewProfile.java
public class ViewProfile extends AppCompatActivity {
String name,username,contact,profession;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_profile);
Intent intent=getIntent();
Bundle b=intent.getExtras();
if(b!=null) {
name = b.getString("NAME");
username = b.getString("ID");
contact = b.getString("PHONE");
TextView tv1=(TextView)findViewById(R.id.textView17);
TextView tv2=(TextView)findViewById(R.id.textView18);
TextView tv3=(TextView)findViewById(R.id.textView19);
tv1.setText(name);
tv2.setText(username);
tv3.setText(contact);
}
ImageView img=(ImageView)findViewById(R.id.imageView4);
img.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent i=new Intent(ViewProfile.this,Profile.class);
startActivity(i);
}
});
}
}
You are calling the wrong activity in the SignUp activity:
if(b==true) {
Intent i = new Intent(Signup.this, MainActivity.class);//problem here
You have set the intent as MainActivity.class and sending data to MainActivity and not to ViewProfile activity.
Change to this in SignUp activity:
if(b==true) {
Intent i = new Intent(Signup.this, ViewProfile.class);
In my app I have three fragments. In the third fragment, a variable is take from a seekBar. Now I want to use this variable in my MainActivity. I tried to send the variable with an intent and show it in a textView onClick to test it, but the textView only shows „null“. Why isn‘t the variable send to the activity?
My MainActivity:
public class MainActivity extends AppCompatActivity {
TextView textTest;
public int a = 33;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTest = (TextView) findViewById(R.id.textView3);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = getIntent();
String messages = intent.getStringExtra("message");
textTest.setText(String.valueOf(messages));
}
});
}
}
My Fragment that sends the variable:
public class ItemThreeFragment extends Fragment {
SeekBar seekBar;
TextView textView11;
int value = 10;
public static ItemThreeFragment newInstance() {
ItemThreeFragment fragment = new ItemThreeFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_three, container, false);
seekBar = (SeekBar) view.findViewById(R.id.seekBar);
seekBar.setMax(25);
seekBar.setProgress(value);
textView11 = (TextView) view.findViewById(R.id.textView11);
textView11.setText("" + value);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
value = i;
textView11.setText("" + value);
Intent intent = new Intent(getActivity().getBaseContext(),
MainActivity.class);
intent.putExtra("message", value);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
})
return view;
}
}
The problems are
In your activity, Intent intent = getIntent(); will get the intent that starts MainActivity.
In your fragment's onProgressChanged, your code doesn't communicate with MainActivity at all.
I can think of two relatively simple solutions for now:
Call a MainActivity function using ((MainActivity) getActivity()).someFunction() in your fragment.
In the MainActivity, use ((YourFragment) getSupportFragmentManager().findFragmentById(R.id.your_fragment_id)).property to access the fragment object's content. You can put the seek bar value into a class variable.
And check this: Communicating between a fragment and an activity - best practices
getActivity().findViewById(R.id.textView3).setText(""+value);
put the code inside onProgressChanged(),I hope its help you.
I think you missed startActivity(intent).
Intent intent = new Intent(getActivity().getBaseContext(),
MainActivity.class);
intent.putExtra("message", value);
getActivity().startActivity(intent);
So there is 4 buttons on this page... 3 of them work fine, the 4th button, which links to ToolsTableLayout.class does not respond at all. There are no erros, the app does not crash or anything... you just click the button and nothing happens.
Code for the button class:
public class MainMenu extends Activity implements OnClickListener{
private String result;
boolean isScanout;
public static final String SCAN_RESULT = "MyPreferencesFile";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
Button ScanOut=(Button)findViewById(R.id.scanout);
ScanOut.setOnClickListener(this);
Button ScanIn=(Button)findViewById(R.id.scanin);
ScanIn.setOnClickListener(this);
Button EndSession = (Button) findViewById(R.id.endsession);
EndSession.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.scanout){
isScanout = true;
IntentIntegrator.initiateScan(this);
}
else if(v.getId()==R.id.scanin){
isScanout = false;
IntentIntegrator.initiateScan(this);
}
else if(v.getId()==R.id.endsession){
Intent endsessionintent = new Intent(MainMenu.this, MainActivity.class);
startActivity(endsessionintent);
}
else if(v.getId()==R.id.toolDB){
Intent i = new Intent(MainMenu.this, ToolsTableLayout.class);
startActivity(i);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null) {
String qrCode = scanResult.getContents();
SharedPreferences codeHack = getSharedPreferences(SCAN_RESULT,0);
SharedPreferences.Editor editor = codeHack.edit();
editor.putString("entry", qrCode);
editor.commit();
}
}
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
R.id.toolDB is the button that makes no response..
and here is the ToolsTableLayout.java class (which does not open):
public class ToolsTableLayout extends Activity {
public static final String SCAN_RESULT = "MyPreferencesFile";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablelayout2);
SharedPreferences codeHack = getSharedPreferences(SCAN_RESULT,0);
String QRcode = codeHack.getString("entry", "unregistered");
StringTokenizer token = new StringTokenizer(QRcode," ");
String name = token.nextToken();
String quantity = token.nextToken();
TextView t1 = (TextView) findViewById(R.id.slot1b);
TextView t2 = (TextView) findViewById(R.id.slot2b);
TextView t3 = (TextView) findViewById(R.id.slot3b);
ToolDB info = new ToolDB(this);
info.open();
String c1 = info.getRowID();
info.createEntry(name, quantity);
info.close();
t1.setGravity(Gravity.CENTER_HORIZONTAL);
t2.setGravity(Gravity.CENTER_HORIZONTAL);
t3.setGravity(Gravity.CENTER_HORIZONTAL);
t1.setText(c1);
t2.setText(name);
t3.setText(quantity);
}
}
not sure if the XML files are needed? if so let me know and I will update.
You did not set onClickListener for 4th button. Please add
Button btnToolDb = (Button) findViewById(R.id.toolDB);
btnToolDb.setOnClickListener(this);
Judging by the code you have provided, I believe you have forgotten to create and attach the ClickListener to the fourth button which is missing from the code.