on button click reload adapter - android

I want to change data on listview runtime after button click...
here is my code
oncreate works fine
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// When item is tapped, toggle checked properties of CheckBox and Planet.
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick( AdapterView<?> parent, View item,
int position, long id) {
Planet planet = listAdapter.getItem( position );
planet.toggleChecked();
PlanetViewHolder viewHolder = (PlanetViewHolder) item.getTag();
viewHolder.getCheckBox().setChecked( planet.isChecked() );
}
});
// Create and populate planets.
planets = (Planet[]) getLastNonConfigurationInstance() ;
if ( planets == null ) {
planets = new Planet[] {
new Planet("Mercury"), new Planet("Venus"), new Planet("Earth"),
new Planet("Mars"), new Planet("Jupiter"), new Planet("Saturn"),
new Planet("Uranus"), new Planet("Neptune"), new Planet("Ceres"),
new Planet("Pluto"), new Planet("Haumea"), new Planet("Makemake"),
new Planet("Eris")
};
}
ArrayList<Planet> planetList = new ArrayList<Planet>();
planetList.addAll( Arrays.asList(planets) );
// Set our custom array adapter as the ListView's adapter.
listAdapter = new PlanetArrayAdapter(this, planetList);
mainListView.setAdapter( listAdapter );}
but when i want to change it runtime after button click like this i get error ...
private View.OnClickListener onLigyVyber = new View.OnClickListener() {
public void onClick(View button3) {
// Create and populate planets.
planets = (Planet[]) getLastNonConfigurationInstance() ;
planets = new Planet[] {
new Planet("a"), new Planet("b"), new Planet("c"),
new Planet("d")
};
ArrayList<Planet> planetList = new ArrayList<Planet>();
planetList.addAll( Arrays.asList(planets) );
// Set our custom array adapter as the ListView's adapter.
listAdapter = new PlanetArrayAdapter(this, planetList);
mainListView.setAdapter( listAdapter );
i got error on this line
The constructor Ventana.PlanetArrayAdapter(new View.OnClickListener(){}, ArrayList) is undefined
listAdapter = new PlanetArrayAdapter(this, planetList);
Thanks a lot.

use :
listAdapter = new PlanetArrayAdapter(getBaseContext(), planetList);
or you can use YOUR_Activity_NAME.this

Related

Android: Get listview value from an "2-line" listview on onclick

when i click on a listview with a two_line_list_item. I want to get the value from the first line.
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
item = new HashMap<String,String>();
item.put( "line1", "my_line");
item.put( "line2", "my_sub");
list.add( item );
sa = new SimpleAdapter(this, list, android.R.layout.two_line_list_item ,new String[] { "line1","line2" }, new int[] {android.R.id.text1, android.R.id.text2});
setListAdapter( sa );
lv = (ListView) findViewById(android.R.id.list);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{
String test = lv.getItemAtPosition(position)
}
});
String test wil contain:"{line1=my_line,line2=my_sub}"
How can i output only the value "my_line"
String[] strings = { "line1","line2" };
sa = new SimpleAdapter(this, list, android.R.layout.two_line_list_item , strings, new int[] {android.R.id.text1, android.R.id.text2});
setListAdapter( sa );
lv = (ListView) findViewById(android.R.id.list);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{
String test = strings[position];
}
});

Is it possible to create a ListView using run()?

I would like to know if it is possible to create a list view in runnable using something like this ? Could someone please give me an example to do it? Thank you.
public void testBtnListViewClick(View v) {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
LinearLayout ll = new LinearLayout(this);
ListView lv = new ListView(this);
String[] values = new String[10];
for(int i=0;i<10;i++){
values[i] = ""+i;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, values);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Toast.makeText(getBaseContext(), ""+arg2,Toast.LENGTH_SHORT).show();
Log.d("DEBUG", ""+arg2);
}
});
//ll.addView(lv);
ll.addView(lv, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
setContentView(ll);
}
}
Yes ..you can..runOnUiThread() which runs in the main thread.In android UI changes like Views can be modiied or added only in UI thread..Its in the UI thread so no problem for yours..
inside that this refers to runnable object..so change these lines
LinearLayout ll = new LinearLayout(this);
ListView lv = new ListView(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, values);
into
LinearLayout ll = new LinearLayout(MainActivity.this);
ListView lv = new ListView(MainActivity.this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_item, values);

sqlite Viewlist error

i trying tu put some elements from database to a List view,
my problem is that when i starrt my activity, I get this:
**com.example.restaurant.Restaurant#2be2d1d0
com.example.restaurant.Restaurant#2be3d3a8**
instead of database objects.
public class Liste extends Activity{
private ListView listview;
private Button boutonAjouter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste_restaurant);
listview = (ListView)findViewById(R.id.listView1);
Restaurant mcdo = new Restaurant("mcdo","brossard","take-out","fastfood","450-555-5555");
Restaurant burger = new Restaurant("burger","longueuil","take-out","fastfood","450-999-9999");
RestaurantBDD restaurantBDD = new RestaurantBDD(this);
restaurantBDD.openForWrite();
restaurantBDD.insertRestaurant(mcdo);
restaurantBDD.insertRestaurant(burger);
ArrayList <Restaurant> restaurantlist = restaurantBDD.getAllRestaurants();
restaurantBDD.close();
ArrayAdapter <Restaurant> adapter = new ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, restaurantlist);
listview.setAdapter(adapter);
boutonAjouter = (Button) findViewById(R.id.btn_nouveau);
boutonAjouter.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Liste.this, Formulaire.class);
startActivity(intent);
}
});
}
}
If you want to show a list with the names of the restaurants, supposing that your Restaurant class contains a string attribute "name", you should first create an array containing the names of the restaurants and then use an ArrayAdapter <String>:
String[] values = new String[restaurantlist.size()];
for(int i=0;i<restaurantlist.size();i++) {
values[i] = restaurantlist.get(i).getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
listview.setAdapter(adapter);

How to use SimpleCursorAdapter with single_choice and multiple_choice

I am developing a quiz application which contain both multiple answer type and single answer type.the question and answer are store in sqlite database.i use simple cursor adapter and get idea about how to use from here! My question is that how i can switch from simple_list_item_single_choice to simple_list_item_multiple_choice if my answer type changes(single to multiple) and also how can i save the answer choosen.Please give some idea about it.Here is the coding....
db = new DBAdapter(this);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
db.getAllTitles(),
new String[] { "title" },
new int[] { android.R.id.text1 });
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
check this code: for single choice:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_single_choice,
db.getAllTitles(),
new String[] { "title" },
new int[] { android.R.id.text1 });
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
for multiple:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
simple_list_item_multiple_choice,
db.getAllTitles(),
new String[] { "title" },
new int[] { android.R.id.text1 });
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
depend on your requirement you have to set one of above: i think you aleady kno which question require single ans & which require multiple depend on that you have to also create list item click like
listView.setOnItemClickListener(new OnItemClickListener() {
private String my_sel_items;
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
if(ans == multiple){
my_sel_items = new String("Selected Items");
SparseBooleanArray a = lView.getCheckedItemPositions();
for (int i = 0; i < a.size(); i++) {
if (a.valueAt(i)) {
my_sel_items = my_sel_items + ","
+ (String) listView.getAdapter().getItem(i);
}
}
Log.v("values", my_sel_items);
}else{
// for single it default selected item
}
}
});

Updating listview in Android

I've made a listview for some games, and when a game is deleted i'd like it to vanish in the listview. I'm trying to use adapter.notifyDataSetChanged(), but i can't get it to work, can you help me?
public static final String MY_SETTINGS = "MySettings";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
DBHelpeR entry = new DBHelpeR(Loadmenu.this);
entry.open();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item,entry.getGameNames());
setListAdapter(adapter);
entry.close();
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setBackgroundResource(R.drawable.background);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
lv.setOnItemLongClickListener(new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> parent, final View view,
int position, long id) {
String gamename = ((TextView) view).getText().toString();
builder.setTitle("Slet " + gamename + "?");
builder.setItems(new CharSequence[]{"Slet"} , new DialogInterface.OnClickListener() {
// Click listener
public void onClick(DialogInterface dialog, int item) {
DBHelpeR entry = new DBHelpeR(Loadmenu.this);
entry.open();
int game_id = entry.getGameID(((TextView) view).getText().toString());
Log.d("load",Integer.toString(game_id));
entry.deleteGame(game_id);
entry.close();
adapter.notifyDataSetChanged();
}
});
builder.show();
return true;
}
});
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item,entry.getGameNames());
replace the above line with below :::
ArrayList<String> entries = entry.getGameNames();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item,entries);
and when you delete just dcall the below two lines again::
entries = entry.getGameNames();
adapter.notifyDataSetChanged();
Updated::
Again before calling adapter.notifyDataSetChanged(); call the below lines::
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, entry.getGameNames());
setListAdapter(adapter);
call
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item,entry.getGameNames());
setListAdapter(adapter);
Again before calling
adapter.notifyDataSetChanged();
Note: make sure you have changed the array for the change to be visible.

Categories

Resources