I keep getting an error for this application every time I press the menu button --> History to start the History.java class. I'm fairly certain it has to do with the Bundle method for sending the two arrays from the TipBookActivity.java class to the History.java class.
Below is the TipBookActivity code:
public class TipBookActivity extends Activity {
/** Called when the activity is first created. */
TextView textTip,textHour,textWage;
EditText editHour,editTip;
float wage;
int precision = 100;
String sTip,sHour;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);
Button bSubmit = (Button) findViewById(R.id.bSubmit);
final Bundle bTip = new Bundle();
final Bundle bHour = new Bundle();
final ArrayList<String> tipList = new ArrayList<String>();
final ArrayList<String> hourList = new ArrayList<String>();
bSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textHour.setText(editHour.getText().toString());
textTip.setText(editTip.getText().toString());
wage = Float.parseFloat(textTip.getText().toString()) / Float.parseFloat(textHour.getText().toString());
String tip = String.format("$%.2f",wage);
textWage.setText(String.valueOf(tip) + " an hour");
textHour.setText(editHour.getText() + " Hour(s)");
textTip.setText("$" + editTip.getText());
bTip.putStringArray(sTip,new String[] {editTip.getText().toString()});
bHour.putStringArray(sHour,new String[] {editHour.getText().toString()});
tipList.addAll(Arrays.asList(sTip));
hourList.addAll(Arrays.asList(sHour));
Intent i = new Intent(TipBookActivity.this,History.class);
i.putExtras(bTip);
i.putExtras(bHour);
}
});
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater mMain = getMenuInflater();
mMain.inflate(R.menu.main_menu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuHistory:
startActivity(new Intent("com.smarticle.tipbook.HISTORY"));
return true;
case R.id.menuClear:
//set up next tutorials
Toast display = Toast.makeText(this, "Clear History feature coming soon.", Toast.LENGTH_SHORT);
display.show();
return true;
}
return false;
}
}
The History class code:
public class History extends Activity{
private ListView mainListViewTip;
private ListView mainListViewHour;
private ArrayAdapter<String>listAdapterTip;
private ArrayAdapter<String>listAdapterHour;
String sTip,sHour;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.history_main);
Bundle bTip = this.getIntent().getExtras();
Bundle bHour = this.getIntent().getExtras();
String[] array1 = bTip.getStringArray(sTip);
String[] array2 = bHour.getStringArray(sHour);
ListView mainListViewTip = (ListView) findViewById(R.id.mainListViewTip);
ListView mainListViewHour = (ListView) findViewById(R.id.mainListViewHour);
ArrayList<String> tipList = new ArrayList<String>();
ArrayList<String> hourList = new ArrayList<String>();
tipList.addAll(Arrays.asList(sTip));
hourList.addAll(Arrays.asList(sHour));
listAdapterTip = new ArrayAdapter<String>(this,R.layout.simplerow,tipList);
listAdapterHour = new ArrayAdapter<String>(this,R.layout.simplerow,hourList);
mainListViewTip.setAdapter(listAdapterTip);
mainListViewHour.setAdapter(listAdapterHour);
}
}
Any help on identifying the error cause would be greatly appreciated. The code works (in theory, I think), it just won't work in practice. The general idea is to input two numbers into EditText fields, save them as strings, display them as TextViews, set them as an ArrayList, then bundle and send them to the other class to display in a ListView.
You are not initializing sTip and sHour Strings in both Activities. so initializing sTip and Shour Strings with any constant value as in both Activities:
String sTip="sTip",sHour="sHour";
and from TipBookActivity you are not passing intent to startActivity so first declare Intent i globally then start your Activity as:
TextView textTip,textHour,textWage;
EditText editHour,editTip;
float wage;
int precision = 100;
String sTip,sHour;
Intent i; // declare here
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);
Button bSubmit = (Button) findViewById(R.id.bSubmit);
final Bundle bTip = new Bundle();
final Bundle bHour = new Bundle();
final ArrayList<String> tipList = new ArrayList<String>();
final ArrayList<String> hourList = new ArrayList<String>();
bSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textHour.setText(editHour.getText().toString());
textTip.setText(editTip.getText().toString());
wage = Float.parseFloat(textTip.getText().toString()) / Float.parseFloat(textHour.getText().toString());
String tip = String.format("$%.2f",wage);
textWage.setText(String.valueOf(tip) + " an hour");
textHour.setText(editHour.getText() + " Hour(s)");
textTip.setText("$" + editTip.getText());
bTip.putStringArray(sTip,new String[] {editTip.getText().toString()});
bHour.putStringArray(sHour,new String[] {editHour.getText().toString()});
tipList.addAll(Arrays.asList(sTip));
hourList.addAll(Arrays.asList(sHour));
i = new Intent(TipBookActivity.this,History.class);
i.putExtras(bTip);
i.putExtras(bHour);
}
});
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuHistory:
startActivity(i)); // start Activity here by passing intent
return true;
Approach you are following is completely wrong, you can put one Bundle at a time to the intent, and you are putting two bundles, sTip, and sHours.
Second Bundle sHours will override the first one, and I think its main cause of the null pointer exception, instead you should put all the values(in your case two String Arrays) to a single bundle. and put that bundle to the Intent.
Do as Follows:
public class TipBookActivity extends Activity {
/** Called when the activity is first created. */
TextView textTip,textHour,textWage;
EditText editHour,editTip;
float wage;
int precision = 100;
String sTip="sTip";
String sHour="sHour";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);
Button bSubmit = (Button) findViewById(R.id.bSubmit);
final Bundle bundle= new Bundle();
final ArrayList<String> tipList = new ArrayList<String>();
final ArrayList<String> hourList = new ArrayList<String>();
bSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textHour.setText(editHour.getText().toString());
textTip.setText(editTip.getText().toString());
wage = Float.parseFloat(textTip.getText().toString()) / Float.parseFloat(textHour.getText().toString());
String tip = String.format("$%.2f",wage);
textWage.setText(String.valueOf(tip) + " an hour");
textHour.setText(editHour.getText() + " Hour(s)");
textTip.setText("$" + editTip.getText());
bundle.putStringArray(sTip,new String[] {editTip.getText().toString()});
bundle.putStringArray(sHour,new String[] {editHour.getText().toString()});
tipList.addAll(Arrays.asList(sTip));
hourList.addAll(Arrays.asList(sHour));
Intent i = new Intent(TipBookActivity.this,History.class);
i.putExtras(bundle);
}
});
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater mMain = getMenuInflater();
mMain.inflate(R.menu.main_menu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuHistory:
startActivity(new Intent("com.smarticle.tipbook.HISTORY"));
return true;
case R.id.menuClear:
//set up next tutorials
Toast display = Toast.makeText(this, "Clear History feature coming soon.", Toast.LENGTH_SHORT);
display.show();
return true;
}
return false;
}
}
and in History Activity:
public class History extends Activity{
private ListView mainListViewTip;
private ListView mainListViewHour;
private ArrayAdapter<String>listAdapterTip;
private ArrayAdapter<String>listAdapterHour;
String sTip="sTip";
String sHour="sHour";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.history_main);
Bundle bundle= this.getIntent().getExtras();
String[] array1 = bundle.getStringArray(sTip);
String[] array2 = bundle.getStringArray(sHour);
ListView mainListViewTip = (ListView) findViewById(R.id.mainListViewTip);
ListView mainListViewHour = (ListView) findViewById(R.id.mainListViewHour);
ArrayList<String> tipList = new ArrayList<String>();
ArrayList<String> hourList = new ArrayList<String>();
tipList.addAll(Arrays.asList(sTip));
hourList.addAll(Arrays.asList(sHour));
listAdapterTip = new ArrayAdapter<String>(this,R.layout.simplerow,tipList);
listAdapterHour = new ArrayAdapter<String>(this,R.layout.simplerow,hourList);
mainListViewTip.setAdapter(listAdapterTip);
mainListViewHour.setAdapter(listAdapterHour);
}
}
Related
I am trying pass data from my dialog box to a new Activity. So basically what i want to do for example in my Dialog box i have Name: John, when i click on my okay button it opens a new activity but i want that John to be set in my edit box on my new activity:
Here is my Activity where my Dialog box code is:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/** To change selected state view */
view.setSelected(true);
HashMap<String, Object> obj = (HashMap<String, Object>) ADAhere.getItem(position);
String SlectedName = (String) obj.get("NAME");
String SlectedPrice = (String) obj.get("PRICE");
String SlectedSize = (String) obj.get("SIZE");
String SlectedRange = (String) obj.get("RANGE");
String SlectedSupp = (String) obj.get("SUPPLIER");
// Toast.makeText(getActivity().getApplicationContext(), SlectedName, Toast.LENGTH_SHORT).show();
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow();
//dialog.setTitle("Confirm your Vote");
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.diaglog);
final TextView VName = (TextView) dialog.findViewById(R.id.Name);
final TextView VRange = (TextView) dialog.findViewById(R.id.Range);
final TextView VSUPPLIER = (TextView) dialog.findViewById(R.id.Supplier);
final TextView VSIZE = (TextView) dialog.findViewById(R.id.Size);
final TextView VPrice = (TextView) dialog.findViewById(R.id.Price);
VName.setText(SlectedName);
VRange.setText(SlectedRange);
VSUPPLIER.setText(SlectedSupp);
VSIZE.setText(SlectedSize);
VPrice.setText(SlectedPrice);
dialog.show();
Button cancelBtn = (Button) dialog.findViewById(R.id.cancel_btn);
cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
Button UpdateBtn = (Button) dialog.findViewById(R.id.btn_update);
UpdateBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity().getApplicationContext(),Upvalence.class);
startActivity(i);
}
});
Button deleteBtn = (Button) dialog.findViewById(R.id.btn_delete);
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity().getApplicationContext(),Upvalence.class);
startActivity(i);
}
});
dialog.show();
}
});
so i want to pass VName.setText(SlectedName); to a new activity:
name1.setText(dialogactivityname);
Pass in as an extra:
instead of:
Intent i = new Intent(getActivity().getApplicationContext(),Upvalence.class);
startActivity(i);
you should pass in the name
Intent i = new Intent(getActivity().getApplicationContext(),Upvalence.class);
i.putExtra("string", SlectedName);
startActivity(i);
Then, on your Upvalence activity:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
Bundle arguments = this.getIntent().getExtras();
String yourString = arguments.getString("string");
}
I have an app, where a user can enter a number and save to listView. The app is split into 3 activities.
The first activity is the MainActivity, which does something else. The second activity is where I enter a number and save it. The code Is below
public class HomeTeamScored extends Activity {
protected static EditText display;
protected static ArrayAdapter<String> adapter;
Button addButton;
CheckBox checkBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rect_activity_home_goal_player);
display = (EditText) findViewById(R.id.editText);
addButton = (Button) findViewById(R.id.btn);
checkBox = (CheckBox) findViewById(R.id.checkBox);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String task = display.getText().toString();
adapter.add(task);
adapter.notifyDataSetChanged();
SavePreferences();
Intent i = new Intent(HomeTeamScored.this, MainActivity.class);
startActivity(i);
finish();
}
});
}
protected void SavePreferences() {
// TODO Auto-generated method stub
SharedPreferences data = getApplicationContext().getSharedPreferences("saveNumber", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = data.edit();
for (int i = 0; i < adapter.getCount(); ++i){
// This assumes you only have the list items in the SharedPreferences.
editor.putString(String.valueOf(i), adapter.getItem(i));
}
editor.commit();
}
}
In my third activity, I retrieve the saved number.
public class ScoreBoardOverview extends Activity {
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rect_activity_score_board_overview);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
lv = (ListView) findViewById(R.id.listView);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
loadPreferences();
}
protected void loadPreferences(){
SharedPreferences data = getApplicationContext().getSharedPreferences("saveNumber", Context.MODE_PRIVATE);
for (int i = 0;; ++i){
final String str = data.getString(String.valueOf(i), "");
if (!str.equals("")){
adapter.add(str);
} else {
break; // Empty String means the default value was returned.
}
}
}
}
All this is working fine, and it adds a number to listView, but the problem is that it overrides the old value, were instead it should add a second row.
Anyone who can help??
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String task = display.getText().toString();
adapter.add(task);
adapter.notifyDataSetChanged();
SavePreferences();
Intent i = new Intent(HomeTeamScored.this, MainActivity.class);
startActivity(i);
finish();
}
});
}
here your adapter item size will be 0 when you call second activity each time. if you want to add second or third item you should write your
protected void loadPreferences(){
SharedPreferences data = getApplicationContext().getSharedPreferences("saveNumber", Context.MODE_PRIVATE);
for (int i = 0;; ++i){
final String str = data.getString(String.valueOf(i), "");
if (!str.equals("")){
adapter.add(str);
} else {
break; // Empty String means the default value was returned.
}
}
}
method in second activity too. and call loadPreferences() after this line
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
your adapter data size will be changed.
I am trying to pass an array list to another activity but it seems that is not enough. I searched all day to pass the array list with "intent" but with no success. I wrote a code for learning purposes. How to pass the data and show the Arraylist in a second activity?
The action button is btn_save. If you want further details let me know.
The code is in
MainActivity (first activity):
public class MainActivity extends AppCompatActivity {
ArrayList<String> addArray = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editTextOne = (EditText) findViewById(R.id.editTextOne);
final EditText editTextTwo = (EditText) findViewById(R.id.editTextTwo);
Button btn_showText = (Button) findViewById(R.id.buttonShow);
final TextView textView = (TextView) findViewById(R.id.textResults);
Button btn_refresh = (Button) findViewById(R.id.btn_refresh);
Button btn_close = (Button) findViewById(R.id.btn_close);
Button btn_save = (Button) findViewById(R.id.btn_save);
final ListView showMe = (ListView) findViewById(R.id.list_items);
btn_showText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = editTextOne.getText().toString();
String textTwo = editTextTwo.getText().toString();
if (text.length() == 0 || textTwo.length() == 0){
textView.setText("You must enter Name & Surname");
}else {
textView.setText(Html.fromHtml(
"<font color=\"red\">"+ text + "</font> " +
"<font color=\"blue\"><b>" + textTwo + " </b></font>"));
}
}
});
btn_refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
startActivity(getIntent());
}
});
btn_close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
btn_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Context context = getApplicationContext();
// CharSequence mytext = "Hahahaha";
// int duration = Toast.LENGTH_SHORT;
// Toast toast = Toast.makeText(context,mytext,duration);
// toast.show();
String text = editTextOne.getText().toString();
String textTwo = editTextTwo.getText().toString();
String getInput = text + textTwo;
if (addArray.contains(getInput)){
Toast.makeText(getBaseContext(), "Item already Added!", Toast.LENGTH_LONG).show();
}
else if (getInput == null || getInput.trim().equals("")){
Toast.makeText(getBaseContext(), "Input field is empty", Toast.LENGTH_LONG).show();
}
else{
addArray.add(getInput);
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, addArray);
showMe.setAdapter(adapter);
Intent intent = new Intent(MainActivity.this, ListOfNames.class);
((EditText)findViewById(R.id.editTextOne)).setText(" ");
((EditText)findViewById(R.id.editTextTwo)).setText(" ");
}
}
});
}
public void onButtonClick(View v){
if (v.getId() == R.id.btn_list){
Intent i = new Intent(MainActivity.this, ListOfNames.class);
startActivity(i);
}
}
}
ListOfNames second activity: (almost empty)
public class ListOfNames extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_screen);
}
}
If you are trying to pass a arraylist of string then it will be easy. Just pass arraylist with intent like this:
ArrayList<String> list = new ArrayList<String>();
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putStringArrayListExtra("key", list);
startActivity(intent);
And receive it in ActivityTwo like this:
ArrayList<String> list = getIntent().getStringArrayListExtra("key");
I found a temporary solution for this (The app is not broke). The only problem is the arraylist didn't increased. It contains and show only the last value.
Main Activity:
...
else{
// addArray.add(getInput);
// ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, addArray);
// showMe.setAdapter(adapter);
Intent intent = new Intent(MainActivity.this, ListOfNames.class);
intent.putExtra("data", getInput);
startActivity(intent);
// ((EditText)findViewById(R.id.editTextOne)).setText(" ");
// ((EditText)findViewById(R.id.editTextTwo)).setText(" ");
}
...
ListOfNames (Second Activity):
public class ListOfNames extends Activity {
ArrayList<String> addArrayT = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_screen);
Bundle bundle = getIntent().getExtras();
String data = bundle.getString("data");
Button btn_more = (Button) findViewById(R.id.btn_more);
ListView showMe = (ListView) findViewById(R.id.list_items);
addArrayT.add(data);
ArrayAdapter<String> adapterT = new ArrayAdapter<>(ListOfNames.this, android.R.layout.simple_list_item_1, addArrayT);
showMe.setAdapter(adapterT);
}
public void onClickMore(View v){
if (v.getId() == R.id.btn_more){
Intent i = new Intent(ListOfNames.this, MainActivity.class);
startActivity(i);
}
}
}
I have activity with spinners.All I want to is: to add new spinners with same data after button click. Everything showed on pictures. And yes, I am newby, so sorry for every mistakes :)
Have a nice day
public class NaujasUzsakymasList extends ActionBarActivity {
PrekesDBController controller = new PrekesDBController(this);
ListView ls;
ScrollView sv;
TextView infotext;
CheckBox check;
LinearLayout ll;
EditText vnt;
private Spinner spinner1, spinner2;
private Button btnSubmit, btnprideti;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.naujasuzsakymailist);
ScrollView sv = (ScrollView) findViewById(R.id.myscroll);
LinearLayout ll = (LinearLayout) findViewById(R.id.mylinearLayout);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// delete = (Button) findViewById(R.id.btndel);
ls = (ListView) findViewById(R.id.prekeslist);
infotext = (TextView) findViewById(R.id.txtresulttext);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnprideti = (Button) findViewById(R.id.prideti);
// check = (CheckBox) findViewById(R.id.checkBox4);
// vnt = (EditText) findViewById(R.id.editText2);
addItemsOnSpinner2();
addItemsOnSpinner1();
addListenerOnButton();
addListenerOnButtonprideti();
addListenerOnSpinnerItemSelection();
}
/// delete.setOnClickListener(new View.OnClickListener() {
// #Override
//public void onClick(View v) {
// controller = new UzsakymaiDBController(getApplicationContext());
/// SQLiteDatabase db = controller.getWritableDatabase();
// db.delete("uzsakymai", "uzsakymoid=" + txtuzsakymoid, null);
// }
// });
// #Override
// public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.menu_main, menu);
// return true;
// }
public void addItemsOnSpinner2() {
setContentView(R.layout.naujasuzsakymailist);
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
list.add("6");
list.add("7");
list.add("8");
list.add("9");
list.add("10");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
public void addItemsOnSpinner1() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
// ArrayList<HashMap<String, String>> data2 = controller.getpavadinimas();
ArrayList<HashMap<String, String>> data2 = controller.getpavadinimas();
int i = 0;
final String pavadinimas[] = new String[data2.size()];
for (HashMap<String, String> hashMap : data2) {
for (String value : hashMap.values()) {
pavadinimas[i] = value ;
i++;
}
}
List<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList, pavadinimas);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arrayList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(NaujasUzsakymasList.this,
"OnClickListener : " +
"\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
});
}
public void addListenerOnButtonprideti() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
btnprideti = (Button) findViewById(R.id.prideti);
for (int j = 0; j < 20; j++) {
btnprideti.setOnClickListener(new View.OnClickListener() {
int j = 0;
#Override
public void onClick(View v) {
ScrollView sv = (ScrollView) findViewById(R.id.myscroll);
LinearLayout ll = (LinearLayout) findViewById(R.id.mylinearLayout);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.naujasuzsakymailist12, null);
// Button btnprideti = (Button) view.findViewById(R.id.spinner1);
Spinner spinner1 = (Spinner) view.findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) view.findViewById(R.id.spinner2);
// btnprideti.setText("Nr." + j);
//cb.isChecked();
ll.addView(view);
j++;
}
});
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void DisplayToast(String msg) {
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
}}
I have editText in Setup Activity. I used SharedPreference for saving this value and get later. I passed this edittext value to another activity button text. Initially i need to hide the button. If edittext values are coming from sharedPreference i need to display the button.Thanks alot
public class Setup extends Activity implements AdapterView.OnItemSelectedListener {
EditText editText;
Button button;
Spinner spinner;
TextView text;
ArrayList<String> ar = new ArrayList<String>();
// String a=editText.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
button = (Button) findViewById(R.id.button1);
editText = (EditText) findViewById(R.id.ed1);
spinner = (Spinner) findViewById(R.id.spinner1);
text = (TextView) findViewById(R.id.back);
getnameButton();
ArrayAdapter<String> arr = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ar);
arr.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
spinner.setAdapter(arr);
spinner.setOnItemSelectedListener(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("sample", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name",editText.getText().toString());
editor.putInt("position",spinner.getSelectedItemPosition());
editor.commit();
Intent myIntent = new Intent(Setup.this, MainActivity.class);
startActivity(myIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_setup, menu);
return true;
}
public void getnameButton() {
Intent intent = getIntent();
String text1 = intent.getStringExtra("text1");
String text2 = intent.getStringExtra("text2");
String text3 = intent.getStringExtra("text3");
String text4 = intent.getStringExtra("text4");
String text5 = intent.getStringExtra("text5");
String text6 = intent.getStringExtra("text6");
String text7 = intent.getStringExtra("text7");
String text8 = intent.getStringExtra("text8");
ar.add(text1);
ar.add(text2);
ar.add(text3);
ar.add(text4);
ar.add(text5);
ar.add(text6);
ar.add(text7);
ar.add(text8);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
editText.setText(ar.get(position));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
=>Activity Class
public class MainActivity extends Activity {
Button button1,button2,button3,button4,button5,button6,button7,button8;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button) findViewById(R.id.btn1);
button2=(Button) findViewById(R.id.btn2);
button3=(Button) findViewById(R.id.btn3);
button4=(Button) findViewById(R.id.btn4);
button5=(Button) findViewById(R.id.btn5);
button6=(Button) findViewById(R.id.btn6);
button7=(Button) findViewById(R.id.btn7);
button8=(Button) findViewById(R.id.btn8);
textView=(TextView) findViewById(R.id.txtsua);
getName();
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),Setup.class);
intent.putExtra("text1", button1.getText().toString());
intent.putExtra("text2",button2.getText().toString());
intent.putExtra("text3",button3.getText().toString());
intent.putExtra("text4", button4.getText().toString());
intent.putExtra("text5",button5.getText().toString());
intent.putExtra("text6",button6.getText().toString());
intent.putExtra("text7", button7.getText().toString());
intent.putExtra("text8", button8.getText().toString());
startActivity(intent);
}
});
// getnameButton();
}
public void getName() {
button1.setVisibility(View.GONE);
SharedPreferences preferences = getSharedPreferences("sample", 0);
String Namestr = (preferences.getString("Name", ""));
int position = (preferences.getInt("position", Integer.parseInt("")));
for (int i = 0; i < 8; i++) {
Button[] a = {button1, button2, button3, button4, button5, button6, button7, button8};
if (Namestr.length() > 0&&position==i) {
a[i].setVisibility(View.VISIBLE);
a[i].setText(preferences.getString("Name", ""));
}
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}}