I can't make the spinner write to the database. I read the docs about spinner, but I'm definitely doing something wrong. Can someone give some advice?
I've read many tutorials but I'm getting confused. How can I make the spinner write to database in the onClick in which I have already on input and want to add 1 more including the spinner.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class AddActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
DatabaseHelper mylogsDB;
Button btn_save;
EditText etHours;
Spinner etShift;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
etHours = (EditText) findViewById(R.id.editText);
etShift = (Spinner) findViewById(R.id.spinner);
btn_save = (Button) findViewById(R.id.btn_save);
mylogsDB = new DatabaseHelper(this);
AddData();
// Spinner click listener
etShift.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<>();
categories.add("Πρωί");
categories.add("Μεσημέρι");
categories.add("Νύχτα");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
etShift.setAdapter(dataAdapter);
}
public void onItemSelected (AdapterView <?> parent, View view,int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Έχετε επιλέξει: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public void AddData() {
btn_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String hours = etHours.getText().toString();
String shift = etShift.getSelectedItem().toString();
boolean insertData = mylogsDB.addData(hours, shift);
if (insertData == true) {
Toast.makeText(AddActivity.this, "Η αποθήκευση ολοκληρώθηκε επιτυχώς.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(AddActivity.this, "Ώχ,Κάτι πήγε στραβά.", Toast.LENGTH_LONG).show();
}
}
});
}
}
You need to keep the selected value from the Spinner and then use it in the method of which you use to insert into database
Also you should call AddData(); in your btn_save onClick event !
public class AddActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
DatabaseHelper mylogsDB;
Button btn_save;
EditText etHours;
Spinner etShift;
private String spinnerSelectedItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
etHours = (EditText) findViewById(R.id.editText);
etShift = (Spinner) findViewById(R.id.spinner);
btn_save = (Button) findViewById(R.id.btn_save);
mylogsDB = new DatabaseHelper(this);
btn_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddData();
}
});
// Spinner click listener
etShift.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<>();
categories.add("Πρωί");
categories.add("Μεσημέρι");
categories.add("Νύχτα");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
etShift.setAdapter(dataAdapter);
}
public void onItemSelected (AdapterView <?> parent, View view,int position, long id) {
// On selecting a spinner item
spinnerSelectedItem = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Έχετε επιλέξει: " + spinnerSelectedItem, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public void AddData() {
String hours = etHours.getText().toString();
//spinnerSelectedItem should have a value
if(!hours.equals("") && !spinnerSelectedItem.equals("")){
boolean insertData = mylogsDB.addData(hours, spinnerSelectedItem);
if (insertData == true) {
Toast.makeText(AddActivity.this, "Η αποθήκευση ολοκληρώθηκε επιτυχώς.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(AddActivity.this, "Ώχ,Κάτι πήγε στραβά.", Toast.LENGTH_LONG).show();
}
}else{
//do something
}
}
}
Related
My problem is that my code does not react accordingly whenever an user selects an item from an AutoCompleteTextView.
flag is a variable which is set to a value whenever one item from each AutoCompleteTextView has been selected. If it's set to 1, then it means it's right and it should proceed to main activity. Otherwise, a toast is displayed on click of button whose onClick calls the method callMainActivity.
There are no errors. Gradle build is successful, but clicking on that button (mentioned above) does nothing at all.
Code:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.Arrays;
import java.util.List;
public class Location extends AppCompatActivity {
private static int flag=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
int city = android.R.layout.simple_dropdown_item_1line;
int area = android.R.layout.simple_dropdown_item_1line;
int store = android.R.layout.simple_dropdown_item_1line;
String []city_array = getResources().getStringArray(R.array.City);
String []area_array= getResources().getStringArray(R.array.Area);
String []store_array= getResources().getStringArray(R.array.Store);
List<String> city_list= Arrays.asList(city_array);
List<String> area_list= Arrays.asList(area_array);
List<String> store_list= Arrays.asList(store_array);
ArrayAdapter<String> adapter_city = new ArrayAdapter(this,city, city_list);
ArrayAdapter<String> adapter_area = new ArrayAdapter(this, area, area_list);
ArrayAdapter<String> adapter_store = new ArrayAdapter(this, store, store_list);
final AutoCompleteTextView autocompleteView_city =
(AutoCompleteTextView) findViewById(R.id.City);
final AutoCompleteTextView autocompleteView_area =
(AutoCompleteTextView) findViewById(R.id.Area);
final AutoCompleteTextView autocompleteView_store =
(AutoCompleteTextView) findViewById(R.id.Store);
autocompleteView_area.setAdapter(adapter_area);
autocompleteView_city.setAdapter(adapter_city);
autocompleteView_store.setAdapter(adapter_store);
autocompleteView_area.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View arg0) {
autocompleteView_area.showDropDown();
if(autocompleteView_area.getListSelection()!= ListView.INVALID_POSITION)
flag=1;
else
flag=0;
}
});
autocompleteView_city.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View arg0) {
autocompleteView_city.showDropDown();
if(autocompleteView_area.getListSelection()!= ListView.INVALID_POSITION)
flag=1;
else
flag=0;
}
});
autocompleteView_store.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View arg0) {
autocompleteView_store.showDropDown();
if(autocompleteView_area.getListSelection()!= ListView.INVALID_POSITION)
flag=1;
else
flag=0;
}
});
//This is the newly updated part
autocompleteView_area.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
//... your stuff
if(autocompleteView_area.getListSelection()>0) {
flag = 1;
System.out.println(flag + "flag at area");
}else
flag=0;
}
});
}
public void callMainActivity(View view){
if(flag==1) {
Intent in = new Intent(getBaseContext(), MainActivity.class);
startActivity(in);
}
else
Toast.makeText(getBaseContext(),"Please select all fields properly",Toast.LENGTH_LONG);
}
}
The reason you are not seeing the Toast or changing activities, is because you are never calling callMainActivity(View view) in your code. Add this line to the end of all your OnClickListeners: callMainActivity(arg0) -- if this does not work, put some log statements in your OnClickListeners to check if they are triggering or not.
Also, if you want to trigger the call when an item from your AutoCompleteTextView result list is selected, you should use an AdapterView.OnItemClickedListener instead. This will notify you when an item is selected from the AutoCompleteTextView list, or when nothing is selected and then you can react accordingly.
my code is working and I am getting the value on textview but its not changing on first click, suppose my spinner pops up and I select other value than at the same time my textview value doesn't changes it changes on the next click.
package com.vedicrishiastro.kundli.Screens.Extras;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import com.vedicrishiastro.kundli.R;
import com.vedicrishiastro.kundli.Screens.AbstractActivity;
public class Settings extends AbstractActivity implements View.OnClickListener {
private LinearLayout linearSelectLang,linearSetDefault,linearSelectPanch;
private TextView txtSelectLang,txtSetDefault,txtSelectPanch;
private Spinner spinner1,spinner2,spinner3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
linearSelectLang = (LinearLayout)findViewById(R.id.LinearSelectLang);
linearSetDefault = (LinearLayout)findViewById(R.id.LinearSetDefault);
linearSelectPanch = (LinearLayout)findViewById(R.id.LinearSelectPanch);
spinner1 = (Spinner)findViewById(R.id.settingSpinner1);
spinner2 = (Spinner)findViewById(R.id.settingSpinner2);
spinner3 = (Spinner)findViewById(R.id.settingSpinner3);
txtSelectLang = (TextView)findViewById(R.id.selectLangtext);
txtSetDefault = (TextView)findViewById(R.id.setdefaulttext);
txtSelectPanch = (TextView)findViewById(R.id.selectPanchtext);
linearSelectLang.setOnClickListener(this);
linearSetDefault.setOnClickListener(this);
linearSelectPanch.setOnClickListener(this);
spinner1.setVisibility(View.GONE);
spinner2.setVisibility(View.GONE);
spinner3.setVisibility(View.GONE);
}
public void onClick(View view){
int id = view.getId();
switch (id){
case R.id.LinearSelectLang:
{
spinner1.performClick();
String text = spinner1.getSelectedItem().toString();
txtSelectLang.setText(text);
spinner1.setVisibility(View.GONE);
}
break;
case R.id.LinearSetDefault:
{
spinner2.performClick();
String text = spinner2.getSelectedItem().toString();
txtSetDefault.setText(text);
spinner2.setVisibility(View.GONE);
}
break;
case R.id.LinearSelectPanch:
{
spinner3.performClick();
String text = spinner3.getSelectedItem().toString();
txtSelectPanch.setText(text);
spinner3.setVisibility(View.GONE);
}
break;
}
}
}
I tried this but it isn't working
case R.id.LinearSelectLang:
{
spinner1.performClick();
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0)
{
txtSelectLang.setText("English");
}
else
{
txtSelectLang.setText("हिंदी");
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
break;
How about you use the spinner's OnItemSelected event?
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Thing is, if you call performClick, the spinner pops out, but this call is not blocking. So you need the OnItemSelectedListener to get an async response with the input made by the user.
Calling getSelectedItem right after performClick (which opens the spinner?) will return the previously set element - which is the error you are facing.
Create an onItemSelectListener for your Spinner and change the text every time an item is selected.
spinner.setOnItemSelectedListener(new OnItemSelectListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
// DO it here
}
});
I got an activity that is called from main activity. In the activity, i got database connections established and i fill a spinner according to db data. When a user selects one of the spinner items and press the button, the activity finishes and returns a value to the main activity (activity on result). But the button in the activity doesnt work, so the activity never returns a value to main activity. Here is the code:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class mySpinnerClass extends Activity implements
OnItemSelectedListener {
private VeriTabani veritabani;
private SQLiteDatabase db;
Spinner spinner;
Button btnAdd;
String dataBaseName;
int selectedValue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
dataBaseName = "";
selectedValue = -1;
Intent i = getIntent();
dataBaseName = i.getStringExtra("dataBaseName");
veritabani = new VeriTabani(this);
db = veritabani.getWritableDatabase();
// Spinner element
spinner = (Spinner) findViewById(R.id.mySpinner);
// add button
btnAdd = (Button) findViewById(R.id.addButton);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Loading spinner data from database
loadSpinnerData();
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (dataBaseName == "phoneOrientation") {
Intent intent = getIntent();
intent.putExtra("phoneOrientation", selectedValue);
setResult(RESULT_OK, intent);
finish();
}
else if(dataBaseName== "phonePosition") {
Intent intent = getIntent();
intent.putExtra("phonePosition", selectedValue);
setResult(RESULT_OK, intent);
finish();
}
else if(dataBaseName =="Label"){
Intent intent=getIntent();
intent.putExtra("label",selectedValue);
setResult(RESULT_OK, intent);
finish();
}
}
});
}
private void loadSpinnerData() {
// Spinner Drop down elements
List<String> lables = getAllLabels();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
public List<String> getAllLabels() {
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + dataBaseName;
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(cursor.getColumnIndex("isim")));
} while (cursor.moveToNext());
}
// closing connection
// returning lables
return labels;
}
public int returnID(String item) {
int toBereturnedID;
Cursor myCursor = db.rawQuery("SELECT _id from " +dataBaseName+ " WHERE isim="+"'"+item+"'", null);
myCursor.moveToFirst();
toBereturnedID = myCursor.getInt(0);
return toBereturnedID;
}
public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
String label = parent.getItemAtPosition(position).toString();
selectedValue = returnID(label);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
You are calling loadSpinnerData(); method in onCreate() of activity so dataBaseName is always going to be empty, so you want to have value of spinner at time click you need to call that with on onClick() method of button, which will give you correct dataBaseName and use dataBaseName.equals("phoneOrientation") instead of dataBaseName == "phoneOrientation"
Try creating a new Intent instead of getIntent().
Intent intent=new Intent();
intent.putExtra("label",selectedValue);
setResult(RESULT_OK, intent);
finish();
Hope it helps.
I have a spinner that show my array data list and delete button.
What I am trying to do is when I click on the delete button, it automatically deletes a selected spinner value, but I'm not certain how to do this.
In the delete button click function, deleted the selected spinner value after debugging my activity again but I want delete automatically a selected spinner value when I click on the delete button.
package quesansw.the1;
import java.util.ArrayList;
import android.app.Activity;
import android.app.Dialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class View1 extends Activity {
SQLiteDatabase db;
ArrayAdapter adapter;
private String array_spinner[];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = openOrCreateDatabase("mydatabase.db",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
final Dialog d1 = new Dialog(this);
Window window = d1.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
//d1.setTitle("Login");
d1.setContentView(R.layout.view);
d1.show();
Button Click = (Button) d1.findViewById(R.id.Click);
Button Save = (Button) d1.findViewById(R.id.Save);
Button Delete = (Button) d1.findViewById(R.id.Delete);
ArrayList<String> list = new ArrayList<String>();
Cursor cursor = db.rawQuery("select * from records", null);
list.add("");
if (cursor.moveToFirst())
{
do
{
list.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
/*array_spinner=new String[20];
array_spinner[0]=list.get(0);*/
Spinner s = (Spinner) d1.findViewById(R.id.tittle_spinner);
adapter = new ArrayAdapter<Object>(this,android.R.layout.simple_spinner_item, list.toArray());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
/*Save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Spinner s = (Spinner) d1.findViewById(R.id.tittle_spinner);
String str = s.getSelectedItem().toString();
System.out.println("********"+str);
Cursor cur1=db.rawQuery("select * from records where tittle='"+str+"' ",null);
cur1.moveToNext();
String str1=cur1.getString(1);
EditText ans = (EditText) d1.findViewById(R.id.text);
ans.setText(str1);
}
});*/
Delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Spinner s = (Spinner) d1.findViewById(R.id.tittle_spinner);
String str = s.getSelectedItem().toString();
db.execSQL("delete from records where tittle='"+str+"' ");
}
});
Click.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Spinner s = (Spinner) d1.findViewById(R.id.tittle_spinner);
String str = s.getSelectedItem().toString();
System.out.println("********"+str);
Cursor cur1=db.rawQuery("select * from records where tittle='"+str+"' ",null);
cur1.moveToNext();
String str1=cur1.getString(1);
EditText ans = (EditText) d1.findViewById(R.id.text);
ans.setText(str1);
}
});
}
}
1) Here is my activity screen shot
2) Another screen shot with spinner values:
Why are you declaring and initializing your Spinner so many times? Just do it once
public class View1 extends Activity {
SQLiteDatabase db;
ArrayAdapter adapter;
private String array_spinner[];
Spinner spinner; // Declare your spinner here
then in onCreate()
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = openOrCreateDatabase("mydatabase.db",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
spinner = (Spinner) d1.findViewById(R.id.tittle_spinner); //initialize your spinner here
final Dialog d1 = new Dialog(this);
then when you click delete button call adapter.notifyDataSetChanged() to update your Array and spinner
In deleteButton's onClickListener, remove the selected spinner value from the array data list and call onnotifydatasetChanges or again setSpinner adapter with the new array list after deletion.
public class MainActivity extends Activity {
Spinner spin;
Button delete;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
delete = (Button) findViewById(R.id.button1);
spin = (Spinner) findViewById(R.id.spinner1);
final ArrayList<String> spinneritems = new ArrayList<String>();
spinneritems.add("item 1");
spinneritems.add("item 2");
spinneritems.add("item 3");
spinneritems.add("item 4");
spinneritems.add("item 5");
final ArrayAdapter<String> adp = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinneritems );
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adp);
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String selectedSpinner = spin.getSelectedItem().toString();
spinneritems.remove(selectedSpinner);
adp.notifyDataSetChanged();
}
});
}
}
}
On delete button click listener add following code
Spinner s = (Spinner) d1.findViewById(R.id.tittle_spinner);
String str = s.getSelectedItem().toString();
adapter.remove(str);
adapter.notifyDataSetChanged();
s.setSelection(0);
Something is wrong with my code. In the application, when I "add item", it doesn't show anything, and if I am clicking somewhere around the Android application, then "item" sometimes comes.
Can somebody help me?
package com.example.proov;
import java.util.ArrayList;
import com.example.proov.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class proovin extends Activity {
private ListView LView;
ArrayList <String>ar = new ArrayList<String>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LView = (ListView) findViewById(R.id.ListView01);
// Set option as Multiple Choice. So that user can able to select more the one option
LView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, ar));
LView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Button b = (Button) findViewById(R.id.add_item);
final EditText d = (EditText) findViewById(R.id.title);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ar.add(d.getText().toString());
}
});
}
}
Use below code instead of your code.
public class proovin extends Activity {
private ListView LView;
ArrayList <String>ar = new ArrayList<String>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LView = (ListView) findViewById(R.id.ListView01);
// Set option as Multiple Choice. So that user can able to select more
// the one option
final ArrayAdapter<String> adpt=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, ar);
LView.setAdapter(adpt);
LView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Button b = (Button) findViewById(R.id.add_item);
final EditText d = (EditText) findViewById(R.id.title);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ar.add(d.getText().toString());
adpt.setNotifyOnChange(true);
LView.setAdapter(adpt);
}
});
}
}
The item is likely getting added to the ArrayList, but that is different from getting added to the ListView. You need to tell the ListView that you updated the data model so that it knows to look. See ArrayAdapter.notifyDatasetChanged()
you can use this add string to list on a button click
final String a[]={"hello","world"};
final ArrayAdapter<String> at=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,a);
final ListView sp=(ListView)findViewById(R.id.listView1);
sp.setAdapter(at);
final EditText et=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
int k=sp.getCount();
String a1[]=new String[k+1];
for(int i=0;i<k;i++)
a1[i]=sp.getItemAtPosition(i).toString();
a1[k]=et.getText().toString();
ArrayAdapter<String> ats=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,a1);
sp.setAdapter(ats);
}
});