How to implement a "main menu" ListView? - android

I have an activity that displays a list of "main menu" items. Each list item should start a new activity when clicked. I do this by loading the appropriate activity using it's class name.
Code:
public class MenuActivity extends ListActivity {
String classes[] = { "AboutUsActivity", "CompanyProfileActivity", "ProductsActivity", "ContactActivity" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try {
Class ourClass = Class.forName("com.example.test." + cheese);
Intent ourIntent = new Intent(this, ourClass);
startActivity(ourIntent);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Because I'm storing class names in the list, that is what is shown to the user, so it's not very user-friendly. How can I fix this?
For example, instead of showing "AboutUs", how can I make the list show something like "About Us" or "About Company's Name"?

Try this:
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MenuActivity extends ListActivity
{
private String[] menuItems = { "About Us", "Company Profile", "Products", "Contact" };
private String[] menuClassNames = { AboutUsActivity.class.getName(), CompanyProfileActivity.class.getName(), ProductsActivity.class.getName(), ContactActivity.class.getName() };
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuItems));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
try
{
Intent intent = new Intent(this, Class.forName(menuClassNames[position]));
startActivity(intent);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
Alternatively, you could make menuClassNames a Class[], and then you wouldn't need to go the extra step of loading the class by name.

Use 2 arrays, one for the classes and other one for the names you want to display.

Related

How to load an specific activity(Intent) on Listview(item)Click?

package com.example.app;
import com.example.app.adaptor.*;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
public class ListviewExample extends ListActivity {
static final String[] ASDFGH = new String[] { "ABC", "PQR",
"XYZ", "RAA","AA" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new etcArray(this, main_activity ));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// get selected items
String selectedValue = (String) getListAdapter().getItem(position);
if(selectedValue.equalsIgnoreCase("ABC"))
{
Intent in = new Intent(getApplicationContext(),ABC.class);
startActivity(in);
}
else
if(selectedValue.equalsIgnoreCase("AA"))
{
Intent in = new Intent(getApplicationContext(),AA.class);
startActivity(in);
}
}
In the above program ..using normal condition like if string.equalsIgnorecase("AA")
we can launch the intent on that specific click !! is there any method to launch an activity in any listview Item Click ???
Create class array as well So you can point to exact activity easily ...
static final Class[] classArray = new Class[] { ABC.class,PQR.class,XYZ.class,RAA.class,AA.class };
static final String[] ASDFGH = new String[] { "ABC", "PQR",
"XYZ", "RAA","AA" };
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
Intent intent=newIntent(getApplicationContext(),classArray[position]);
startActivity(intent);
}
Try to run following code.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// get selected items
if(position == 0)
{
Intent intent=newIntent(this, Class1.class);
startActivity(intent);
} else if(poition == 1)
{
Intent intent=newIntent(this, Class2.class);
startActivity(intent);
} else if(poition == 1)
Intent intent=newIntent(this, Class3.class);
startActivity(intent);
}
You can use following code :
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Class<?> nextClass = null;
// get selected items
String selectedValue = (String) getListAdapter().getItem(position);
if(selectedValue.equalsIgnoreCase("ABC"))
nextClass = ABC.class;
else if(selectedValue.equalsIgnoreCase("AA"))
nextClass = AA.class;
if(nextClass != null)
startActivity(new Intent(context, nextClass));
}

Android ListView does not update onResume

I made an Activity for searching people that also shows history of recent research.
If I long click on an item of the history it asks me if I want to delete it. If I press "Yes" it deletes the list item.
So, I write something and click to "Search" button. This brings me in another Activity with results. Here I click on result so it stores the person info and brings me in the person page.
When I come back I don't see the new person in the history.
So I overwritten onResume() but it still not work and now I cannot delete items from the history list.
Here the code:
package com.lpsmt.proffinder;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.lpsmt.R;
public class HomeActivity extends Activity
{
protected Db db = null;
protected List<ProfBean> historyProfs = null;
protected ProfListItemAdapter listAdapter = null;
protected ListView listView = null;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.db = new Db(this);
this.setContentView(R.layout.prof_finder_home);
this.historyProfs = this.db.getHistory(-1); // -1 means with no limits
this.listAdapter = new ProfListItemAdapter(HomeActivity.this, R.id.prof_finder_history_list_view, this.historyProfs);
this.listView = (ListView) this.findViewById(R.id.prof_finder_history_list_view);
listView.setAdapter(this.listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(HomeActivity.this, ProfPageActivity.class);
Bundle bundle = new Bundle();
bundle.putString("profId", HomeActivity.this.historyProfs.get(position).getProfId());
intent.putExtras(bundle);
HomeActivity.this.startActivity(intent);
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id)
{
Resources resources = HomeActivity.this.getResources();
String title = resources.getString(R.string.prof_finder_history_delete_title);
String message = resources.getString(R.string.prof_finder_history_delete_message);
AlertDialog.Builder adb=new AlertDialog.Builder(HomeActivity.this);
adb.setTitle(title);
adb.setMessage(message);
final int positionToRemove = position;
String positive = resources.getString(R.string.prof_finder_history_delete_positive);
String negative = resources.getString(R.string.prof_finder_history_delete_negative);
adb.setNegativeButton(negative, null);
adb.setPositiveButton(positive, new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ProfBean prof = HomeActivity.this.historyProfs.get(positionToRemove);
HomeActivity.this.db.deleteProf(prof.getProfId());
HomeActivity.this.historyProfs.remove(positionToRemove);
HomeActivity.this.runOnUiThread(new Runnable() {
public void run() {
HomeActivity.this.listAdapter.notifyDataSetChanged();
}
});
}});
adb.show();
return true;
}
});
}
public void searchProf(View view) throws Exception
{
EditText queryEditText = (EditText) this.findViewById(R.id.prof_finder_search_query);
String query = queryEditText.getText().toString().trim();
queryEditText.setText(query);
if (query.length() < 3) {
String message = this.getResources().getString(R.string.prof_finder_query_too_short);
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(HomeActivity.this, SearchResultActivity.class);
Bundle bundle = new Bundle();
bundle.putString("query", query);
intent.putExtras(bundle);
this.startActivity(intent);
}
public void onResume()
{
super.onResume();
this.historyProfs = this.db.getHistory(-1);
this.listAdapter.notifyDataSetChanged();
}
}
You haven't set any new data to list view. Thats why your new contact isn't added to the list after notifyDataSetChanged(). You need to add some method into adapter like
setData(List<ProfBean> data)
{
this.currentAdaptersList= data;
}
and then call notifyDataSetChanged(). So the final onResume will be :
public void onResume()
{
super.onResume();
this.historyProfs = this.db.getHistory(-1);
this.listAdapter.setData(this.historyProfs);
this.listAdapter.notifyDataSetChanged();
}
Enjoy.
And using onResume() for this task is bad idea. Is better to use onActivityResult.
notifyDataSetChanged() didn't work for me either. I was able to solve this a little bit differently:
I use OnStart() (in a derived class from Fragment)
I use setNotifyOnChange() of the ArrayAdapter:
ListView listView = (ListView) findViewById(R.id.logListView);
listView.setAdapter(logAdapter);
logAdapter.setNotifyOnChange(true);
I create the adapter once:
logAdapter = new ArrayAdapter(activity, android.R.layout.simple_list_item_1, activity.logMessages);
in onViewCreated().

How to create ListMenu dynamically?

I'm working on creating a dictionary and I'm trying to create ListMenu dynamically as it is created automatically while filtering words from database alphabetically.
I created a ListMenu manually, but cannot turn it into dynamic one. Does anyone have an idea how to create the ListMenu dynamically?
Here's my code:
package ge.gtug;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Settings extends ListActivity{
String listItems[] = {"AboutUs", "AboutApp", "Feedback"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Settings.this, android.R.layout.simple_list_item_1, listItems));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
String koba = listItems[position];
super.onListItemClick(l, v, position, id);
try{
Class myClass1 = Class.forName("ge.gtug." + koba);
Intent myIntent1 = new Intent(Settings.this, myClass1);
startActivity(myIntent1);
}catch(ClassNotFoundException e){
e.printStackTrace();
try {
Class myClass2 = Class.forName("ge.gtug." + koba);
Intent myIntent2 = new Intent(Settings.this, myClass2);
startActivity(myIntent2);
}catch(ClassNotFoundException o){
o.printStackTrace();
}
}
}
}
Instead of using an Array for storing data use a List (ArrayList, Vector, or similar). And whenever you alter the data collection you need to inform the ArrayAdapter that the data has changed - to notify the array adapter that the list has changed you call the method
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Settings.this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adapter);
listItems.add("new string");
adapter.notifyDataSetChanged();
http://developer.android.com/reference/android/widget/ArrayAdapter.html#notifyDataSetChanged%28%29

android development eclipse (list activity)

It has the 'class' underlined in yellow and says its a warning saying that 'Class is a raw type. References to generic type Class should be parameterized'
Class ourClass = Class.forName("com.thenewboston.jack." + cheese);
I have tried all the quick fixes and every thing. I don't know what to do. Could any one help and tell me what to do?
Here is the rest of the code and where the code is placed:
package com.thenewboston.jack;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity{
String classes[] = {"Startingpoint", "Example1", "Example2", "Example3"
, "Example4", "Example5", "Example6", "Example7", "Example7", "Example7"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_expandable_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try{
Class ourClass = Class.forName("com.thenewboston.jack." + cheese);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
Modify like this
Class<Activity> ourClass = Class.forName("com.thenewboston.jack." + cheese);
or simply suppress this warnig using:
#SuppressWarnings("rawtypes")
above the line which is generating warning.

In Eclipse setting classes as list view forces close

After following code and showing no errors when I use my button to access my list view it forces close my code is as follows
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Listview extends ListActivity {
String classNames[] = {"home1", "Sweet", "tutorial2"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, classNames));
}#Override
protected void onListItemClick (ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
String openClass = classNames[position];
try{
Class selected = Class.forName("us.beats.with." + openClass);
Intent selectedIntent = new Intent(this, selected);
startActivity(selectedIntent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
where the button was set up I had Button Listview = (Button) findViewById(R.id.Listview);
Listview.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("us.beats.with.Mylist"));
mpButtonClick.start();
but start activity was us.beats.with.Listview should have been the above
Change your classname and run it will work fine
public class MyList extends ListActivity {
String classNames[] = {"home1", "Sweet", "tutorial2"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, classNames));
}#Override
protected void onListItemClick (ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
String openClass = classNames[position];
try{
Class selected = Class.forName("us.beats.with." + openClass);
Intent selectedIntent = new Intent(this, selected);
startActivity(selectedIntent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
Now run the above code

Categories

Resources