android - get string from edittext and store it in array - android

I have a
EditText- addArea = (EditText) findViewById(R.id.editTextArea);
and ArrayList - public static ArrayList<String> areaList = new ArrayList<String>();
and a Button- addbtn = (Button) findViewById(R.id.addBTN);
This is my code to Store Strings into array. It displaying the toastMessage but I doesn't know whether its storing Strings or not.
addbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String area = addArea.getText().toString();
if (!area.isEmpty()){
areaList.add(area);
toastmessage(area+" added successfully.");
addArea.setText("");
}
}
});
What is need is, The spinner is in the next layout. When I click the spinner it should diplay the items that i have enterd in the edittext.

To check whether the list is added, you can check by this way
for (int i=0; i<areaList.size();i++){
addArea.append(areaList.get(i));
addArea.append("\n");
}
You can know whether the string is stored into the areaList by checking the textView setText value.
To pass the areaList to next Activity, add this
Intent intent = new Intent(MainActivity.this, secondActivity.class);
intent.putExtra("areaList",areaList);
In the second Activity, use this line to get all the areaList data
ArrayList<String> areaList = (ArrayList<String>)getIntent().getSerializableExtra("areaList");
Then set the areaList to spinner
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, areaList);
spinner.setAdapter(adapter);

In order to see make sure the item is added you can print the array or check its size.
First declare a global variable as int lastSize;
lastSize = 0; //we start from 0
addbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String area = addArea.getText().toString();
if (!area.isEmpty()){
areaList.add(area);
//size has changed, means new item added successfully
if (areaList.size() > lastSize) {
toastmessage(area+" added successfully.");
addArea.setText("");
}
//lets update lastSize to record latest size
lastSize = areaList.size();
}
}
});

Related

Problem in unable to validate spinner must be click

Adapter.class
holder.txt_AddToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String product_image = brandWiseProductArrayList.get(position).getProductImgPath();
int qty = Integer.parseInt(holder.edtxt_integer_number.getText().toString());
}
});
Here, When I am click this button must be asked to select spinner
Is there any way to validate spinner or put "select something" in the first position of a spinner which is filled by objArraylist
ProductSpinnerAdapter productSpinnerAdapter = new ProductSpinnerAdapter(context, brandWiseProductArrayList.get(position).getProductDetailsArrayList());
holder.spinner_product_details.setAdapter(productSpinnerAdapter);
Try this
String product_image = brandWiseProductArrayList.get(holder.spinner.getSelectedItemPosition()).getProductImgPath();
change holder.spinner to respective spinner variable.
Let me know if this works

combining 2 activities together

ok so I'm making an app where I use an edit text to write anything and when I press a button it adds whatever I wrote in the edit to a listView, and it works, but I want the List to be in a different activity than the button and edit text, so I moved it without changing the code.can anyone figure this out,
BTW all the variables are public.
public class MainActivity extends AppCompatActivity {
public ArrayList<String> arrayList2;
public ArrayAdapter<String> adapter,adapter2;
public EditText editText,editText2;
public ArrayList<String> itemList,itemList2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] items = {};
itemList = new ArrayList<String>(Arrays.asList(items));
adapter = new ArrayAdapter<String>(this, R.layout.activity_list_layout,R.id.txtItem,itemList);
ListView listV = (ListView)findViewById(R.id.list_layout);
listV.setAdapter(adapter);
editText = (EditText)findViewById(R.id.thingadd);
Button btAdd = (Button)findViewById(R.id.add);
String[] age = {};
arrayList2 = new ArrayList<String>(Arrays.asList(age));
itemList2 = new ArrayList<String>(Arrays.asList(age));
adapter2 = new ArrayAdapter<String>(this, R.layout.activity_list__layout2,R.id.txtage,itemList2);
ListView listV2 = (ListView)findViewById(R.id.Age);
listV2.setAdapter(adapter2);
editText2 = (EditText)findViewById(R.id.agetxt);
btAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newItem=editText.getText().toString();
String newItem2=editText2.getText().toString();
itemList.add(newItem);
itemList2.add(newItem2);
adapter.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
}
});
}
}
You can use SQLite Database for that, where you would be saving data in database from one activity and reading it and displaying it in listview from another.
You have 2 options to do so.
You can use any database service. Store the values and display them in the next activity. But this needs Internet service to be enabled in the phone.
You can use intent. Convert those input to string type and pass them to next activity and display them using ids. This type do not require Internet service. To know more about this, Visit this link

Android: ListView displays only last values saved in an array

hi i have tried the following code to store the user input values of EditText to an array in another activity and display the values in the array. every time the user input values in EditText of Activity01. it should be stored in an array on the Activity02 to display all the values of user inputs.
The ListView displays one last entered value from Array.
This is the code what i have tried.
Activity01.java
Button GrmBtnSave = (Button) findViewById(R.id.grmbtnsave);
Grmitem = (AutoCompleteTextView) findViewById(R.id.itemsearch);
GrmBtnSave.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent = new Intent ( ItemSearch.this, MainInvoiceActivity.class );
intent.putExtra("GRM_ITEM", Grmitem.getText().toString());
startActivity(intent);
}
}
});
Activity02.java
ListView lv1;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
protected void onCreate(Bundle savedInstanceState) {
//.....
//.....
lv1=(ListView)findViewById(R.id.getalllist);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
lv1.setAdapter(adapter);
String val="";
if(getIntent().getStringExtra("GRM_ITEM")!=null && getIntent().getStringExtra("GRM_ITEM").length() > 0){
val = getIntent().getStringExtra("GRM_ITEM");
list.add(val);
}
adapter.notifyDataSetChanged();
}
I tried solving it but still i get the last entered value in the EditText.
Thank you for help
The static keyword will do the magic.
Make change in declaration of list in your activity02.java like this. I have tested it. and it works.
static ArrayList<String> list = new ArrayList<String>();

Updating the ListView

I'm working on a program that uses a ListView which contains strings that a user enters. I want it to list out each string after the user clicks the button. Also I want every string value that is displayed in the ListView to also be in an array so that I can manipulate the list later. This is what I have right now:
public ArrayList<String> choices = new ArrayList<String>();
public String[] choicesArray;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.listView1);
choicesArray = new String[] { "You have not entered a choice yet" };
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
choicesArray);
listView.setAdapter(adapter);
final Button addButton = (Button) findViewById(R.id.Button1);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
adapter.add(message);
//choices.add(message);
//choicesArray = choices.toArray(choicesArray);
//listView.setAdapter(adapter);
}
});
}
This code creates an error:
FATAL EXCEPTION: main
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:404)
at java.util.AbstractList.add(AbstractList.java:425)
at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
at MainActivity$1.onClick(MainActivity.java:38)
The reason your list is not updating is that you are not ever adding your data items to the Adapter. You are only adding them to choicesArray which is the array that you passed to the new ArrayAdapter() constructor.
That constructor does not tie the adapter to the array, but basically copies the array into the adapter (at the time that you call the constructor) Changes made to the array after that will not be reflected in the ArrayAdapter.
I like to think of it that ArrayAdapter<> is basically the same thing as ArrayList<> with the added bonus of being able to generate child views for some AdpaterView parent. You can pass it an array[] to start with and it will initialize itself with the values in that array, but it does not link the array to the to the Adapter, or to the ListView. After that initial constructor call any changes to either one will not be reflected in the other.
So you should change your code to look like this:
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
adapter.add(message);
//adapter.notifyDataSetChanged(); //<-- you might need to call this, but I think it might work without if you do it this way.
//listView.setAdapter(adapter); //<-- you shouldn't need to call this each time. Once at the begining is enough.
}
});
Edit:
change your ArrayAdapter constructor and remove the choiceArray parameter so you should be left with this:
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1);
after that I think you should be good to go. Essentially the problem was that since you were passing in an array, and since arrays are of a fixed size (yours was length = 1) it was not allowing you to add anything. I suspect you could probably use an ArrayList<String> instead of String[] if you wanted, but honestly for your situation I think it will be easier to just skip that parameter all together and add items directly to your adapter whenever you need to add new ones.
after adding the new String to your list you just use notifyDataSetChanged() of your adapter which tells the adapter to update the display and it'll update the ListView with the new string
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
choices.add(message);
choicesArray = choices.toArray(choicesArray);
adapter.notifyDataSetChanged();
}
});
Update
you should consider making this change too
choicesArray = new String[choices.size()];
choicesArray = choices.toArray(choicesArray);

Android - Editing ListAdapter Data from another activity

I have a simple application that has 3 activities. The third activity is a ListActivity that lists movie titles and their respective gross and year, choosing any movie will display the values in the first activity. The second activity is where the user can add a movie to the ListActivity by filling up EditText fields and this is where I'm having problems.
So far, the examples I see involve use this line of code:
SampleCustomAdapter adapter = new SampleCustomAdapter(results);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
However, this line of code doesn't work for my case since the class in the second activity extends Activity and NOT ListActivity.
So far, this is the code I have when I attempt to add the new entries to the List.
//if all edit text fields have values and are valid inputs
if( titleFlag == 1 && grossFlag == 1 && yearFlag == 1){
//fix stuff here
//TODO ADD THE NEW MOVIE TO THE ARRAY IN STRINGS.XML
ArrayList<MyMovies> movieList = new ArrayList<MyMovies>();
MyMovies newMovie = new MyMovies();
newMovie.setMovie(title);
newMovie.setGross(gross);
newMovie.setYear(year);
movieList.add(newMovie);
//go back to the main page after adding
Intent intent = new Intent(this, com.Android.Lab7.Lab7_084106.class);
startActivity(intent);
finish();
}
I also tried adding the adapter.notifyDataSetChanged(); in the third activity after generating the list but to no avail. Oh, the third activitys' onCreate looks something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
//create stuff
super.onCreate(savedInstanceState);
ArrayList<MyMovies> movieList = new ArrayList<MyMovies>();
String[] movieArray = getResources().getStringArray(R.array.movieArray);
String[] grossArray = getResources().getStringArray(R.array.worldwideGross);
String[] yearArray = getResources().getStringArray(R.array.yearArray);
ArrayList<MyMovies> results = new ArrayList<MyMovies>();
// make sure the arrays have the same length
for (int i = 0; i < movieArray.length; i++) {
MyMovies sr = new MyMovies();
sr.setMovie(movieArray[i]);
sr.setGross(grossArray[i]);
sr.setYear(yearArray[i]);
results.add(sr);
}
SampleCustomAdapter adapter = new SampleCustomAdapter(results);
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
adapter.notifyDataSetChanged();
//set stuff such that Page2 sends back a result to page 1
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
sendResult(position);
}
});
}
I used the strings resource to store the array of titles, gross, and year since there's a lot of them and it's not practical that I hardcode them.
So basically I'm stuck on how I can update the List in the third activity from the second activity.
Declare this in the third Activity.
public static String title, gross, year;
public static boolean newMovieAdded=false;
Then when you are in the second activity do the following
thirdActivity.title = MyEditText1.getText().toString();
thirdActivity.gross= MyEditText2.getText().toString();
thirdActivity.year= MyEditText3.getText().toString();
if(!year.equals(""))
newMovieAdded=true;
thats all. when you go to the third activity just write your code
ArrayList<MyMovies> movieList = new ArrayList<MyMovies>();
if(newMovieAdded){
MyMovies newMovie = new MyMovies();
newMovie.setMovie(title);
newMovie.setGross(gross);
newMovie.setYear(year);
movieList.add(newMovie);
newMovieAdded = false;
}

Categories

Resources