• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to use listview with icons to open different activities

louis84

Lurker
Sep 8, 2011
4
0
I am new to android development and will greatly appreciate any help i can get.
I have a listview with icons and am having difficulty opening different activities from the items of the list.
the code is as follows:

Code:
 package mCRM.android.hp;
 
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class first extends ListActivity {
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
LayoutInflater inflater=getLayoutInflater();
View first=inflater.inflate(R.layout.first, parent, false);
TextView TextView01=(TextView)first.findViewById(R.id.listView1);
TextView01.setText(DayOfWeek[position]);
TextView tv1 = (TextView)first.findViewById(R.id.listView1);
tv1.setText(choose[position]);
ImageView icon=(ImageView)first.findViewById(R.id.icon);

if (DayOfWeek[position]=="Opportunities"){
icon.setImageResource(R.drawable.opportunities);
}
else if(DayOfWeek[position]=="Accounts"){
 icon.setImageResource(R.drawable.accts);
}
else if(DayOfWeek[position]=="Contacts"){
icon.setImageResource(R.drawable.contacts);
}
else if(DayOfWeek[position]=="Analytics"){
 icon.setImageResource(R.drawable.analytics);
}
else{
 icon.setImageResource(R.drawable.bookmar);
}

return first;
}
}
String[] DayOfWeek = {"Opportunities", "Accounts", "Contacts",
"Analytics", "Bookmarks"}; // this is the array for the icons of the list
String[] choose = {"opportunities", "accounts", "contacts",
  "analytics", "bookmarks"}; //this is the array for the the different activities. 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   // setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,DayOfWeek));
   setListAdapter(new MyCustomAdapter (first.this, R.layout.first, DayOfWeek));
  
}
@Override
protected void onListItemClick(ListView lv, View view, int position, long id) {
// TODO Auto-generated method stub
 
 //ArrayAdapter = (MyCustomAdapter)
super.onListItemClick(lv, view, position, id);
 //setContentView(R.layout.main);
//String selection = lv.getItemAtPosition(position).toString();
String openClass = choose[position];

try{
 Class selected = Class.forName("mCRM.android.hp."+ openClass);
 Intent selectedIntent = new Intent(this,selected);
 startActivity(selectedIntent);
}
catch(ClassNotFoundException e){
 e.printStackTrace();
}
Toast.makeText(this, openClass, Toast.LENGTH_LONG).show();
}

}


Thanks in advance
 
Have you inserted all activities in the AndroidManifest.xml file? If so, why not make a switch statement instead? That wat you know you have the right class names.

Code:
Intent i = null;

switch (position)
{
    case 0:
        i = new Intent(this, opportunities.class);
        break;
    case 1:
        i = new Intent(this, accounts.class);
        break;
    case 2:
        i = new Intent(this, contacts.class);
        break;
    case 3:
        i = new Intent(this, analytics.class);
        break;
    case 4:
        i = new Intent(this, bookmarks.class);
        break;
    default:
        return;
}

if (i != null)
    startActivity(i);
 
Upvote 0
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones