Getting the String Value to proceed to the next (specific) Activity - java

What I am trying here is, I want to get the string value of flevel and then when I click the search button it will proceed to the next (specific) Activity.
Goal is:
if the flevel is "Beginner" the next activity will be for the "Beginner", and if the flevel is "Experienced" the next activity will be for the "Experienced", and so on..
flevelfb = FilipinoBeginner
Bundle bnfb2 = getIntent().getExtras();
String flevelfb = bnfb2.getString("flevel");
flevel.setText(String.valueOf(flevelfb));
flevelfe = FilipinoExperienced
Bundle bnfe2 = getIntent().getExtras();
String flevelfe = bnfe2.getString("flevel");
flevel.setText(String.valueOf(flevelfe));
This part is for the search button:
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String flevels = flevel.getText().toString();
if (flevels.equals(flevelfb)){
Intent intent= new Intent(getApplicationContext(), FilipinoBeginner.class);
startActivity(intent);
}
else if (flevels.equals(flevelfe)){
Intent intent= new Intent(getApplicationContext(), FilipinoExperienced.class);
startActivity(intent);
}
}
});

you can change just search button click like this
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String flevels = flevel.getText().toString();
Intent intent = null;
if (flevels.equals("Beginner")){
intent = new Intent(getApplicationContext(), FilipinoBeginner.class);
}
else if (flevels.equals("Experienced")){
intent = new Intent(getApplicationContext(), FilipinoExperienced.class);
}
startActivity(intent);
}
});

Related

How to pass an intent back to MainActivity

My app has a RecyclerView in the MainActivity with an onClickListener which sends an intent to a DetailActivity. The DetailActivity has a button to go back without doing anything and a button to send an intent to the `Activity.
I already checked other posts in here like How to pass intent with extras to an already running activity and others, but no one results.
Here is a version that I tried:
MainActivity:
#Override
protected void onNewIntent(Intent intent) {
int codPedido = getIntent().getIntExtra("CodPedido",1);
//Toast to test if it's working
Toast.makeText(getApplicationContext(), String.valueOf(codPedido), Toast.LENGTH_SHORT).show();
}
OnClick of RecyclerViewAdapter:
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("CodigoPedido", pedidos.get(i).getCodPedido());
intent.putExtra("Local", pedidos.get(i).getLocal());
intent.putExtra("Dia",pedidos.get(i).getDia());
intent.putExtra("Periodo", pedidos.get(i).getPeriodo());
intent.putExtra("urgente", pedidos.get(i).isUrgente());
intent.putExtra("requisitante",pedidos.get(i).getRequisitante());
intent.putExtra("observacoes", pedidos.get(i).getObservacoes());
DetailActivity:
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_confirm) {
Intent intent = new Intent(DetailActivity.this, MainActivity.class);
intent.putExtra("CodPedido", codPedido);
startActivityForResult(intent, 1);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Edit:
This is what I tried now:
viewHolder.clRow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity mActivity = new MainActivity();
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("CodigoPedido", pedidos.get(i).getCodPedido());
intent.putExtra("Local", pedidos.get(i).getLocal());
intent.putExtra("Dia",pedidos.get(i).getDia());
intent.putExtra("Periodo", pedidos.get(i).getPeriodo());
intent.putExtra("urgente", pedidos.get(i).isUrgente());
intent.putExtra("requisitante",pedidos.get(i).getRequisitante());
intent.putExtra("observacoes", pedidos.get(i).getObservacoes());
mActivity.startActivityForResult(intent, 1);
}
});
and it gives method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
What I was doing before was using the context which is here in the constructor
public RvPedidosAdapter(List<Pedido> pedidos, Context context) {
this.pedidos = pedidos;
this.context = context;
}
like this:
viewHolder.clRow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("CodigoPedido", pedidos.get(i).getCodPedido());
intent.putExtra("Local", pedidos.get(i).getLocal());
intent.putExtra("Dia",pedidos.get(i).getDia());
intent.putExtra("Periodo", pedidos.get(i).getPeriodo());
intent.putExtra("urgente", pedidos.get(i).isUrgente());
intent.putExtra("requisitante",pedidos.get(i).getRequisitante());
intent.putExtra("observacoes", pedidos.get(i).getObservacoes());
context.startActivity(intent);
}
});
On Your code please pass startActivityForResult() with intent param with result code and get the result on MainActivity in onActivityResult();
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("CodigoPedido", pedidos.get(i).getCodPedido());
intent.putExtra("Local", pedidos.get(i).getLocal());
intent.putExtra("Dia",pedidos.get(i).getDia());
intent.putExtra("Periodo", pedidos.get(i).getPeriodo());
intent.putExtra("urgente", pedidos.get(i).isUrgente());
intent.putExtra("requisitante",pedidos.get(i).getRequisitante());
intent.putExtra("observacoes", pedidos.get(i).getObservacoes());
startActivityForResult(intent, 1);
On your second activity please use this code.
Intent intent = new Intent(DetailActivity.this, MainActivity.class);
intent.putExtra("CodPedido", codPedido);
setResult(RESULT_OK, intent);
finish();
and move to your MainActivity.class to get the result data in onActivityResult() method like this.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK){
(data.getIntExtra("CodPedido");
}
}
you can use
startActivityForResult(Intent intent,int requestcode);

How to use a variable in a new activity?

I added a variable that pass form the first activity to the second one.
I want to use the info that has accepted from the first activity, at the new activity, and will save it on new double variable, display it as permanent on a textView.
Now, it appears only when I am clicking on the regular button that start the new activity.
As first step, I guess, I need to remove - "startActivity(intent1);".
How should I move on from here?
Java code:
First Activity (Name : settings.java)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}
public void onClick (View v){
Intent intent = new Intent(settings.this, WaitressRecord.class);
startActivity(intent);
}
protected void onClickWait (View v) {
//--- Casting & Converting EditText "etSalaryWaitress" to Double "doubleSW".
btnWaitress =(Button)findViewById(R.id.btnWaitress);
etSalaryWaitress = (EditText) findViewById(R.id.etSalaryWaitress);
doubleSW = Double.parseDouble(etSalaryWaitress.getText().toString());
//---Casting Radio Button(s).
rbPercentage = (RadioButton)findViewById(R.id.rbPercentage);
rbShekel = (RadioButton)findViewById(R.id.rbShekel);
if (doubleSW < 100 ) {
if (rbPercentage.isChecked()) {
HafrashaP = 1 - (doubleSW / 100.0);
strHafPer = String.valueOf(HafrashaP);
Toast.makeText(settings.this, strHafPer, Toast.LENGTH_SHORT).show();
// start the SecondActivity
Intent intent1 = new Intent(this, WaitressRecord.class);
intent1.putExtra(Intent.EXTRA_TEXT, strHafPer);
startActivity(intent1);
} else if (rbShekel.isChecked()) {
HafrashaS = -doubleSW;
strHafShek = String.valueOf(HafrashaS);
Toast.makeText(settings.this, strHafShek, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(settings.this, "לא הוזנה סוג ההפרשה", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(settings.this, "מספר שגוי", Toast.LENGTH_SHORT).show();
}
}
New Activity: (Name : WaitressRecord.java)
public class WaitressRecord extends AppCompatActivity {
String strHafPer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_waitress_record);
// get the text from MainActivity
Intent intent1 = getIntent();
strHafPer = intent1.getStringExtra(Intent.EXTRA_TEXT);
// use the text in a TextView
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(strHafPer);
}
}
//First Activity
Intent intent= new Intent(this, SecondActivity.class);
Bundle extra = new Bundle();
mBundle.putString(VARIABLE_KEY, value);
intent.putExtras(mBundle);
startActivity(intent);
//on the second Acitivty
intent intent = getIntent();
Bundle bundleExtra;
//Null Checking
if (intent != null ) {
bundleExtra = getIntent().getExtras();
// Be sure your check your "VARIABLE KEY SAME AS in THE FIRST ACTIVITY
String resultString = extras.getString("VARIABLE_KEY");
}

Variables losing data between activities and I don't know why?

I am currently creating an android app that scans a network and outputs results in a ListView but I am trying to make it to where I tap on the network and it saves the data in a database then sends you to a page to show you what you selected but when I click an item it substrings the values correctly and displays work fine on the main activity but when I try to use the variables on my display page activity there values are set null.
Here is the main activity in the click listener:
networklist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String grabItemInfo = wifis[position];
Network_Info info1 = new Network_Info();
info1.setMainBSSID( grabItemInfo.substring(grabItemInfo.indexOf('#') +1, grabItemInfo.lastIndexOf('#')));
info1.setMainSSID( grabItemInfo.substring(0,(grabItemInfo.indexOf('#'))));
info1.setMainCAP( grabItemInfo.substring(grabItemInfo.lastIndexOf('#')+1, grabItemInfo.length()));
Toast toastTest = Toast.makeText(getApplicationContext(), info1.getMainSSID(), Toast.LENGTH_SHORT);
Toast toastTest2 = Toast.makeText(getApplicationContext(), info1.getMainBSSID(), Toast.LENGTH_SHORT);
Toast toastTest3 = Toast.makeText(getApplicationContext(), info1.getMainCAP(), Toast.LENGTH_SHORT);
toastTest.show();
toastTest2.show();
toastTest3.show();
ContentValues dbv = new ContentValues();
dbv.put("SSID", info1.getMainSSID());
dbv.put("BSSID", info1.getMainBSSID());
dbv.put("CAPABILITIES", info1.getMainCAP());
netDataBase.insert("netDataTable", "NULL", dbv);
Intent intent = new Intent(getApplicationContext(), Attack_Page.class);
startActivity(intent);
}
});
Here is my display page:
public class Attack_Page extends Network_List {
protected void onCreate(Bundle SavedIS){
super.onCreate(SavedIS);
setContentView(R.layout.attack_page);
TextView SSIDview = (TextView) findViewById(R.id.SSIDView);
TextView BSSIDview = (TextView) findViewById(R.id.BSSIDView);
TextView CAPview = (TextView) findViewById(R.id.CAPView);
Button backButton = (Button) findViewById(R.id.backbutton);
Intent intent = getIntent();
//String MainSSIDP = intent.getStringExtra(getMainSSID());
Network_Info info1 = new Network_Info();
Toast testToast = Toast.makeText(getApplicationContext(), info1.getMainSSID(), Toast.LENGTH_SHORT);
testToast.show();
//Cursor IDselect = netDataBase.rawQuery("SELECT SSID FROM netDataTable WHERE SSID = "+getMainSSID()+"", wifis);
//SSIDview.setText(IDselect.toString());
backButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent bintent = new Intent(getApplicationContext(), Network_List.class);
startActivity(bintent);
}
});
}
}
Here is my setters and getters class:
public class Network_Info {
private String mainCAP;
private String mainSSID;
private String mainBSSID;
public void setMainSSID(String newMainSSID){
mainSSID = newMainSSID;
}
public void setMainBSSID(String newMainBSSID){
mainBSSID = newMainBSSID;
}
public void setMainCAP(String newMainCAP){
mainCAP = newMainCAP;
}
public String getMainSSID(){
return mainSSID;
}
public String getMainBSSID(){
return mainBSSID;
}
public String getMainCAP(){
return mainCAP;
}
}
Figured out you have to pass the variable with the intent:
Intent intent = new Intent(getApplicationContext(), Attack_Page.class);
intent.putExtra("EXTRA_SSID", info1.getMainSSID());
intent.putExtra("EXTRA_BSSID", info1.getMainBSSID());
intent.putExtra("EXTRA_CAP", info1.getMainCAP());
startActivity(intent);
Then use the key that you set in putExtra()
String MainSSIDP = intent.getStringExtra("EXTRA_SSID");
Thanks for the help though!

Uri add text from edittext to link

findNumber.setOnClickListener(new View.OnClickListener() {
Uri uri = Uri.parse("http://wwww.whitepages.ca/phone/" + phoneNumber.getText());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
#Override
public void onClick(View v) {
startActivity(intent);
}
});
When I do that it only opened up "http://www.whitepages.com/phone/"

onClick Textview pass another string to intent

I made one TextView clickable. When it's clicked a new Intent is started.
articleURL [i].setText( articleURLArr [i] );
articleURL[i].setPaintFlags(articleURL[i].getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
articleURL[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println ( ((TextView) v).getText().toString() );
Intent intent = new Intent(getActivity().getBaseContext(), WebViewing.class);
intent.putExtra("sourceURL", ((TextView) v).getText().toString());
startActivity(intent);
}
});
But now I wanted to pass another value to the intent. source[i]. So I tried it this way
i.putExtra("source" , ((TextView) source[v.getId()]).getText().toString());
But that gives me an ArrayIndexOutOfBoundsException with index= -1.
How can I pass another value to the intent when I handle the onclick?
In my opinion you have two options to solve this:
1- Use i inside your click listener. To do it you need to assign it to a final variable.
final int index = i;
articleURL[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println ( ((TextView) v).getText().toString() );
Intent i = new Intent(getActivity().getBaseContext(), WebViewing.class);
i.putExtra("sourceURL", ((TextView) v).getText().toString());
i.putExtra("source" , ((TextView) source[index]).getText().toString());
startActivity(i);
}
});
2- Store the index in the view's tag (not id):
articleURL[i].setTag(i);
articleURL[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println ( ((TextView) v).getText().toString() );
Intent i = new Intent(getActivity().getBaseContext(), WebViewing.class);
i.putExtra("sourceURL", ((TextView) v).getText().toString());
i.putExtra("source" , ((TextView) source[Integer.parseInt(v.getTag())]).getText().toString());
startActivity(i);
}
});

Categories

Resources