Passing dynamic string to activity - java

I create an activity with dynamic buttons in a loop. I get a list and create a button for each element in the list. The buttons go to the same activity afterward, but with each button I want to pass different string.
I did this in the loop:
tour_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TourListActivity.this,
TourMenuActivity.class);
String info = tour.toString();
intent.putExtra(TOUR_INFO, info);
startActivity(intent);
}
});
But at the end, all the buttons get the same string (the string of the last button).
========================================
full code:
try {
JsonObject respObject = jsonParser.parse(response).getAsJsonObject();
JsonArray tourListArray = respObject.getAsJsonArray("tours");
System.out.println("tourListArray: " + tourListArray.toString());
for(int i = 0; i < tourListArray.size(); i++){
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
tour = tourListArray.get(i).getAsJsonObject();
String tourCode = tour.get("tourcode").getAsString();
Button tour_button = new Button(this);
tour_button.setText("Tour Code: " + tourCode);
tour_button.setGravity(Gravity.LEFT);
tour_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TourListActivity.this,
TourMenuActivity.class);
String info = tour.toString();
intent.putExtra(TOUR_INFO, info);
startActivity(intent);
}
});
ll.addView(tour_button);
LinearLayout yourLL = (LinearLayout) findViewById(R.id.Tours_List);
yourLL.setOrientation(LinearLayout.VERTICAL);
yourLL.addView(ll);
}
} catch (JsonIOException e) {
e.printStackTrace();
}

When you create the button you can:
button.setTag(someString);
and then in the onClick you can:
public void onClick(View v) {
Intent intent = new Intent(TourListActivity.this,
TourMenuActivity.class);
String info = tour.toString();
intent.putExtra(TOUR_INFO, ((Button)v).getTag());
startActivity(intent);
}

The variable tour is defined outside the loop, so each button share the same variable.
At each iteration, you just change the reference stored by this variable.
You could to create a final variable inside your loop, and use it inside the OnClickListener:
for (int i = 0; i < tourListArray.size(); i++) {
...
final String tourInfo = tour.info;
tour_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(
TourListActivity.this,
TourMenuActivity.class
);
intent.putExtra(TOUR_INFO, tourInfo);
startActivity(intent);
}
});
...
}

Related

status bar goes black while showing bottom sheet dialog

how do I prevention the status bar from loosing it's color while a bottom sheet is showing up?
I use a bottom sheet view with custom layout.
tried modify the style of the dialog but still no luck
here is my code
if (position == 1) {
View modalbottomsheet = getActivity().getLayoutInflater().inflate(R.layout.others_sheet, null);
dialog = new BottomSheetDialog(getActivity());
dialog.setContentView(modalbottomsheet);
dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(true);
dialog.show();
contact_num = (EditText) modalbottomsheet.findViewById(editText);
choose_contact = (Button) modalbottomsheet.findViewById(choose);
choose_contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == choose_contact.getId()) {
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE);
}
}
});
hidetext = (EditText) modalbottomsheet.findViewById(R.id.hidetext);
recharge_btn = (Button) modalbottomsheet.findViewById(R.id.rechargeothers_btn);
recharge_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId()== recharge_btn.getId()){
String phone = ((EditText) dialog.findViewById(R.id.editText)).getText().toString();
String hidden = ((EditText) dialog.findViewById(R.id.hidetext)).getText().toString();
String pre = "*805*2";
String hash = "%23";
String dail = "tel:" + pre + phone + hidden + hash;
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dail)));
dialog.hide();
}
}
});
}

RecyclerView passing data via Intent extras

So I'm trying to pass some data via Intent extras to the second activity. This code worked fine with ListView, but now when I switched to RecyclerView it doesn't show any text, text area is blank.
Here's the code: (starting in onBindViewHolder())
holder.container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
passData();
}
});
}
private void passData() {
Todo item = new Todo();
Intent i = new Intent(c, Details.class);
i.putExtra("nazivTodoa", item.getTitle());
i.putExtra("datumTodoa", item.getRecordDate());
i.putExtra("idTodoa", item.getItemId());
c.startActivity(i);
}
And this is how I get in in second activity:
Bundle extras = getIntent().getExtras();
String naslov = extras.getString("nazivTodoa");
String datum = extras.getString("datumTodoa");
textViewNazivTodoaDetails.setText(naslov);
textViewDatumTodoaDetails.setText(datum);
What am I doing wrong?
What you are doing wrong, is you are not getting the current object that is clicked on. You do that by getting your object from the arraylist you use in your adapter. Do it like that:
Arraylist<Item> yourlist = new Arraylist();
#Override
public void onClick(View v) {
// position = pass the current position of the object you want
passData(int position);
}
});
}
private void passData(int position) {
Todo item = new Todo();
Intent i = new Intent(c, Details.class);
i.putExtra("nazivTodoa", yourlist.get(position).getTitle());
i.putExtra("datumTodoa", yourlist.get(position).item.getRecordDate());
i.putExtra("idTodoa", yourlist.get(position).item.getItemId());
c.startActivity(i);
}
Like dymmeh said, you need to pass the item in the list to the new activity. You are currently passing a newly created empty object.
Instead of
Todo item = new Todo();
Intent i = new Intent(c, Details.class);
i.putExtra("nazivTodoa", item.getTitle());
i.putExtra("datumTodoa", item.getRecordDate());
i.putExtra("idTodoa", item.getItemId());
You should have
Todo item = listData.get(itemPosition);
Intent i = new Intent(c, Details.class);
i.putExtra("nazivTodoa", item.getTitle());
i.putExtra("datumTodoa", item.getRecordDate());
i.putExtra("idTodoa", item.getItemId());

How to pass string id's through intent?

i want to send integer value through intents , and access it on another activity ... i tried using the following code .. but its not working..!!
mCheatButton = (Button)findViewById(R.id.cheat_button);
mCheatButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mCheatButton.setVisibility(View.INVISIBLE);
mIsCheater = false;
int mCurrentIndex=2;
Intent i = new Intent(quizactivity.this, CheatActivity.class);
i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, mCurrentIndex);
startActivityForResult(i, 0);
}
catch (Exception e)
{
}
}
});
SECOND ACTIVITY:
mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(extras == null)
{ mAnswerIsTrue= 0;
updateanswer(mAnswerIsTrue);
}
else
{
mAnswerIsTrue= extras.getInt(EXTRA_ANSWER_IS_TRUE);
updateanswer(mAnswerIsTrue);
}
updateanswer(mAnswerIsTrue);
}
});
public void updateanswer(int q){
mAnswerTextView.setText(solution[q].getA());
setAnswerShownResult(true);
}
please help me solve it out..!!
i want to send integer value through intents , and access it on another activity ... i tried using the following code .. but its not working..!!
what is the error in above code.. i cant really understand...!!
Your problem is on your second activity, I guess.
Intent extras = getIntent();
int yourInt = extras.getIntExtra(EXTRA_ANSWER_IS_TRUE, -1);
In you second activity you need to do
int i = getIntent().getExtras().getInt(CheatActivity.EXTRA_ANSWER_IS_TRUE)

Sorting the arraylist by the values user has inputted

How can i sort the arraylist if the user inputs values himself. For example if user adds: Ben, Tom, Jack. And then presses play it opens a new activity and shows the name Jack. if you press back, Tom. And then Ben. I want it to be the other way around. I tried collections.sort() but had no luck with it. Any other ideas?
The array:
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
The way user adds names:
public void onClick(View v) {
String input = mYrasytiVarda.getText().toString();
if(input.length() > 0)
{
// add string to the adapter, not the listview
adapter.add(input);
// no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
}else{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Klaida:");
alertDialog.setMessage("Blogai yrašytas vardas");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.show();
}
}
Here I get the names and send them to the other activity:
#Override
public void onClick(View v) {
// count items
int i;
for (i = 0; i < adapter.getCount(); i++) {
String obj = adapter.getItem(i);
// send items to other activity
Intent pradetiZaidima = new Intent(v.getContext(), ZaidimasActivity.class);
pradetiZaidima.putExtra("playerList", obj);
startActivity(pradetiZaidima);
}
}
Here I display them:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zaidimas);
//get the player list from ZaidejaiActivity
Bundle recdData = getIntent().getExtras();
String myVal = recdData.getString("playerList");
//show the player list
mZaidejas = (TextView)findViewById(R.id.ZaidejoVardas);
mZaidejas.setText(myVal);
}
});
}
In this loop:
for (i = 0; i < adapter.getCount(); i++) {
String obj = adapter.getItem(i);
// send items to other activity
Intent pradetiZaidima = new Intent(v.getContext(), ZaidimasActivity.class);
pradetiZaidima.putExtra("playerList", obj);
startActivity(pradetiZaidima);
}
... you create the activity once for each item in the list, in the order they are stored in the ArrayAdapter. You could just iterate through the items in the reverse order.
Eg:
#Override
public void onClick(View v) {
for (int i = adapter.getCount()-1; i >= 0; i--) {
String item = adapter.getItem(i);
Intent pradetiZaidima = new Intent(v.getContext(), ZaidimasActivity.class);
pradetiZaidima.putExtra("playerList", item);
startActivity(pradetiZaidima);
}
}

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