AutoCompleteTextView wont work as EditText did - java

I replaced an EditText with an AutoCompleteTextView in my app to make things a little more user friendly, but I am having an error.
In the app, the user types in the name of a plant, and then clicks a button to be taken to a new activity where some information about the plant is displayed.
The error: the app crashes after I press the button to be taken to the next activity after the user types in the name of the plant. This didnt happen when I still had an EditText. Perhaps I am using the AutoCompleteTextView wrong?
Heres the relevant code:
public class Main2Activity extends AppCompatActivity {
AutoCompleteTextView edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//this is the AutoComplete
AutoCompleteTextView edit = (AutoCompleteTextView) findViewById(R.id.et_item);
//this is the list of suggestions for the AutoComplete
String[] items = getResources().getStringArray(R.array.items_array);
java.util.Arrays.sort(items);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
edit.setAdapter(adapter);
//this is the method that is called when the button is pressed
public void find(View view) {
String name = edit.getText().toString();
//basically, whatever is typed into the AutoComplete is turned into a string, and
//if the string matches one of the existing plants, the user is taken to
//the next activity
if(name.equalsIgnoreCase("Sunflower")){
Intent intent = new Intent(this, Sunflower.class);
startActivity(intent);
}
else if(name.equalsIgnoreCase("Cactus")){
Intent intent = new Intent(this, Cactus.class);
startActivity(intent);
}
Can anyone see why this does not work?

Change below:
AutoCompleteTextView edit = (AutoCompleteTextView) findViewById(R.id.et_item);
To:
edit = (AutoCompleteTextView) findViewById(R.id.et_item);
Your autocomplete textview scope is limited to onCreate() and
edit.getText().toString();
You are trying to get text from it which is not initialized yet. So it will get null pointer exception.

Related

How to add a new varible (from previous activity) to a list to display on listview in android studio?

public class Main2Activity extends AppCompatActivity {
ListView simpleList;
String[] thoughtList = {" "};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
String newThought = intent.getStringExtra(MainActivity.EXTRA_TEXT);
thoughtList.add(newThought); //error comes herer hich wont allow add
simpleList = (ListView)findViewById(R.id.simpleListView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
simpleList.setAdapter(arrayAdapter);
}
}
that is my code on second page it brings over varible (saved as newThought) from the previous activity which i then want to add to thoughtList to display in ListView but won't let me add the new varible to the list. does my code need tweaking or should i start again to achieve varible brougth over from previous activity to add into list view on activity 2
Have you tried using an ArrayList instead of an string array?
String array has a fixed size when array list does not.
That may be your problem.

How to read context of two spinners and send both to next activity?

I have two spinners and I want to grab the values that the user chooses and send it to the next activity. My first activity is titled IO and I create the spinners and get the data from the selections in my onCreate.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_io);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Spinner locationSpinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(IO.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.busStops));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
locationSpinner.setAdapter(myAdapter);
location = locationSpinner.getSelectedItem().toString();
Spinner destinationSpinner = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> myAdapter2 = new ArrayAdapter<String>(IO.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.busStops));
myAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
destinationSpinner.setAdapter(myAdapter2);
destination = destinationSpinner.getSelectedItem().toString();
}
I tried sending the data in another method titled sendRoutes where I created an intent but it didn't work and I was wondering how to do this.
Assuming that you want to open your activity on a button click then,
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String destination = destinationSpinner.getSelectedItem().toString();
String location = locationSpinner.getSelectedItem().toString();
Intent intent = new Intent(CurrentActivity.this,NextActivity.class);
intent.putExtra("destination",destination);
intent.putExtra("location",location);
startActivity(intent);
}
});
You are passing destination and location values to NextActivity by intent.
in IO Activity definition Intent :
Intent intent = new Intent(IO.this,SecondActivity.class);
location = locationSpinner.getSelectedItem().toString();
destination = destinationSpinner.getSelectedItem().toString();
intent.putExtra(KEY_DEST,destination);
intent.putExtra(KEY_LOC,location);
startActivity(intent);
in SecondActivity get two parameters in onCreate :
String destination = getInetent().getExtras.getString(KEY_DEST);
String location = getInetent().getExtras.getString(KEY_LOC);

Adding items in ListView

I have problems with my list . I had an Activity I get some Data from.
The data must accessed in variables in my class :
Mylist.java class
public String Job,Ship,Location,Shift,Date;
public String workingHour;
Mylist(String job,String loc,String shp,String shft,String dat,String wh){
Job=job;
Location=loc;
Ship=shp;
Shift=shft;
Date=dat;
workingHour=wh;
}
and by pressing Save Button The list must add an item.My problem is when I add another items the list have only one item How to fix this ?
Adding Class :
AddOredit.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_oredit);
final ArrayList<Mylist>list =new ArrayList<>();
final ArrayList<String> Job=new ArrayList<>();
String selecteditem1, selecteditem2,getlocation,getship,getdate,gethours;
Spinner type=(Spinner)findViewById(R.id.s1);
Spinner shift=(Spinner)findViewById(R.id.s2);
EditText location=(EditText)findViewById(R.id.loc);
EditText ship=(EditText)findViewById(R.id.ship);
EditText date=(EditText)findViewById(R.id.date);
EditText workinghours=(EditText)findViewById(R.id.hours);
Button Save = (Button) findViewById(R.id.editbtn);
Button cancel = (Button) findViewById(R.id.cancelbtn);
selecteditem1 = type.getSelectedItem().toString(); //for first spinner
selecteditem2 = shift.getSelectedItem().toString();//for second spinner
getlocation=location.getText().toString();
getship=ship.getText().toString();
getdate=date.getText().toString();
gethours=workinghours.getText().toString();
list.add(new Mylist(selecteditem1,getlocation,getship,selecteditem2,getdate,gethours));
Log.e(TAG, String.valueOf(list.size()));
for(int i=0;i<list.size();i++){
Job.add("Job "+(i+1));
}
//When pressing save Button
Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//list.add(new Mylist(selecteditem1, getlocation, getship, selecteditem2, getdate, gethours));
Intent intent = new Intent(AddOredit.this, MainActivity.class);
intent.putStringArrayListExtra("MyList", Job);
startActivity(intent);
}
});
}
MainActivity.java this class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv= (ListView) findViewById(R.id.listView);
Button ADD=(Button)findViewById(R.id.addbtn);
Button DELETE=(Button)findViewById(R.id.deletebtn);
Button PROFILE=(Button)findViewById(R.id.profilebtn);
Intent intent=getIntent();
ArrayList<String> Job ;
Job=intent.getStringArrayListExtra("MyList");
if(Job!=null){
ListAdapter adapter= new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Job);
lv.setAdapter(adapter);
//when user click on add button
}
ADD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//go to AddOreditpage
Intent i = new Intent(MainActivity.this, AddOredit.class);
startActivity(i);
}
});
**Note :- ** Add Button is for Adding items in the list and Save Button is for saving data entered at AddOredit class.
**Note :- ** By clicking Add Button , getting to the AddOredit Activity .
By Clicking Save Button, getting back to the main activity with the item added
You are assigning a new List for list object on your second activity created. Try to assign the list with an existing object and add your new object.
EDIT:
Try to Declare this globally as,
private static ArrayList list =new ArrayList<>();
and with in onCreate() remove it. Let me know if it still do not work.

Android Program (Adding items to ListView in another activity)

I am curious how I would go about adding an an editText variable to a list in another activity. I have created an arrayList that names are stored into as they are entered in the editText line. These are stored in the nameList variable in the class StartingActivity. How would i go about adding and showing these names into a ListView in another activity and class called ListActivity as shown. Thank you. Also, how do i refractor the names of classes and files without it just basically not making my program runable.
Code:
Class StartingActivity:
public void onClick_btnConfirm {
EditText editName = (EditText) findViewById(R.id.editName);
TextView textOutput = (TextView) findViewById(R.id.textOutput);
Intent intentList = new Intent(StartingActivity.this, ListActivity.class);
if(editName.length()!=0) {
nameList.add(editName.getText().toString());
editName.setText("");
}
//intentList.putExtra("Name", editName.toString());
startActivity(intentList);
}
Class ListActivity:
public class ListActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
ListView listView = (ListView) findViewById(R.id.listAthletes);
final StartingActivity nameList = new StartingActivity();
nameList.getNameList();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, nameList.getNameList());
listView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
You cannot run findviewbyid() for an id in another Activity.This answer might be of help to you. If that is not what you really want to go with, you can do two things:
Send Intents with the string.
Use a sharedPreference and write the string to a sharedPreference. SharedPreferences also have an OnChangeListener, so you will be able know if the preference has changed. In the onResume() of that activity , you can just read from the sharedPreference, and update the UI
Also take a look at AsyncTask here
As mentioned , you can also use the SQLite Database

Using Intent in Android

I am currently learning Android app development and I am a bit confused on how to use Intent. I am trying to make a "To Do list" app. My problem right now is that I want to be able to tap the item in my to do list to go to a Edit Item page.
Here is what I have so far.
ToDoActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
etNewItem = (EditText) findViewById(R.id.etNewItem);
lvItems = (ListView) findViewById(R.id.lvItems); // now we have access to ListView
readItems(); // read items from file
todoAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); //create adapter
lvItems.setAdapter(todoAdapter); // populate listview using the adapter
setupListViewListener();
setupEditItemListener();
}
The activity I want to launch is called EditItemListener. These are the two functions that I am playing Intent with. Right now I am just testing how to display the EditItemActivity.
private void launchEditItem() {
Intent i = new Intent(this, EditItemActivity);
startActivity(i);
}
private void setupEditItemListener() { // on click, run this function to display edit page
lvItems.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
launchEditItem();
}
});
}
You should specify it as a class
Intent i = new Intent(this, EditItemActivity.class);
Also if you want to kill the current activity
use finish() after StartActivity()
on a listview set the OnItemClickListener and on the listener do this
Intent i = new Intent(this, YourActivity.class); //where you are (this) and where you go (YourAcitivity.class)
startActivity(i); //Now GO!
The intent will start another activity, this activity must be declared on AndroidMainfest.xml

Categories