This is my code :
Here _alProduct is an ArrayList defined static in the main class.
I am deleting an item from _alProduct on long click of listview.
Now I want to display the listview with the deleted item removed.
public class MCRActivity2 extends Activity {
TextView tvShoppingCart;
ListView lvSelectedItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mcractivity2);
lvSelectedItems = (ListView) findViewById(R.id.lvSelectedItems);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, MobileCashRegisterActivity._alProduct);
lvSelectedItems.setAdapter(adapter);
lvSelectedItems.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> adapter, View view,
int position, long arg3) {
// TODO Auto-generated method stub
String text = "Clicked item is " + adapter.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();// ""+ lvSelectedItems.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
MobileCashRegisterActivity._alProduct.remove(position);
MobileCashRegisterActivity._alPrice.remove(position);
MobileCashRegisterActivity._alQuantity.remove(position);
return false;
}
});
}
}
Call remove() on your ArrayAdapter, instead of calling remove() on the ArrayList. This will both remove the item from the ArrayList and tell the AdapterView to refresh its contents.
Related
I am having an issue with using a listview that when I click it nothing is happening. I would like for it when I click the listview item to make a toast so I know it is being clicked. I have been trying/researching for a while and nothing. Would anybody mind taking a look to see if I am missing something I just am overlooking? Many thanks in advance!
Here is my class:
public class MyCar extends Activity {
/**
* Called when the activity is first created.
*/
public ListView mylistView;
String carInfo;
private ArrayAdapter<String> mylistAdapter;
ArrayList<String> arrayListCar = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mycar);
mylistView = (ListView) findViewById(R.id.listView);
arrayListCar = new ArrayList<String>();
//Just had to remove setting this adapter 2 times. Took out line below to fix.
mylistView.setAdapter(mylistAdapter);
mylistView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView) view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
carInfo = trySomethin();
fillList();
}
public void fillList() {
String make = "";
String model = "";
String[] pieces = carInfo.split("\"");
make = pieces[3];
model = pieces[7];
ArrayList<String> carList = new ArrayList<String>();
carList.add(make + " " + model);
// Create ArrayAdapter using the car list.
mylistAdapter = new ArrayAdapter<String>(MyCar.this, android.R.layout.simple_list_item_single_choice, carList);
mylistView.setAdapter(mylistAdapter);
mylistAdapter.notifyDataSetChanged();
}
}
if you have any elements on listview item change this for them
android:focusable="false"
and if you are changing any elements visibility on runtime you have to handle focus programatically each time you change its visibility.
Try this
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
I'm supposed to make a task , I have a spinner connected with a String array x , this array contains three values , so I want when clicking any choice of the spinner a specific list view will will give a specific three values and , this is my code :
public class Four extends ActionBarActivity {
String x [] = {"Jordan","Saudi Arabia", "Syria"};
String Jordan[] = {"Amman","Aqaba","Sarqa"};
String Saudi[] = {"Riyadh","Jeddah","Khobar"};
String Syria[] = {"Hems","Halab","Demashk"};
Spinner sp1 ;
ListView lv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.four);
lv1 = (ListView)findViewById(R.id.listView1);
// Jordan List View
ArrayAdapter<String> jor = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Jordan);
lv1.setAdapter(jor);
// Saudi Arabia List View
ArrayAdapter<String> saud = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Saudi);
lv1.setAdapter(saud);
// Syria List View
ArrayAdapter<String> syr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Syria);
lv1.setAdapter(syr);
sp1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> a = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item , x);
sp1.setAdapter(a);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
The thir parameter of onItemSelected() (int arg2 in your example, but I would suggest you rename them) is the position you selected in the Spinner. So you could implement that method like this:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (position == 0)
lv1.setAdapter(jor);
else if (position == 1)
lv1.setAdapter(saud);
else if (position == 1);
lv1.setAdapter(syr);
}
Remember that all the variables involved (lv1, jor, saud, syr) must be defined as final to be used inside the anonymous class, e.g.
final ArrayAdapter<String> jor = ...
final ArrayAdapter<String> saud = ...
I've a ListView with multiple TextView in a single row. The TextView values are name, groupname, date and some hidden fields. And a spinner outside of this ListView. Once i select item from spinner ListView needs to filtered. That is the other row will be gone. I've try setVisibility() method but it's not works good. I'm using simple adapter. How can i achieve this. Please help me.
My piece of sample code:
Spinner draft_filter = (Spinner) findViewById(R.id.draft_filter);
draft_filter.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Spinner draft_spinner = (Spinner) arg0;
String selected_string = (String) draft_spinner.getSelectedItem();
//Save this String and filter the listview using this
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
ListView level1 = (ListView) findViewById(R.id.listview);
level1.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
for (int i=0; i<level1.getChildCount(); i++)
{
// Set the Visibility of the row here
}
});
//For loading listview
//Here fillmaps is the arraylist contains hashmap values
String[] drafts = new String[] { "child_name", "age"};
int[] controls = new int[] { R.id.item1, R.id.item2};
adapter = new SimpleAdapter(getApplicationContext(), fillMaps,
R.layout.draft_observation_list_layout, drafts, controls);
level1.setAdapter(adapter);
How to filter this listview Please help.
Call adapter each time when Spinner item value is change with filtered list value.
I tried lots of stuff but the solution is very simple:
((SimpleAdapter) MyActivity.this.adapter).getFilter().filter(filter_text);
got problem
Have 2 spinner on layout and wanna get dependence of this spinner
Structure
String[] main={"Moscov","LosAngeles","Pekin"};
String[] one={"1","2","3","4"};
String[] two={"2","2","5","8"};
String[] tree={"5","7","3","2"};
Create first adapter for first spinner with main array.
And dependently for item choice must crete adapter with array which depend of choice
If item 1 then adapter with array one, if item 2 then array two
You will create your spinner and just set the Adapter for the second spinner according to the selection using onItemSelected on the first Spinner. Something like
#Override
public void onItemSelected(AdapterView<?> spinner, View view, int position,long arg3)
{
TextView tv = (TextView)view; // convert the view to TextView
String selected = tv.getText().toString();
if (selected.equals("Moscov"))
{
// set the Adapter here with the first set of values
}
else if(selected.equals("LosAngeles"))
{
// set the Adapter here with the second set of values
}
else
{
// set the Adapter here with the third set of values
}
}
You also could use the position which may be more scalable if you put your String Arrays into an ArrayList.
#Override
public void onItemSelected(AdapterView<?> spinner, View view, int position,long arg3)
{
switch(position)
{
case : 0;
// called first array
break;
case : 1;
// called second array
break;
}
}
Checkout this code snippet , in this values of spinner 2 is dependent on spinner 1
public class MainActivity extends Activity {
Spinner s1,s2;
Button btn;
String s;
TextView tv;
ArrayAdapter<String> adap1,adap2,adap3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1=(Spinner)findViewById(R.id.spinner1);
s2=(Spinner)findViewById(R.id.spinner2);
btn=(Button)findViewById(R.id.button1);
tv=(TextView)findViewById(R.id.tv1);
String[] v1=getResources().getStringArray(R.array.c1);
String[] v2=getResources().getStringArray(R.array.c2);
String[] v3=getResources().getStringArray(R.array.c3);
adap1=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v1);
adap2=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v2);
adap3=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v3);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ItemSelectedListenerr implements OnItemSelectedListener {
String[] v=getResources().getStringArray(R.array.c1);
public String s;
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
s=arg0.getItemAtPosition(arg2).toString();
if(arg2==0)
{
s2.setAdapter(adap1);
}
if(arg2==1)
{
s2.setAdapter(adap2);
}
if(arg2==2)
{
s2.setAdapter(adap3);
}
tv.setText(s);
Log.i("hahaha", "item selected is"+s);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}
I hope this will help you out. :)
I have a ListActivity class. It has a button which sorts the item according to one of their attributes. But it so happens that the item are getting sort by their name but upon clicking the item of its original position is getting executed.
Here is the code snippet :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
mButton = (ToggleButton) findViewById(R.id.mButton);
ArrayList<myObject> ListData = new ArrayList<myObject>();
back = (ImageButton) findViewById(R.id.backButton);
label = (TextView) findViewById(R.id.likethis);
label.setText("List");
inputSearch = (EditText) findViewById(R.id.inputSearch);
//Manager -- class that reads from the sdcard
Manager plm = new Manager();
// get all files from sdcard
//getList() function of Manager class
//List declared outside oncreate()
this.List = plm.getList();
final ArrayList<myObject> backUp = new ArrayList<myObject>(songsList);
if(sortCalled) {
mButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.m_checked));
mButton.setChecked(true);
Collections.sort(List);
notifyDataChanged(List);
}
else {
mButton.setBackground(getResources().getDrawable(R.drawable.m_unchecked));
mButton.setChecked(false);
notifyDataChanged(backUp);
}
back.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
inputSearch.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
adapter.notifyDataSetChanged();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
//ArrayList <Song> temp;
((SimpleAdapter) PlayListActivity.this.adapter).getFilter().filter(s, new Filter.FilterListener() {
public void onFilterComplete(int count) {
// TODO Auto-generated method stub
adapter.notifyDataSetChanged();
}
});
adapter.notifyDataSetChanged();
}
});
mButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mButton.isChecked()){
mButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.m_checked));
Collections.sort(List);
sortCalled = true;
notifyDataChanged(List);
}
else{
sortCalled = false;
mButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.m_unchecked));
notifyDataChanged(backUp);
}
}
});
}
public void settingListAdapter(SimpleAdapter adapter){
setListAdapter(adapter);
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting listitem index
int Index = position;
// Starting new intent
Intent in = new Intent(getApplicationContext(),
MainActivity.class);
// Sending Index to MainActivity
in.putExtra("Index", Index);
setResult(100, in);
// Closing ListView
finish();
}
});
}
public void notifyDataChanged(ArrayList<Song> songsList) {
// TODO Auto-generated method stub
setListAdapter(null);
sortCalled = true;
ArrayList<myObject> songsListData = new ArrayList<myObject>();
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
myObject ob = sList.get(i);
// adding HashList to ArrayList
ListData.add(ob);
}
ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String,String>>();
// int j = 0;
for(myObject i : ListData){
HashMap<String, String> feed = new HashMap<String, String>();
feed.put("Title", i.getTitle());
array.add(feed);
}
BaseAdapter adapter1;
// int i = 0;
// Adding menuItems to ListView
adapter1 = new SimpleAdapter(this, array,
R.layout.list_item, new String[]{"Title" }, new int[] {
R.id.Title });
setListAdapter(adapter1);
adapter1.notifyDataSetChanged();
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting listitem index
int Index = position;
// Starting new intent
Intent in = new Intent(getApplicationContext(),
MainActivity.class);
// Sending songIndex to MainActivity
in.putExtra("Index", Index);
setResult(100, in);
// Closing ListView
finish();
}
});
I think you should try this.when you sort the data of your listview.it is working me perfectly
adapter.notifyDataSetChanged();
Everytime you make a change to adapter you should call notifyDataSetChanged() in UIThread
Please check this video to understand listView better
Edit
For an ArrayAdapter, notifyDataSetChanged only works if you use the add, insert, remove, and clear functions on the Adapter.
When an ArrayAdapter is constructed, it holds the reference for the List that was passed in. If you were to pass in a List that was a member of an Activity, and change that Activity member later, the ArrayAdapter is still holding a reference to the original List. The Adapter does not know you changed the List in the Activity.
Your choices are:
Use the functions of the ArrayAdapter to modify the underlying List (add, insert, remove, clear, etc.)
Re-create the ArrayAdapter with the new List data. (Uses a lot of resources and garbage collection.)
Create your own class derived from BaseAdapter and ListAdapter that allows changing of the underlying List data structure.
Use the notifyDataSetChanged every time the list is update. To call it on the UI-Thread use the runOnUiThread method of the Activity. Then notifyDataSetChanged will work.
You could try the Adapter.notifyDataSetChanged() function. It might work.
If you are using a SimpleCursorAdapter, calling requery() on the cursor attached to the adapter will automatically refresh the adapter and attached view.
If it's BaseAdapter content changes, you call BaseAdapter.notifyDataSetChanged() which will tell the ListView to update itself.
In other words you only need to make the following tiny change:
public void setValue(Object newValue) {
this.value = newValue;
notifyDataSetChanged();
}
Actually, since you're changing the state of an existing item (rather than adding new ones etc) then notifyDataSetInvalidated() would be a better choice.