load data from cursor to Listview android2.1 - android

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.

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);

how to display a arraylist in listview?

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);
}

get data from customize listview

Here is my code,
public class SecondScreenActivity extends Activity {
ListView foodJntListView;
ArrayList<Restaurent> restaurentData;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second_screen);
restaurentData = getFoodJnt();
foodJntListView=(ListView)findViewById(R.id.listView_foodjnt);
foodJntListView.bringToFront();
// setting the adapter to the list
foodJntListView.setAdapter(new RestaurantBaseAdapter(this,restaurentData));
//setting the onclick listener,activity on clicking on an item of the
foodJntListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String hotelname=restaurentData.get(position).toString();
//things to write
}
});
}
// get all the list of foodjoints
private ArrayList<Restaurent> getFoodJnt() {
// TODO Auto-generated method stub
ArrayList<Restaurent> results=new ArrayList<Restaurent>();
Restaurent restrnt=new Restaurent();
restrnt.setFoodJointname("Ashila");
restrnt.setCuisine("Biriyani,Moughlai");
restrnt.setAddress("Kolkata,E M Bypass");
restrnt.setOpenhours("10:00am-10:00pm");
results.add(restrnt);
restrnt=new Restaurent();
restrnt.setFoodJointname("Bhajohori Manna");
restrnt.setCuisine("Bengali,Chinese");
restrnt.setAddress("Kolkata,Esplanede");
restrnt.setOpenhours("10:00am-10:00pm");
results.add(restrnt);
restrnt=new Restaurent();
restrnt.setFoodJointname("Bar B Q");
restrnt.setCuisine("Bengali,Chinese,Thai");
restrnt.setAddress("Kolkata,Park Street");
restrnt.setOpenhours("10:00am-10:00pm");
results.add(restrnt);
return results;
}
public void makeAToast(String str) {
//yet to implement
Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
I can able to show a customized listview with a bunch of textview on it as it's item but,I want to get the Name of the restauirants setOnItemClick on the List view.
e.g whenever I click Click on "Bar B Q","calcutta Food Court",it'll show me only "Bar B Q","calcutta Food Court" not other informations.
thnx in advance.Feel free to if u need anything.
"my application screen shot"
assume you've got method like getFoodJointname() in your Restaurent class, you can write the below in your onItemClick():
String hotelname = restaurentData.get(position).getFoodJointname();
makeAToast(hotelname);
this will work hopefully.
Restaurent rest= (Restaurent) foodJntListView.getSelectedItem();

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 Button Problem

i am making a app which generate buttons according to the value entered by user. each button have have there own function defined in XML. Now my main problem is how to shorten these codes.
name[0].setClickable(true);
name[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[0].setText("kjghjbjhb");
}
});
name[2].setClickable(true);
name[2].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[2].setText("kjghjbjhb");
}
});name[1].setClickable(true);
name[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[1].setText("kjghjbjhb");
}
});
and soo on.....writing these codes again and again is not possible as button generated are dynamic, i dunno how many buttons will be generated. Please tell if there is a some other way to do this.
Something like this?
createButton(int i){
name[i].setClickable(true);
name[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[i].setText("kjghjbjhb");
}
});
}
With this method you can also make a for-loop:
for (int i = 0; i<name.length; i++){
createButton(i);
}
Here I am specifying the steps to be executed.
You must be creating the buttons by new Button(); just hold its reference in a Collection say ArrayList
ArrayList ar = new ArrayList();
Button b1 = new Button();
ar.add(b1);
Now create a private inner class which is implementing the View.OnClickListener. Now as per rules implement theOnClick() and so the stuff which you want to be done at there
class A extends Activity{
// your stuff here for OnCreate and other business logic
private final class MyListener implements View.OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
v.setText("kjghjbjhb");
}
}
}
Notice that I am setting the text with the reference of object v in onClick. Also make this class singleton.
Now set create the instance of this class (as the MyListerner will be singleton the object will be one) in the setOnClickListener() like this:
MyListener listener = MyListener.getInstance();
b.setOnClickListener(listener);
You can opt this way when the buttons are created on some event or user action. In case if you need to create the buttons in loop you can use the 1st and 3rd step in loop.

Categories

Resources