I have to display a list of messages and make it clickable ,i have read ListView andtried to use it,but i have data in something like below code,how will i add to adapter? from this list i will loop and get messages say list.get(i).getMessage(); which has to be displayed and there are multiple messages.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.reminderlist);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.reminderlayout);
setContentView(R.layout.reminderlayout);
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
ArrayList<GetReminder> list = (ArrayList<GetReminder>) getIntent().getSerializableExtra("reminderList");
System.out.println("size is >>>"+list.size());
// Binding resources Array to ListAdapter
}
Implement your custom adapter parametrized with GetReminder. Take a look here for example. Than you can do something like this:
ArrayList<GetReminder> list = (ArrayList<GetReminder>) getIntent().getSerializableExtra("reminderList");
ListView listView = findViewById(R.id.listview);
MyCustomAdapter adapter = new MyCustomAdapter(this, 0);
adapter.addAll(list)
or just pass your list as a third parameter in MyCustomAdapter constructor.
You can do
setListAdapter(new ArrayAdapter<GetReminder>(getApplicationContext(), R.layout.reminderlayout,(GetReminder[]) list.toArray()));
considering this code is in onCreate() method of your class that extends ListActivity
Related
I am just starting out using Android, so this is probably a very basic question.
I have created an array called priorityNames. I want to display that in a list and be able to make a selection from that list. At this stage, I cannot get the list to display.
As a starting point I used a short example from windrealm.org tutorials
Any help would be appreciated. Also if anyone can point me to a better example it would be appreciated.
public class SimpleListViewActivity extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, R.array.priorityNames);
mainListView.setAdapter(listAdapter);
}
}
You are passing just an id with R.array.priorityNames.
Use:
getResources().getStringArray(R.array.priorityNames)
You are probably missing a TextView in your xml layout (simplerow.xml) with id:
...android:id="#android:id/text1"...
Could someone tell me what is wrong with this piece of code?
It is supposed to be a ListView from an XML file that is then referred to in Java.
Alas, it crashes my application every time it enters the Menu class.
public class Menu extends ListActivity {
String Name_for_classes[] = {"- 1-9 Tabels -", "- 10-19 Tabels -", "- 20-29 Tabels -" };
String Tabel_classes[] = {"First", "Second", "Third"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.menu, Name_for_classes));
ListView list = getListView();
list.setTextFilterEnabled(true);
}
}
Okay, let's assume you have your ListView in an XML file called my_listview.xml.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_listview.xml);
ListView list = (ListView)findViewById(R.id.list);
ArrayAdapter<String> yourAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Name_for_classes);
list.setAdapter(yourAdapter);
}
I'm not sure what is in R.layout.menu, but guessing by the naming its the activity layout. This should be used with setContentView(R.layout.menu) in onCreate. The layout that is passed into the ArrayAdapter is the TextView you are using to populate the listview.
You have forgotten to call the setContentView in your onCreate method, so your listview is not referenced yet.
When you use setContentView, it is equivalent as sayin 'for this activity I want to use the template 'myTemplate.xml'
After that, you have to 'linked' your java ListView attribute to the listview declared in your template.
I am developing an android application in which i have to place 5 items with 5 icons in a list.Has anyone implemented it before?
If Yes,Can he help me how to implement it?
Thanks
tushar
you can try from this
Dynamic ListView in Android app
public class ListViewDemo extends ListActivity {
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems=new ArrayList<String>();
//DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
ArrayAdapter<String> adapter;
//RECORDING HOW MUCH TIMES BUTTON WAS CLICKED
int clickCounter=0;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v) {
listItems.add("Clicked : "+clickCounter++);
adapter.notifyDataSetChanged();
}
}
android.R.layout.simple_list_item_1 is default list items layout supplied by android and >you can use this stock layout for non complex things.
listItems is an array list which holds the data shown in the ListView and all the >insertion and removal should be done on listItems the changes in list should reflect in >the view and thats handled by ArrayAdapter adapter which should be notified using
adapter.notifyDataSetChanged();
Adapter is instantiated with 3 paramters the context which could be your >activity/listactivity the layout of you individual list item and lastly the list which is >the actual data to be displayed in the list.
In my activity there is a listview containing lines of a text file. To populate this listview i created an arrayadapter in the onCreate(Bundle savedInstanceState) method. The listview is populated with items if there are lines in the file. The arrayadapter part looks like this:
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
ArrayAdapter<String> adapter1;
adapter1 = new ArrayAdapter<String>(Main.this,R.layout.list_black_text,R.id.list_content, assignArr0);
lv1.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
Then by clicking on Menu item1 (so now we are outside the onCreate(Bundle savedInstanceStat) method i write one line in the text file. For now everything works fine. If i exit and reopen the app the new line appears in the listview. But the listview is not refreshed after adding the new line, so at the end of the " case R.id.Menu1:" part.
So after adding one line to the file I want to refresh the listview by reading the lines from the file to an array then populating the listview with that array. Problem is that i cannot do that, even if i put the ArrayAdapter<String> adapter1; line outside the onCreate() method where the other arrays and variables are declared.
It does not see the listview (lv1 cannot be resolved).
If i define the ListView where the other variables are (ListView lv1;) this whole part of the program is not working.
I also need to empty the listview (the adapter?) by adapter1.clear(); or sg before populating it.
Any ideas are welcome.
If you need those variables to be global, you should declare them as
private final ArrayList<String> assignArr0 = new ArrayList<String>();
private ListView lv1;
private ArrayAdapter<String> adapter1;
#Override
public void onCreate(Bundle icicle)
{
lv1 = (ListView) findViewById(R.id.ListView01);
adapter1 = new ArrayAdapter<String>(Main.this,R.layout.list_black_text,
R.id.list_content, assignArr0);
lv1.setAdapter(adapter1);
}
You should set the adapter to the ListView only one time, inside the onCreate method of your activity, and the notifiyDatasetChanged should be called after you made changes to the list (the array used in your ListAdapter).
Say you have a method to add the recently read lines not yet included in your list. After adding the lines to the ArrayList used in the ListAdapter, you should call the notifyDatasetChanged method, to visualize the new items in the ListView:
private void addLinesToList(final String... newLines)
{
assignArr0.addAll(Arrays.asList(newLines));
adapter1.notifyDataSetChanged();
}
If you are reading the whole file every time it changes, and populate it from top (not just inserting the new lines), you have to first remove all the elements of the list, so the first line of the addLinesToList method should be:
assignArr0.clear();
I have a list of ListItems's in my listview. The ListItem has a name instance variable. I am trying to get the text of each of the items just to be the name.
How can I do this?
public class List extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<ListItem>(this, R.layout.list_item, Previousclass.LinkedList-from-previous-class));
Right now it is just displaying the memory location of each item i.e. ListItem#47234c10
You have to extend ArrayAdapter to implement getView(). The ArrayAdapter implementation is returning the toString() value of your ListItem object.
Or alternatively stringify your data before adding it to the adapter.
What is Previousclass.LinkedList-from-previous-class ? Is it a list of your items?
If yes,
Your list of items should be a list of names of items. Your resulting screen should have listview in it and the adapter you created should be used this way
result.setListAdapter(Youradapter)
I am just giving you sample based on what code you have posted. If you need details, please elaborate your code.
result.setListAdapter(new ArrayAdapter<ListItem>(this, R.layout.list_item, Previousclass.LinkedList-from-previous-class))