How to add elements to a ListView? - android

I want to add an element to my ListView. I tried with myAdapter.add() (see code below) since ArrayAdapter has an add() method. But that didn't work. What is the correct way to add elements to my ListView?
public class LinearLayoutDemo extends ListActivity implements OnItemClickListener {
private static final String[] items={"1", "2", "4", "8", "16", "32", "64"};
ListView myLV;
ArrayAdapter myAdapter;
#Override public void onCreate(Bundle icicle) {
super.onCreate(icicle);
myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, items);
myLV = (ListView) findViewById(android.R.id.list);
myLV.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
myLV.setAdapter(myAdapter);
// The following causes the program to fail:
myAdapter.add("128");
}
Also, the program crashes if if I try myAdapter.clear(). Am I not using the ArrayAdapter correctly?

You have to add to items and then call myAdapter.notifyDataSetChanged(). But the way you defined items as final you cannot add. If you want to add new element easily you should declare items as ArrayList<String>.
public class LinearLayoutDemo extends ListActivity implements OnItemClickListener {
private static List<String> items = Arrays.asList("1", "2", "4", "8", "16", "32", "64");
ListView myLV;
ArrayAdapter myAdapter;
#Override public void onCreate(Bundle icicle) {
super.onCreate(icicle);
myAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_activated_1, items);
myLV = (ListView) findViewById(android.R.id.list);
myLV.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
myLV.setAdapter(myAdapter);
items.add("128");
myAdapter.notifyDataSetChanged();
}

You need to call:
myAdapter.notifyDataSetChanged();
on the adapter after adding the elements to notify on any changes in adapter's data set, But you can't change your data set after creation of Array. use ArrayList instead.

Related

Search and Filter Android ListView

I am having a problem filtering an Android ListView using a search-bar.
Before I expanded my code and added a CustomList class it worked okay.
The CustomList class is for populating the ListView that contains Images and sub-text. My CustomList class extends ArrayAdapter.
This is the relevant code from both classes
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] languages;
public CustomList(Activity context, String[] languages) {
super(context, R.layout.list_single, languages);
this.context = context;
this.languages = languages;
}
}
and the MainActivity:
public class MainActivity extends Activity {
CustomList adapter;
ListView list;
EditText searchBar;
String[] languages = { "ActionScript", "Ajax", "Python", "Java", "JavaScript", "C++", "C#", "C", "NASM", "Assembly",
"PHP", "R", "Scala", "TeX", "Visual Basic", "Fortran", "COBOL", "Lisp", "Lua", "Ruby", "XML", };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Arrays.sort(languages);
searchBar = (EditText) findViewById(R.id.searchBarTI);
adapter = new CustomList(MainActivity.this, languages);
list = (ListView) findViewById(R.id.listVw);
list.setTextFilterEnabled(true);
list.setAdapter(adapter);
searchBar.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
MainActivity.this.adapter.getFilter().filter(cs);
}
});
}
}
When I enter text into the search-bar, only the first element in the listView is left. The rest are filtered out:
Image of the bug
I am not sure what I am leaving out or not seeing. Someone please assist.
If you are customizing the ArrayAdapter then you should implement a filterable interface for searching data in adapter.
Check out below link :
Custom getFilter in custom ArrayAdapter in android

Maintaining the previous items after the update listview

I have a problem with updating my ListView. I don't want to lose my ListView's data after the first initialization.
I searched on this site and some other sites and found different solutions. At last I used the below code, but I didn't get any result.
private void handelpost(List<Posts> posts,Page pages) {
ListView listView;
final CustomListAdapterForPostOrgan adapter;
if (pages.getCurrentPage() > 1) {
listView = (ListView) findViewById(R.id.lv_for_post_organ);
adapter = new CustomListAdapterForPostOrgan(this, posts);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
} else {
listView = (ListView) findViewById(R.id.lv_for_post_organ);
adapter = new CustomListAdapterForPostOrgan(this, posts);
listView.setAdapter(adapter);
}
}
You're creating new Adapter with new data every time. Which is not advisable when you're working with pagination. You need to do following changes.
Declare your adapter and list view and an ArrayList class level.
public class MyActivity extends Activity{
private ListView mListView;
private CustomListAdapterForPostOrgan mAdapter;
private List<Posts> mPosts;
}
Initialize your ArrayList, then Adapter and then set it as adapter to ListView once in onCreate() of Activity.
private void onCreate(Bundle savedInstanceState) {
super.onCreate();
//After you've called setContentView() etc etc.
mPosts = new ArrayList();
mListView = (ListView) findViewById(R.id.lv_for_post_organ);
mAdapter = new CustomListAdapterForPostOrgan(this, mPosts);
mListView.setAdapter(mAdapter);
}
When you receive new data, first add it to your class-level array and then call notifyDataSetChanged() like this:
private void handelpost(List<Posts> posts,Page pages) {
//add new posts to existing array..
mPosts.addAll(posts);
//since adapter already has your array, simply make it refresh.
mAdapter.notifyDataSetChanged();
}

Inserting into ArrayAdapter does not update Spinner selection

I am trying to insert an item into an Adapter that multiple Spinners are using. However when I insert into the adapter, I would like the Spinners to retain their original selection based off the object and not the position.
In the case below, the Spinner is originally selecting "four", but when I click the button to insert "three", the spinner is now set to "three" instead of updating to the new position of "four". How can I achieve this?
public class MyActivity extends Activity {
List list;
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("four");
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, list);
spinner.setAdapter(adapter);
// set selection to "four"
spinner.setSelection(2);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//list.add(2, "three") causes the same problem
adapter.insert("three", 2);
adapter.notifyDataSetChanged();
}
});
}
Should you call spinner.setAdapter(adapter); or a similar method after inserting a new value into the adaptor?
spinner.setSelection(list.indexOf("four"));
This will set the selection to "four" no matter on what position it is.

How to add elements in simple_list_item_2?

I want to add elements in simple_list_item_2. but i don't know how to add. I created 2 ArrayLists. Here is my code
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listv=(ListView) findViewById(R.id.listview);
arraylist1=new ArrayList<String>();
arraylist1.add("a");
arraylist1.add("b");
arraylist1.add("c");
arraylist2=new ArrayList<String>();
arraylist2.add("1");
arraylist2.add("2");
arraylist2.add("3");
adapter= new ArrayAdapter(this,android.R.layout.simple_list_item_2,arraylist1);
listv.setAdapter(adapter);
}
ArrayList<String> arraylist1,arraylist2;
ArrayAdapter adapter;
ListView listv;
}
Just as you added the elements before you can add them anytime you want as,
arraylist1.add("a");
And then you just have notify the adapter that the data had been changed, to do that put the below line of code after you add or remove data from the array list...
adapter.notifyDatasetChanged();

How to add an Array of TextViews into a ListView?

I got this piece of code that creates new TextView, then adds it to ArrayList<View> and when it is finished adding TextViews to Array it sets adds that Array into ListView. But somehow my ListView is appearing empty. Any idea what am I doing wrong?
Here is the code:
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayList<View> textvs = new ArrayList<View>();
for (int i=0; i<10;i++) {
TextView tv = new TextView(MainActivity.this);
tv.setText(""+i);
textvs.add(tv);
}
lv.addTouchables(portit); // lv is my listview
You should use ArrayAdapter. You did this the wrong way. Here is a sample:
public class ArrayAdapterDemo extends ListActivity {
String[] items = { "this", "is", "a", "really", "silly", "list" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1,
items));
}

Categories

Resources