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"...
Related
Is it possible to provide values to the AutoCompleteTextView in XML / via resources, therefore without setting an adapter in code?
I want to use the AutoCompleteTextView as part of an exposed dropdown menu, so all values shall be shown at once and no filtering shall happen. Also all values to be shown are known at compile time.
I think you can do that by using the list via Resource and implementing it in AutoCompleteTextView passing by the adapter.
See the example here :
public class CountriesActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
Resources res = getResources();
private static final String[] COUNTRIES = res.getStringArray(R.array.planets_array);
}
Hope this could help you or any one else .
Yes its possible. There is a method showDropDown() which you can call.
Try something like
autoCompleteTextView.showDropDown()
Reference :
https://developer.android.com/reference/android/widget/AutoCompleteTextView.html#showDropDown()
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
I have a class that extends ListActivity with a SimpleAdapter as the list adapter. My code looks like this:
public class ListOfFirms extends ListActivity {
Intent extras;
int time;
String km;
ArrayList<String> firms = new ArrayList<String>();
SimpleAdapter adapter;
static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firms);
extras = getIntent();
time = extras.getIntExtra("time", 0);
km = extras.getStringExtra("km");
adapter = new SimpleAdapter(
this, list, R.layout.taxi_custom,
new String[] {"name","price"},
new int[] {R.id.taxi_name,R.id.taxi_price});
initializeFirm();
setListAdapter(adapter);
}
My question is how I can add a button to each element in the list, the button should be floating to right. My list contains object of the class Firm, how can I know which object that I grab out from the list, when a user presses this button?
here is example of custom listview which may help you
use custom adapter....
and set
listview.setAdapter(adapter);
You will have to write a CustomAdapter which extends BaseAdapter.
If you can use OnTouchListener and OnLongClickListener instead of a button it is a little easier to implement. Also if you just want to select the item, it ies easier tu use the standard built-in Android mechanisms.
Only if you REALLY need a button on each list item you have to do it like Gaurav Agarwal suggested... - which is something you might have to do sooner or later anyway :-)
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 have a question about the ListView and how to use it. My Prolem is that my listView is only a part of the view and I am not sure how to do this.
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView) findViewById(R.id.ListView01);
String[] strings = new String[]{"Test1","Test2"};
ArrayAdapter<String> myArrayAdapter= new ArrayAdapter<String>(this, R.id.ListView01,strings);
myListView.setAdapter(myArrayAdapter);
I think the problem is the "this" in myArrayAdapter!?
The layout resource id you're supposed to pass to ArrayAdapter is a layout that's used to render each item in the list, not the layout for the list itself. Android provides some layout resources for the common cases. Try using:
ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);