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();
}}
Related
I have three spinners in my activity. So I have to open specific activity based on the input of three spinners; if I have entered "1" in my first spinner "cse" in my second spinner and "b" in my third spinner I have to open the respected activity and so on for different combinations.
I have tried using if statements and the app is crashing every time since.
Please beware of the comments in the code those are the codes which I have tried and didn't get any result.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Button btn;
/*TextView selection1;
TextView selection2;
TextView selection3;*/
Spinner spin1;
Spinner spin2;
Spinner spin3;
String[] years = {"1","2","3","4"};
String[] branches = {"CSE","ECE","EEE","CIVIL","IT","MECH"};
String[] sections = {"A","B","C"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin1 = (Spinner) findViewById(R.id.spinner1);
spin1.setOnItemSelectedListener(this);
/*List<String> branches = new ArrayList<String>();
branches.add("CSE");
branches.add("ECE");
branches.add("IT");
branches.add("MECH");
branches.add("CIVIL");
branches.add("EEE");*/
ArrayAdapter<String> dataAdapter =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,years);
//Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//attaching dataAdapter to spinner
spin1.setAdapter(dataAdapter);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
spin2.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,branches);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(dataAdapter2);
Spinner spin3 = (Spinner) findViewById(R.id.spinner3);
spin3.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,sections);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin3.setAdapter(dataAdapter3);
Button btn= (Button) findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displaycheck();
}
});
}
public void displaycheck(){
/*Spinner s1 = (Spinner)findViewById(R.id.spinner1);
Spinner s2 = (Spinner)findViewById(R.id.spinner2);
Spinner s3 = (Spinner)findViewById(R.id.spinner3);
String sp1 = s1.getPrompt().toString();
String sp2 = s2.getPrompt().toString();
String sp3 = s3.getPrompt().toString();
if (sp1.equals("3")&&sp2.equals("CSE")&&sp3.equals("B")){*/
Toast.makeText(this, "Welcome", Toast.LENGTH_LONG).show();
Intent it = new Intent(this,M2.class);
startActivity(it);
//}
/*else{
Toast.makeText(this, "Invalid input", Toast.LENGTH_LONG).show();
}*/
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//on selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
/*if (spin1.getSelectedItem().toString().equals("3")) {
if (spin2.getSelectedItem().toString().equals("CSE")) {
if (spin3.getSelectedItem().toString().equals("B")) {*/
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
/*Intent it = new Intent(this, M2.class);
startActivity(it);*/
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
/*Button btn =(Button)findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
displaycheck();
}
}
);
}
public void displaycheck(){
Spinner s1 =(Spinner) findViewById(R.id.spinner1);
Spinner s2 = (Spinner) findViewById(R.id.spinner2);
Spinner s3 = (Spinner) findViewById(R.id.spinner3);
String ye = y.getText().toString();
String br = b.getText().toString();
String se = s.getText().toString();
if (ye.equals("3")&&br.equals("cse")&&se.equals("b")) {
Toast.makeText(this, "ra ra ", Toast.LENGTH_LONG).show();
Intent it = new Intent(this, M2.class);
startActivity(it);
}
else{
Toast.makeText(this, "po po", Toast.LENGTH_LONG).show();
}
}*/
}
You are not giving value to variable which are global - Activity Scope - spin1, spin2, spin3.
Instead your are giving value to onCreate or Method specific scope; when you're fetching value from it it's never initialized.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Button btn;
Spinner spin1;
Spinner spin2;
Spinner spin3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get spinner in variable
spin1 = (Spinner) findViewById(R.id.spinner1);
...
// Your adapter, ItemListener, DropDownViewResource
spin2 = (Spinner) findViewById(R.id.spinner2);
... // Your adapter, ItemListener, DropDownViewResource
spin3 = (Spinner) findViewById(R.id.spinner3);
... // Your adapter, ItemListener, DropDownViewResource
Button btn= (Button) findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displaycheck();
}
});
}
public void displaycheck(){
String sp1 = spin1.getSelectedItem().toString();
String sp2 = spin2.getSelectedItem().toString();
String sp3 = spin3.getSelectedItem().toString();
// Your combinations check
if (sp1.equals("3")&&sp2.equals("CSE")&&sp3.equals("B")){
// based on combination - redirect user
}
}
}
To get selected item from spinner you use this
String value1 = s1.getSelectedItem().toString();
String value2 = s2.getSelectedItem().toString();
String value3 = s3.getSelectedItem().toString();
Now you can check the values with any logic for example:
if(value1.equals("a") && value2.equals("cse") && value3.equals("b"))
openActivityCodeHere();
I made the close button on the inflated layer. And I want to close this inflated layer when I click that button. How can I solve this problem
I used Rl.setVisibility(View.GONE);
But When I restart this method it's not working properly.
(different layer comes out...all the buttons are not working)
public void layer(){
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final RelativeLayout Rl = (RelativeLayout) inflater.inflate(R.layout.menu_bookmark, null);
//Rl.setBackgroundColor(Color.parseColor("#99000000"));
final RelativeLayout.LayoutParams paramRl = new RelativeLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, WRAP_CONTENT);
addContentView(Rl, paramRl);
final String current_url = webView.getUrl();
final Button btn_add = (Button) findViewById(R.id.btn_add);
final Button btn_out = (Button) findViewById(R.id.btn_out);
final EditText editText = (EditText) findViewById(R.id.edit_input);
editText.setText(current_url);
final ListView listView = (ListView) findViewById(R.id.lv_bookmark);
final ArrayList<String> arrayList = new ArrayList<String>();
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.menu_bookmark_row, arrayList);
listView.setAdapter(arrayAdapter);
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString().length() > 0) {
String inputStr = editText.getText().toString();
arrayList.add(inputStr);
arrayAdapter.notifyDataSetChanged();
}
}
});
btn_out.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// <--I NEED HELP!!!!!
}
});
parentLinearLayout.removeView((View) v.getParent());
https://www.androidtutorialpoint.com/basics/dynamically-add-and-remove-views-in-android/
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;
}}
I'm fairly new
I have succeeded to display my search results in my listview using an EditText.
Now when I click my result to bring up the "details" it gives me a wrong database record. The question is how to get the record of the clickedItem from my searchresults.
This is the List.java
public class List extends ActionBarActivity {
DatabaseManager db;
java.util.List<Passwords> passwordLijst = new ArrayList<Passwords>();
java.util.List<String> passwordsTitelLijst = new ArrayList<String>();
EditText inputSearch;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
SwipeRefreshLayout mSwipe = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
ImageView imageView=new ImageView(this);
imageView.setImageResource(R.drawable.ic_add_white_24dp);
FloatingActionButton actionButton = new FloatingActionButton.Builder(this)
.setContentView(imageView)
.setBackgroundDrawable(R.drawable.selecter_button)
.build();
actionButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), addPass.class);
startActivity(intent);
}
}
);
mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
setRefreshListener();
}
});
initDatabaseManager();
}
private void setRefreshListener() {
construeerPasswordLijsten();
constueerScherm();
SwipeRefreshLayout mSwipe = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
mSwipe.setRefreshing(false);
}
#Override
protected void onResume(){
super.onResume();
construeerPasswordLijsten();
constueerScherm();
}
private void construeerPasswordLijsten(){
passwordLijst = db.getAllPassword();
passwordsTitelLijst = Passwords.constructTitleList(passwordLijst);
}
private void constueerScherm(){
final ListView myListView = (ListView) findViewById(R.id.mylist);
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),R.layout.rowlist, passwordsTitelLijst);
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
int index = position;
Passwords clickedItem = passwordLijst.get(index);
Intent detailIntent = new Intent(getApplicationContext(), ListDetail.class);
detailIntent.putExtra("clickedItem", clickedItem);
startActivity(detailIntent);
}
});
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
ArrayList<String> temp = new ArrayList<String>();
int textlength = inputSearch.getText().length();
temp.clear();
for (int i = 0; i < passwordsTitelLijst.size(); i++) {
if (textlength <= passwordsTitelLijst.get(i).length()) {
if (inputSearch.getText().toString().equalsIgnoreCase(
(String)
passwordsTitelLijst.get(i).subSequence(0,
textlength))) {
temp.add(passwordsTitelLijst.get(i));
}
}
}
myListView.setAdapter(new ArrayAdapter<String>(List.this, R.layout.rowlist, temp));
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
int index = position;
Passwords clickedItem = passwordLijst.get(index);
Intent intent1 = new Intent(List.this, ListDetail.class);
intent1.putExtra("clickedItem", clickedItem);
startActivity(intent1);
}
});
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
}
Here is the ListDetail.java
public class ListDetail extends ActionBarActivity {
EditText showPW;
CheckBox mcbPW;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_detail);
initialise();
setEditButtonListener();
setSaveButtonListener();
setDeleteButtonListener();
showPW = (EditText) findViewById(R.id.text_detail_pw);
mcbPW = (CheckBox) findViewById(R.id.cbPW);
mcbPW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
showPW.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
showPW.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
});
}
private void setDeleteButtonListener() {
Button deleteBtn = (Button) findViewById(R.id.delete_detail_button);
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = getIntent();
Passwords clickedItem = (Passwords) i.getSerializableExtra("clickedItem");
DatabaseManager db = DatabaseManager.getInstance();
db.deletePasswords(clickedItem);
Toast toast = Toast.makeText(getApplicationContext(), "Password Deleted!", Toast.LENGTH_SHORT);
toast.show();
Intent j = new Intent(getApplicationContext(),List.class);
startActivity(j);
}
});
}
private void setEditButtonListener() {
Button editBtn = (Button) findViewById(R.id.update_detail_button);
editBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText txtView_title = (EditText) findViewById(R.id.text_detail_title);
txtView_title.setEnabled(true);
EditText txtView_user = (EditText) findViewById(R.id.text_detail_username);
txtView_user.setEnabled(true);
EditText txtView_pw = (EditText) findViewById(R.id.text_detail_pw);
txtView_pw.setEnabled(true);
EditText txtView_notes = (EditText) findViewById(R.id.text_detail_notes);
txtView_notes.setEnabled(true);
EditText txtView_url = (EditText) findViewById(R.id.text_detail_url);
txtView_url.setEnabled(true);
EditText txtView_expDate = (EditText) findViewById(R.id.text_detail_expDate);
txtView_expDate.setEnabled(true);
}
});
}
private void setSaveButtonListener() {
Button saveButton = (Button) findViewById(R.id.save_detail_button);
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = getIntent();
Passwords clickedItem = (Passwords)intent.getSerializableExtra("clickedItem");
EditText txtView_title = (EditText) findViewById(R.id.text_detail_title);
String title = txtView_title.getText().toString();
EditText txtView_user = (EditText) findViewById(R.id.text_detail_username);
String user = txtView_user.getText().toString();
EditText txtView_pw = (EditText) findViewById(R.id.text_detail_pw);
String pw = txtView_pw.getText().toString();
EditText txtView_notes = (EditText) findViewById(R.id.text_detail_notes);
String notes = txtView_notes.getText().toString();
EditText txtView_url = (EditText) findViewById(R.id.text_detail_url);
String url = txtView_url.getText().toString();
EditText txtView_expD = (EditText) findViewById(R.id.text_detail_expDate);
String expD = txtView_expD.getText().toString();
Passwords aangepastePW = new Passwords();
aangepastePW.setId(clickedItem.getId());
aangepastePW.setTitle(title);
aangepastePW.setUsername(user);
aangepastePW.setPassword(pw);
aangepastePW.setNotities(notes);
aangepastePW.setUrl(url);
aangepastePW.setExpDate(expD);
DatabaseManager db = DatabaseManager.getInstance();
db.updateStudent(aangepastePW);
Toast.makeText(ListDetail.this, "Information Changed!", Toast.LENGTH_SHORT).show();
Intent j = new Intent(getApplicationContext(),List.class);
startActivity(j);
}
});
}
private void initialise() {
Intent intent = this.getIntent();
Passwords clickedItem = (Passwords)intent.getSerializableExtra("clickedItem");
EditText txtView_title = (EditText) findViewById(R.id.text_detail_title);
txtView_title.setText(clickedItem.getTitle());
txtView_title.setEnabled(false);
EditText txtView_username = (EditText) findViewById(R.id.text_detail_username);
txtView_username.setText(clickedItem.getUsername());
txtView_username.setEnabled(false);
EditText txtView_pw = (EditText) findViewById(R.id.text_detail_pw);
txtView_pw.setText(clickedItem.getPassword());
txtView_pw.setEnabled(false);
EditText txtView_notes = (EditText) findViewById(R.id.text_detail_notes);
txtView_notes.setText(clickedItem.getNotities());
txtView_notes.setEnabled(false);
EditText txtView_url = (EditText) findViewById(R.id.text_detail_url);
txtView_url.setText(clickedItem.getUrl());
txtView_url.setEnabled(false);
EditText txtView_expD = (EditText) findViewById(R.id.text_detail_expDate);
txtView_expD.setText(clickedItem.getExpDate());
txtView_expD.setEnabled(false);
}
In the onItemClick of your listview replace the below line
Passwords clickedItem = passwordLijst.get(index);
with
Passwords clickedItem = adapter.getItem(index);
it retrieves the item at the given position in the adapter.
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);
}
}