I want to get EditText value in ListView - android

I have implemented Listview. I can see my string array inputs in the list view. There is one Edittext box now I want to add value in editText Box everytime addbutton is click it should be seen on the listview later on I have to retrieve the whole list or multiple selection list
I am stuck at how to add the edittext value to the string. Below is the code Please tell me why on clicking Addbutton the edittext is not showing up in the list and how it will refresh everytime I add ? ?
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.multi);
ListItem=new ArrayList<String>();
ListItem.add("000");
ipList=(ListView) findViewById(R.id.ListItem);
AddButton = (Button) findViewById(R.id.Add_Button);
AddItem.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ListIP.add(getText(R.id.Multi_Add_EditText).toString());
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,IP);

Just call
adapter.notifyDataSetChanged();
after
ListIP.add(getText(R.id.Multi_AddIP_EditText).toString());
to see the new value in the list.

Related

Save more than one item from ListView using SharedPreferences

I am trying to make a simple todo list app which will probably store only a few things.
After adding items in the ListView, and closing the app, the only entry that loads is the last one that is entered. How do I make all the entered entries show up after closing the app?
Code:
public class MainActivity extends ActionBarActivity {
EditText display;
ListView lv;
ArrayAdapter<String> adapter;
Button addButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (EditText) findViewById(R.id.editText1);
lv = (ListView) findViewById(R.id.listView1);
addButton = (Button) findViewById(R.id.button1);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);
LoadPreferences();
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String task = display.getText().toString();
adapter.add(task);
adapter.notifyDataSetChanged();
SavePreferences("LISTS", task);
}
});
}
protected void SavePreferences(String key, String value) {
// TODO Auto-generated method stub
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = data.edit();
editor.putString(key, value);
editor.commit();
}
protected void LoadPreferences(){
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
String dataSet = data.getString("LISTS", "None Available");
adapter.add(dataSet);
adapter.notifyDataSetChanged();
}}
Thanks in advance.
You should have to keep key different for each item you are adding to SharedPreferences, otherwise last item you added in preference will override the last one.
I won't suggest using SharedPreferences for making an app like a todo list.
Make a SQLite database and store your todos in that. Or you can save the todos in a JSON file which is also very simple to achieve.
Using SharedPreferences won't be efficient in this case as you will need to track the keys of all the todo preferences, which creates another overhead.
Check out this post by Lars Vogel about creating content providers and SQLite Databses:
Android SQLite database and content provider - Tutorial

Android Trouble with Edit Text Views

In my application,on press of a button "Add contact" phone book gets open and then user selects a contact that get displayed in Edittext View and when another button "Add More Contacts" is pressed, a another Edit-Text View gets displayed on the top for which i can again select the contact from phone book. But the problem that i am facing is that i want that user can only add up to 5 Edit Text .
I am using the following code, but its force crashing the application. Please help.
And also i want the Edit Text Views to be non editable, for which i tried editable=false but its working only for the first Edit Text View not on the other Views, that user adds afterwards.
int id = 1;
layoutLinear = (LinearLayout) findViewById(R.id.mLayout);
btn_addmore_cntct = (Button) findViewById(R.id.baddmorecontacts);
btn_addmore_cntct.setOnClickListener(OnClick());
EditText editview = new EditText(this);
editview.setText("Add more");
editview.setEnabled(false);
editview.setFocusable(false);
}
// implementing OnClickListener OnClick() method for "btn_addmore_cntct"
// button
private OnClickListener OnClick() {
// TODO Auto-generated method stub
// changing return type "null" to "new OnClickListner"
return new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(layoutLinear.getChildCount()>5){
}else{
final EditText tab = new EditText(getApplicationContext());
tab.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tab.setId(id);
id++;
layoutLinear.addView(tab,0);
tab.requestFocus();
}
Just check number of layoutLinear child
if(layoutLinear.getChildCount()>5){
//nothing to do
}else{
//create new EditText and add to layoutLinear
}
user this code to disable EditText
edittext.setEnabled(false);
edittext.setFocusable(false);
You can use following way.
int temp;
public void onCreate(Bundle savedinstance) {
super.onCreate(savedinstance);
setContentView(R.layout.yourlayout);
temp=1;
layoutLinear = (LinearLayout) findViewById(R.id.mLayout);
btn_addmore_cntct = (Button) findViewById(R.id.baddmorecontacts);
btn_addmore_cntct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(temp<=5){
EditText tab = new EditText(getApplicationContext());
tab.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tab.setEnabled(false);
tab.setFocusable(false);
layoutLinear.addView(tab);
temp++;
}else{
//Print message to user Cant add more editText.
}
}
});
}
Let me know its working or not.

Android temp update spinner before it gets values from SQLite

I'm wondering if there is some trick to get the spinner updated from an edittext and onClick, before it gets committed to the database and retrieve from there? The list and the spinnerAdapter are set for retrieving values from database, so I'm aware that this question might be stupid.
I was thinking of this logic: enter some text to edittext, click ok, then temporary update the spinner with this text, before it goes to database, then do some other stuff in the activity and last commit everything to database. When you then close and open your activity again, the temporary value is lost, but the spinner gets populatet with the same value, but this time from database.
here is some code:
public class Vnos extends Activity {
//... some values
#Override
protected void onCreate(Bundle savedInstanceState) {
//...
final Spinner spinner = (Spinner) findViewById(R.id.spinner1);
final List<VnosiDB> spinerItems = datasource.getAllNiz();
final ArrayAdapter<VnosiDB> spinnerAdapter = new ArrayAdapter<VnosiDB>(
this, android.R.layout.simple_spinner_item, spinerItems);
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
String nizIzSpinerja = spinner.getItemAtPosition(
spinner.getSelectedItemPosition()).toString();
nizDB = nizIzSpinerja;
// nov niz
novNiz = (TextView) findViewById(R.id.dodaj);
novNiz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(Vnos.this,
android.R.style.Theme_Holo_Dialog);
dialog.setContentView(R.layout.nov_niz);
TextView okNov = (TextView) dialog.findViewById(R.id.okNovNiz);
okNov.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText inputNiz = (EditText) dialog
.findViewById(R.id.niz);
dialog.dismiss();
nizDB = inputNiz.getText().toString();
spinnerAdapter.notifyDataSetChanged();
}
});
dialog.show();
}
});
// ...some other code...
//...
//.. then, here I commit everything to database...
shrani = (TextView) findViewById(R.id.shrani);
shrani.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
vnosDB = (int) System.currentTimeMillis();
datasource.createVnos(zacetekDB, razlikaDB, nizDB, deloDB,
postavkaDB, dodatekDB, opisDB, vnosDB);
datasource.close();
Toast test = Toast.makeText(Vnos.this, "Vnos " + deloDB
+ " uspešen!", Toast.LENGTH_LONG);
test.show();
startActivity(new Intent("com.sandpit.jazstudent.URE"));
finish();
}
});
}
}
you are using ArrayAdapter, which is loaded by List<VnosiDB>. I assume that VnosiDB represents a record from the database. Why don't you just put a temporary record in this list when you need it?
List<VnosiDB> spinerItems = new ArrayList<VnosiDB>();
VnosiDB tempRec = new VnosiDB();
//set some values to the properties if you need to
spinnerItems.add(tempRec);
//populate the spinner with the temp record

Using JSONArray with SavePreferences to save data from ListView

i am quite new at coding for android. I tried to use SavePreferences in order to save User Inputted data for a ListView. However this method resulted in only saving the last item that the user inputted, not the entire ListView.(i think this was because i was overwriting the key so that it would only show the last value)
Then it was suggested that i should use a JSONArray but I could not understand what happened.
My Code using JSONArray is below:
Note: there are a bunch of errors on the JSONArray since i dont think i used the correct Key.
public class TaskPage extends SherlockActivity {
EditText display;
ListView lv;
ArrayAdapter<String> adapter;
Button addButton;
ArrayList<String> dataSetarrlist = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (EditText) findViewById(R.id.editText1);
lv = (ListView) findViewById(R.id.listView1);
addButton = (Button) findViewById(R.id.button1);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice);
lv.setAdapter(adapter);
LoadPreferences();
// setChoiceMode places the checkbox next to the listviews
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String task = display.getText().toString();
adapter.add(task);
adapter.notifyDataSetChanged();
SavePreferences("LISTS", task);
}
});
}
protected void SavePreferences(String key, String value) {
// TODO Auto-generated method stub
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
JSONArray array = new JSONArray(data.getString(key, value));
SharedPreferences.Editor editor = data.edit();
editor.putString(key, value);
editor.commit();
}
protected void LoadPreferences(){
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
JSONArray dataSet1 = new JSONArray(data.getString("LISTS", "None Available"));
for(int i = 0; i<dataSet1.length(); i++)
adapter.add(dataSet1.getString(i));
adapter.notifyDataSetChanged();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,dataSetarrlist);
lv.setAdapter(adapter);
// setChoiceMode places the checkbox next to the listviews
}
I am quite confused at this point so can someone please show me where Im going wrong and provide some example code. I almost certain it is because I am not using the key and value correctly to save the data but i cant seem to get this correct
Lastly, is there another method where I dont have to use the JSONArray so that it will still show all the user inputs in the listview.
EDITED CODE IS BELOW
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String task = display.getText().toString();
adapter.add(task);
dataSetarrlist.add(task);
adapter.notifyDataSetChanged();
SavePreferences("LISTS", dataSetarrlist.toString());
}
});
}
protected void SavePreferences(String key, String value) {
// TODO Auto-generated method stub
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = data.edit();
editor.putString("LISTS", dataSetarrlist.toString());
editor.commit();
}
protected void LoadPreferences(){
SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
String dataSet = data.getString("LISTS", dataSetarrlist.toString());
ArrayList<String> dataSetarrlist = new ArrayList<String>();
dataSetarrlist.add(dataSet);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,dataSetarrlist);
lv.setAdapter(adapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// setChoiceMode places the checkbox next to the listviews
}
Your TextView (display) only contains 1 value. On the OnClickListener of addButton, you should add "task" on your array & pass the array to SavePreferences instead of "task":
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String task = display.getText().toString();
adapter.add(task);
dataSetarrlist.add(task);
adapter.notifyDataSetChanged();
SavePreferences("LISTS", dataSetarrlist);
}
});
}
You should modify SavePreferences to process the ArrayList.

load data from cursor to Listview android2.1

I am a new programmer. I am unable to give a start for this. I would like to populate cFloors to a list.
Thankyou in advance...
public class FloorsActivity extends Activity {
AutomationDBAccessor db = new AutomationDBAccessor(this);
private OnClickListener floorsUpdateListener = new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor cFloors = db.getFloors();
if(cFloors!=null)
{
Toast toast = Toast.makeText(getApplicationContext(), "floors loaded", Toast.LENGTH_SHORT);
toast.show();
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.floors);
Button btnGetFloors = (Button) findViewById(R.id.btnGetFloors);
btnGetFloors.setOnClickListener(floorsUpdateListener);
}
}
Have a look at this example on using a ListView: http://developer.android.com/resources/tutorials/views/hello-listview.html
But instead of ArrayAdapter in the example, you can use a SimpleCursorAdapter for cFloors and attach it to the view using setListViewAdapter().
SimpleCursorAdapter: http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
you can follow the link http://android-support-akkilis.blogspot.com/2011/11/simple-cursor-adapter-made-easy.html.It contains all the data/code you require to build a simple cursor adapter.

Categories

Resources