how to display a arraylist in listview? - android

Can anybody help me to display the following arraylist in a listview.I am struck with this.
[E000199, E000100, ER008,EW999,EQ333,EQ111,E9089,E1001,E777,E999,E0123,E00054,E0032,E0016,E0016, E009,
E008,E007,E005,E003,E001].
This is the response I get from a webservice.When I try to display it in a listview in another activity I am getting error as
java.lang.IllegalStateException: System services not available to Activities before onCreate().
pls help me.this is my code in second activity.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listview = (ListView) findViewById(R.id.listView1);
}
public void sendList(ArrayList<String> list) {
// TODO Auto-generated method stub
this.emp_list=list;
System.out.println("^^^^^^"+emp_list);
adapt = new ArrayAdapter<String>(this,R.layout.list,R.id.title,emp_list);
listview.setAdapter(adapt);
setContentView(R.layout.viewlist);
}

For your problem you must you custom adapter.
LINK for your reference

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewlist);
listview = (ListView) findViewById(R.id.listView1);
adapt = new ArrayAdapter<String>(this,R.layout.list,R.id.title,emp_list);
listview.setAdapter(adapt);
}
public void sendList(ArrayList<String> list) {
// TODO Auto-generated method stub
this.emp_list.clear();
this.emp_list.addAll( list );
adapt.notifyDataChanged();
}

Yes it is...because you are trying to display ListView and setAdapter before setContentView() method. Instead it should be as below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewlist); // It should be here //1st solution
listview = (ListView) findViewById(R.id.listView1);
sendList(list); // you forgot to call this method
}
public void sendList(ArrayList<String> list) {
// TODO Auto-generated method stub
this.emp_list=list;
System.out.println("^^^^^^"+emp_list);
adapt = new ArrayAdapter<String>(this,R.layout.list,R.id.title,emp_list);
listview.setAdapter(adapt);
}

Just Keep SetContentView(R.layout.viewlist); after super Statement.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewlist);
listview = (ListView) findViewById(R.id.listView1);
adapt = new ArrayAdapter<String>(this,R.layout.list,R.id.title,emp_list);
listview.setAdapter(adapt);
}
as listview.setAdapter(adapt); will directly Set the Value.

before you'll get a view,you must call setContentView.so you can do like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewlist); //add this
listview = (ListView) findViewById(R.id.listView1);
}
public void sendList(ArrayList<String> list) {
// TODO Auto-generated method stub
this.emp_list=list;
System.out.println("^^^^^^"+emp_list);
adapt = new ArrayAdapter<String>(this,R.layout.list,R.id.title,emp_list);
listview.setAdapter(adapt);
}

Related

Android dynamic listview added position

Hi I am working with android listview.I want to create a dynamic list view in which user can add data dynamically to listview .So far its works fine. Now my problem is my contets are added at start position of listview .I want to add the added contents at last position of list view (like adding comments in facebook post). Is it possible to make ?? Please help me .Thanks in advance :)
MY activity class
public class MainActivity extends Activity {
private EditText etInput;
private Button btnAdd;
private ListView lvItem;
private ArrayList<String> itemArrey;
private ArrayAdapter<String> itemAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpView();
}
private void setUpView() {
// TODO Auto-generated method stub
etInput = (EditText)this.findViewById(R.id.editText_input);
btnAdd = (Button)this.findViewById(R.id.button_add);
lvItem = (ListView)this.findViewById(R.id.listView_items);
itemArrey = new ArrayList<String>();
itemArrey.clear();
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,itemArrey);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addItemList();
}
});
etInput.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER) {
addItemList();
}
return true;
}
});
}
protected void addItemList() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (isInputValid(etInput)) {
itemArrey.add(0,etInput.getText().toString());
etInput.setText("");
itemAdapter.notifyDataSetChanged();
}
}
protected boolean isInputValid(EditText etInput2) {
// TODO Auto-generatd method stub
if (etInput2.getText().toString().trim().length()<1) {
etInput2.setError("Please Enter Item");
return false;
} else {
return true;
}
}
}
Remove position from
itemArrey.add(0,etInput.getText().toString()); // Position=0
Try this way.
itemArrey.add(etInput.getText().toString());
that directly add your string at the end.
You are specifying position to add new element to. Just avoid using position and new item will be added at the end of list.
itemArrey.add( etInput.getText().toString() );
Further more, you might also like to scroll down to this newly added item. So you can use these in xml
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
or use setSelection() to do so like -
lvItem.setSelection(countOfItems-1);

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.

I want to get EditText value in ListView

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.

Android: Refresh ArrayAdapter on resume

I am trying to create a refesh function for my main page. I have searched many site but i can't seem to find a (for me) acccesseble exaple. I am loading information from a sQLLite database. When i use my add activity and i return to the MainScreen activity the item i have added do not appear. How could i refresh this data the moment de activity is resumed.
Any help is welcome, thx in advance.
public ListView whiskeylist;
String[] DataArryWhiskey;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Start db view of whiskey
DBConfig info = new DBConfig(this);
info.open();
DataArryWhiskey = info.getDataInArray();
info.close();
whiskeylist = (ListView) findViewById(R.id.listofWhiskey);
whiskeylist.setOnItemClickListener(this);
whiskeylist.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DataArryWhiskey));
}// end onCreate
On The advice of Adil i change the code to
public ListView whiskeylist;
String[] DataArryWhiskey;
ListAdapter Adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Start db view of whiskey
DBConfig info = new DBConfig(this);
info.open();
DataArryWhiskey = info.getDataInArray();
info.close();
whiskeylist = (ListView) findViewById(R.id.listofWhiskey);
Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DataArryWhiskey);
whiskeylist.setAdapter(Adapter);
// End db view of whiskey
}// end onCreate
#Override
public void onResume()
{
super.onResume();
DBConfig info = new DBConfig(this);
info.open();
DataArryWhiskey = info.getDataInArray();
info.close();
Adapter.notifyDataSetChanged(); // refresh adapter
}
however i get a error on notifyDataSetChanged "the method notifyDataSetChanged is undefined for the type ListAdapter" <- fixed it by changing the ListAdapter to ArrayAdapter but the app still crashes.
//made changes in your oncreate method, see below
DBConfig info;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Start db view of whiskey
info = new DBConfig(this);
whiskeylist = (ListView) findViewById(R.id.listofWhiskey);
whiskeylist.setOnItemClickListener(this);
}// end onCreate
another method give below, call this method from on ActivityResult(), but before doing that ensure that
the field you added with another activity also saved into database.
call show Data() from onActivityResult method or from onResume()
private void showData()
{
info.open();
DataArryWhiskey = info.getDataInArray();
info.close();
whiskeylist.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DataArryWhiskey));
}
Get a class level variable of ArrayAdapter adapter; and initialize it like this way:
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DataArryWhiskey);
hiskeylist.setAdapter(adapter);
and inside onResume() do like this way.
public void onResume()
{
super.onResume();
info.open();
DataArryWhiskey = info.getDataInArray();
info.close();
adapter.notifyDataSetChanged(); // refresh adapter
}
My Recommendation:
Since you are getting your values from database, use SimpleCursorAdapter instead of ArrayAdapter. which will do lots of other work also for you.
Here is a tutorial how to use SimpleCursorAdapter
You can try using adapter.clear(); before adding items.

Categories

Resources