I have a listview with many items and an icon for all.I want to set icon for each item with java code or xml layout
picture:http://axgig.com/images/29987820360372481858.png
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
myArray=new ArrayList<String>();
final ListView lv =(ListView) findViewById(R.id.li1);
myArray.add("1" );
myArray.add("2");
myArray.add("3");
myArray.add("4");
myArray.add("5");
ad = new ArrayAdapter <String> (this , R.layout.text ,myArray);
lv.setAdapter(ad);
you have to create a custom Adapter to push icons into the list view.
follow this tutorial for better understanding
Related
I'm working on an app that has a MainActivity and has many ImageViews for hospitals, clinics, ... When pressing one of them it takes you to a new activity DisplayActivity that loads a pre-populated ListView. It seems illogical to have a separate activity for each one.
My question is can i use one activity to load different ListViews depending on which one was chosen. I know it can be done by XML, adding all ListViews in the DisplayActivity and make them visible/gone, but i want a more dynamic way, such as loading the desired ListView depending on the chosen item in the MainActivity.
This is how my Display activity currently looks like:
public class DisplayActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Display list of hospitals
String[] doctors = getResources().getStringArray(R.array.hospitalList); // Get array List ID
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, doctors);
ListView lv = (ListView) findViewById(R.id.hospitalListView);
lv.setAdapter(adapter);
}
}
Just define a convenience function:
void setListViewContent(#ArrayRes int arrayId) {
String[] items = getResources().getStringArray(arrayId); // Get array List ID
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, items);
ListView lv = (ListView) findViewById(R.id.hospitalListView);
lv.setAdapter(adapter);
}
and then
setListViewContent(R.array.doctors);
setListViewContent(R.array.mcdonalds);
I am just now learning about how listViews work. I currently have a ListView in which each row is an image, with two TextViews to its right. The top TextView is larger and contains a name, the bottom one is smaller and contains a surname. I currently have an ArrayAdapter set up properly to populate the names. However, I can't quite figure out how to also populate the surnames, as doing the same exact thing seems to not work. My code currently looks like this. The k ListView is the one I'm trying to implement for surnames. This is obviously an incorrect approach, though. If I run that code, it will no longer show the names, but only the surnames.
ListView l;
ListView k;
String[] names = {"Andrew","Billy","Charlie","Daniel","Eric","Frank","George","Hal"};
String[] surNames = {"Ainbinder","Brenton","Chen","Donovan","Epstein","Ferris","Gallivan","Higgins"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (ListView) findViewById(R.id.listView1);
k = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.single_row, R.id.topLine, names);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.single_row, R.id.secondLine, surNames);
l.setAdapter(adapter);
k.setAdapter(adapter2);
l.setOnItemClickListener(this);
}
The way you are implementing is not correct. You can follow the link below for correct implementation:
Multiple TextViews in the same List Item
I want to customize android listView. I need an icon at right side, header with green color and larger font and sub header with black color and smaller font and again another icon at the left. It should look like following:
Following is my code:
Class MyList extends Activity{
ListView listView ;
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
String iconUrl = "http://www.someurl.com/icon.png";
String iconUrl2= "http://www.someurl.com/icon2.png";
int counter = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
listView = (ListView) findViewById(R.id.list);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems);
listView.setAdapter(adapter);
someFunctionOnButtonClicked();
//following function to add items to list dynamically
public void someFunctionOnButtonClicked(){
String string1 = "List item "+counter+" Header";
String string2 = "List item "+counter+" sub-header";
listItems.add(string1+"\n"+string2);
adapter.notifyDataSetChanged();
}
Now how can I set different font-weight and color for two headers on same list item? How can I include images on both end? Please help. Thank you.
Create a custom Adapter for the list by extendsing the ArrayAdapter class.
This is an excellent tutrial Adapter implementations - vogella
You will need to implement your own custom ArrayAdapter for this, and set the text color programmatically inside the getView method. Have a look at this example.
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"...
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);