Single activity vs multiple - android

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);

Related

Edit Text and put it to List View

I'm kinda new to Android Studio so I will be grateful if you can help me with my question. How can I enter some text and add it to a ListView with the use of a Bundle?
For example, say I enter a name in the EditText component in the MainActivty, and then when I press the OK button, it will be seen in to another Activity in a List View.
I've been using Bundles to transfer text to another Activity but I can't figure out how to transfer text to ListView.
If all you want to do is display strings in your list items, you can do it fairly simply, otherwise you will have to make a custom adapter. For the former, this is what you will want to do
Create an ArrayList<String> where you will store your values that are entered from the EditText.
Create an ArrayAdapter<String> that will serve to connect the ArrayList to the ListView.
Your final code will look something like this:
public class MainActivity extends Activity {
private ListView listView;
List<String> strings;
ArrayAdapter<String> arrayAdapter;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.yourLayout);
listView= (ListView) findViewById(R.id.yourListView);
strings = new ArrayList<String>();
strings.add("list item 1");
strings.add("list item 2");
arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
strings);
listView.setAdapter(arrayAdapter);
}
}
Then in the onClick of the "OK" button simply add the string from the EditText to the arrayList, and notify the listView that it should be updated be calling this:
arrayAdapter.notifyDataSetChanged();
I don't think you can use Bundles for what you want to achieve. Here's how I do it...
First you need to set up your list. I would use global variables:
ListView listView;
EditText editText;
ArrayList<String> strings;
ArrayAdapter<String> adapter;
Now, in onCreate() , you can do...
listView = (ListView) findViewById(R.id.your_listview);
editText = (EditText) findViewById(R.id.your_edittext );
strings = new ArrayList<>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
listView.setAdapter(adapter);
You will need to set the OnClickListener for your Button. I would do this in onCreate() too:
Button button = (Button) findViewById(R.id.your_button);
button .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here we get the text from the EditText component
String text = editText.getText().toString();
// Now add it to the list
strings.add(text);
// And finally, update the list
adapter.notifyDataSetChanged();
}
});

xml string-array to listView not working

public class Menu extends Activity {
String[] categories;
ListView lv;
Cursor cursor;
Context context;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
categories = getResources().getStringArray(R.array.Categories_Array);
lv = (ListView) findViewById(R.id.listViewCategories);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.activity_main,
getResources().getStringArray(R.array.Categories_Array));
lv.setAdapter(adapter);
}
...
}
I keep getting an "unfortuneately Bible CYB has stopped working" when I run the app
The problem you have is that you are giving your ArrayAdapter a full Activity's layout to inflate. What you want to do is pass it the row's layout it should use for each of the individual rows it should inflate:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, // or other basic row layout
getResources().getStringArray(R.array.Categories_Array));
You have set the activity_main.xml to the activity and use the same in the constructor of ArrayAdapter. To display the a single string in each row
Change this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.activity_main,
getResources().getStringArray(R.array.Categories_Array));
to
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.simple_list_item_1,
categories);
where simple_list_item_1.xml is the layout from the android framework
http://androidxref.com/4.4.3_r1.1/xref/frameworks/base/core/res/res/layout/simple_list_item_1.xml
And the constructor
public ArrayAdapter (Context context, int resource, T[] objects)
Added in API level 1 Constructor
Parameters context The current context. resource The resource ID for a
layout file containing a TextView to use when instantiating views.
objects The objects to represent in the ListView.
Instead of using a array adapter try using a list adapter.. it wud give it a custom layout u designed ..
lv = (ListView) findViewById(R.id.listViewCategories);
ListAdapter adapter = new SimpleAdapter(this,
/*id for your custom layout for ex: R.layout.activity_main*/,
getResources().getStringArray(R.array.Categories_Array),/*Here write the id of the text view where u want to display the info.. forex: R.id.tvExample*/);
lv.setListAdapter(adapter);

To populate a ListView with more than one "list" of information, would I create another adapter or use some other method?

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

How to set icon to items of listview?

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

Android ListView understanding

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);

Categories

Resources