ListView update - android

I have two activities with ListView. This two activitises have individual layout-file. First have myListView01, second have myListView02. How to update listView's adapters?
I do so:
ListView list = (ListView)findViewById(R.id.myListView01);
myAdapter = new ArrayAdapter<String>(this, R.layout.item01, R.id.label, array01);
list.setAdapter(myAdapter);
I remove some items from the array01 and I need to update my listView without this elements.
But this solution to ListActivity does not work:
myAdapter.notifyDataSetChanged();
my code:
package ...;
import ...;
public class Class01 extends Activity
{
ArrayAdapter<String> mAdapter;
ArrayList<String> array01 = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout01);
ListView list = (ListView)findViewById(R.id.my_list01);
mAdapter = new ArrayAdapter<String>(this, R.layout.item01, R.id.label, array01);
list.setAdapter(mAdapter);
}
public void onListItemClick (ListView parent, View v, int position, long id)
{
array01 = deleteElementByPosition(array01, position);
mAdapter.notifyDataSetChanged(); //does not work
}
public void deleteElementByPosition()
{
...
}
}

You have not set onItemClickListener for your ListView.
Extend your Class01 from ListActivity. Activity class doesn't have onListItemClick() method so it is never invoked. Or set your listener with setOnItemClickListener() for your ListView.

Related

Clickable custom array list

How can I make this custom array list clickable to go to the others activities because I tried the intents but it doesn't work
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Names> namesArrayList = new ArrayList<Names>();
namesArrayList.add(new Names(R.drawable.call_centre, "Call Centre"));
namesArrayList.add(new Names(R.drawable.soco_academy_icon, "Academy"));
NamesAdapter NamesListAdapter = new NamesAdapter(this, namesArrayList);
ListView list = (ListView) findViewById(R.id.List_View);
list.setAdapter(NamesListAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
In onItemClick() you can determine which item was clicked by doing this:
Name selectedName = NamesListAdapter.getItem(position);
Then you can do whatever you want with that.

android: spinner not updating my view when notifyDataSetChanged() is called

I am trying to update the spinner view by calling notifyDataSetChanged(). But its not updating.
The below is my Activity:
public class DisplayArchive2 extends AppCompatActivity {
List<String> yearslist;
Spinner spinner;
ArrayAdapter<String> dataAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.archive2);
spinner = (Spinner) findViewById(R.id.year_spinner);
yearslist = new ArrayList<String>();
yearslist.add("2017");
yearslist.add("2016");
yearslist.add("2015");
yearslist.add("2014");
yearslist.add("2013");
dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,yearslist);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
yearslist.clear();
yearslist.add("2010");
yearslist.add("2009");
yearslist.add("2008");
yearslist.add("2007");
yearslist.add("2006");
dataAdapter.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
The list of years is not changing on selecting any year.
It looks like below
Try this use dataAdapter2.setNotifyOnChange(true); its working fine i have tested it my device
void setNotifyOnChange (boolean notifyOnChange)
Control whether methods that change the list (add(T), addAll(Collection), addAll(Object[]), insert(T, int), remove(T), clear(), sort(Comparator)) automatically call notifyDataSetChanged(). If set to false, caller must manually call notifyDataSetChanged() to have the changes reflected in the attached view. The default is true, and calling notifyDataSetChanged() resets the flag to true.
CODE
List<String> yearslist;
Spinner spinner;
ArrayAdapter<String> dataAdapter2;
spinner = (Spinner) findViewById(R.id.year_spinner);
yearslist = new ArrayList<String>();
yearslist.add("2017");
yearslist.add("2016");
yearslist.add("2015");
yearslist.add("2014");
yearslist.add("2013");
dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,yearslist);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter2);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
} else {
yearslist.clear();
yearslist.add("2010");
yearslist.add("2009");
yearslist.add("2008");
yearslist.add("2007");
yearslist.add("2006");
dataAdapter2.setNotifyOnChange(true);
dataAdapter2.notifyDataSetChanged();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
As per my understanding. You are notifying the adapter inside onItemSelected(). In case of Spinner adapter initialization onItemSelected() will get called once for first time . So the list you will get in Spinner is always the new one(i.e 2006-2010) never the first one(i.e 2013-2017).
So if you do notify adapter on some other action or maybe avoid the first call to onItemSelected() your code will work fine .

How to handle 2 Listvews in one activity Android

I have created 2 listviews in 1 Activity.
Eg:Vegetables
Fruits
Meat
So when you click on Fruits I want all the 5 Fruits available to show in the 2nd listView. Can you please tell me how to do this? My code is given below
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listv);
ListView menu= (ListView) findViewById(R.id.listView1);
String items[]= {"Fruits","Vegetables","Jooses","Meat","Toys","Cookeys"};
ListView menu2= (ListView) findViewById(R.id.listView2);
String subitems[]= {"xxx","xxx","xxx","xxx","xxx","xxx"};
menu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems));
}
Set the adapter for the second listview in the SetOnItemClick adapter of first listview:
menu.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, check which item is it and then set adapter and display values in 2nd listview
}
});
Refer:
http://matrix-examplecode.blogspot.in/2011/11/listview-example.html
http://www.mkyong.com/android/android-listview-example/
http://developer.android.com/reference/android/widget/AdapterView.html
## -----lets try this simple code. this will suites your need-----##
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listv);
ListView menu= (ListView) findViewById(R.id.listView1);
ListView menu2= (ListView) findViewById(R.id.listView2);
String items[]= {"Fruits","Vegetables","Jooses","Meat","Toys","Cookeys"};
String subitems1[]= {"xxx1","xxx","xxx","xxx","xxx","xxx"};
String subitems2[]= {"xxx2","xxx","xxx","xxx","xxx","xxx"};
String subitems3[]= {"xxx3","xxx","xxx","xxx","xxx","xxx"};
String subitems4[]= {"xxx4","xxx","xxx","xxx","xxx","xxx"};
String subitems5[]= {"xxx5","xxx","xxx","xxx","xxx","xxx"};
menu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
// on item click listener
menu.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// get the selected values from listview 1
String val = items[arg2];
if(val.equalsIgnoreCase("Fruits"))
{
// set the second listview 2
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems1));
}
else if(val.equalsIgnoreCase("Vegetables"))
{
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems2));
}
else if(val.equalsIgnoreCase("Jooses"))
{
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems3));
}
else if(val.equalsIgnoreCase("Meat"))
{
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems4));
}
else if(val.equalsIgnoreCase("Toys"))
{
menu2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subitems5));
}
}
});
}
Register an onItemClickListener callback on menu (Listview).
menu.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick (AdapterView<?> parent, View view, int position,
long id)
{
/* TODO: Update menu2 adapter items */
/* Position - determines the item that is being clicked on your menu listview,
by that. you can render your second adapter based on the item selected */
/* Call adapter notifyDatasetChanged method to inform adapter that its data set
has changed. */
menu2Adapter.notifyDatasetChanged();
}
});
Or other design may be. Put your 2nd listview on a second activity. every time you
select an item. Push the new activity that points to the 2nd list view..
when you click an item in the left listview, the OnItemCLickListener will be triggered. In the callback, update the underlying data maintained by the second listview and call notifyDataSetChanged.
Check the following answer...I checked this code by myself. May be what you want to do.
ListView menu = (ListView) findViewById(R.id.listView1);
String items[] = { "Fruits", "Vegetables", "Jooses", "Meat", "Toys", "Cookeys" };
final ListView menu2 = (ListView) findViewById(R.id.listView2);
final String subitems[] = { "xxx", "xxx", "xxx", "xxx", "xxx", "xxx" };
menu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//Here YourActivity.this means the Context
menu2.setAdapter(new ArrayAdapter<String>(YourActivity.this,
android.R.layout.simple_list_item_1, subitems));
}
});
Override your first listview(menu in your example) onItemClickListener
and prepare subitems (list to display in menu2) according to your need
or whatever you wants to display and call notifyDataSetChanged() on
menu2 adapter ( menu2.getAdapter().notifyDataSetChanged(); ).

Listview item click issue

I have added leadbolt ad(entry ad). The advertising works correctly but listview.click doesn't work when I close ad from close sign. (Listview.click does not do anything, It works when I remove AdController)
public class SoundList extends ListActivity {
int [] soundfile;
MediaPlayer mediaPlayer;
private AdController myController;
final Activity act = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myController = new AdController(act, "111111111");
myController.loadAd();
soundfile= new int[] {R.raw.sound1,R.raw.sound2.....};
String[] sounds= getResources().getStringArray(R.array.sounds);
// Binding Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, sounds));
ListView lv = getListView();
lv.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleListItem.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
}
Having a focusable item in a row of a ListView causes the OnItemClickListener NOT to be invoked.
To fix this issue add following code to row view.
XML:
android:descendantFocusability="blocksDescendants"
Java:
listItem.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

setOnItemClickListener not working

I am not able get onItemCickListener to work. All I am doing is opening up a fragment using intents. Please do let me know if I am doing anything fishy here ?
public class MyListFragment extends ListFragment {
private ArrayList<String> myList = null;
private ArrayAdapter<String> myAdapter;
#Override
public void onActivityCreated(Bundle savedInstanceState){
Log.d("Example:", "In Fragement Calss");
super.onActivityCreated(savedInstanceState);
Resources myResources = getResources();
myList = new ArrayList<String>(Arrays.asList(myResources.getStringArray(R.array.myExamArray)));
myAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,myList);
setListAdapter(myAdapter);
ListView lv = (ListView) getActivity().findViewById(R.id.listView);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(view.getContext(), DetailsFragment.class);
startActivity(myIntent);
}
});
}
}
Note that the ListView blocks clicks of an item that contains at least one focusable
descendant but it doesn’t make the content focus-reachable calling
setItemsCanFocus(true). One workaround is by disabling focusability of descendants, using
android:descendantFocusability="blocksDescendants"
in the layout defining your list item. (First learned of this myself from http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/, but a few other SO posts also point this out.) Does your layout contain buttons? If so, you'd fall into this case.
I don't see where you are inflating a specific XML for this Fragment... but it is possible you are referencing the wrong ListView.
Regardless you should use the existing onListItemClick method:
public class MyListFragment extends ListFragment {
private ArrayList<String> myList = null;
private ArrayAdapter<String> myAdapter;
#Override
public void onActivityCreated(Bundle savedInstanceState){
// Remove the OnItemClickListener, but keep everything else
}
#Override
public void onListItemClick (ListView l, View v, int position, long id) {
Intent myIntent = new Intent(this, DetailsFragment.class);
startActivity(myIntent);
}
}

Categories

Resources