1. Download our Official Android App: Forums for Android!

Apps How to use listview with icons to open different activities

Discussion in 'Android Development' started by louis84, Sep 8, 2011.

  1. louis84

    louis84 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Sep 8, 2011

    Sep 8, 2011
    4
    0
    5
    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 (Text):
    1.    
    2.  package mCRM.android.hp;
    3.  
    4. import android.app.ListActivity;
    5. import android.content.Context;
    6. import android.content.Intent;
    7. import android.os.Bundle;
    8. import android.view.LayoutInflater;
    9. import android.view.View;
    10. import android.view.ViewGroup;
    11. //import android.widget.AdapterView;
    12. import android.widget.ArrayAdapter;
    13. import android.widget.ImageView;
    14. import android.widget.ListView;
    15. import android.widget.TextView;
    16. import android.widget.Toast;
    17. public class first extends ListActivity {
    18. public class MyCustomAdapter extends ArrayAdapter<String> {
    19. public MyCustomAdapter(Context context, int textViewResourceId,
    20. String[] objects) {
    21. super(context, textViewResourceId, objects);
    22. // TODO Auto-generated constructor stub
    23. }
    24. @Override
    25. public View getView(int position, View convertView, ViewGroup parent) {
    26. // TODO Auto-generated method stub
    27. //return super.getView(position, convertView, parent);
    28. LayoutInflater inflater=getLayoutInflater();
    29. View first=inflater.inflate(R.layout.first, parent, false);
    30. TextView TextView01=(TextView)first.findViewById(R.id.listView1);
    31. TextView01.setText(DayOfWeek[position]);
    32. TextView tv1 = (TextView)first.findViewById(R.id.listView1);
    33. tv1.setText(choose[position]);
    34. ImageView icon=(ImageView)first.findViewById(R.id.icon);
    35.  
    36. if (DayOfWeek[position]=="Opportunities"){
    37. icon.setImageResource(R.drawable.opportunities);
    38. }
    39. else if(DayOfWeek[position]=="Accounts"){
    40.  icon.setImageResource(R.drawable.accts);
    41. }
    42. else if(DayOfWeek[position]=="Contacts"){
    43. icon.setImageResource(R.drawable.contacts);
    44. }
    45. else if(DayOfWeek[position]=="Analytics"){
    46.  icon.setImageResource(R.drawable.analytics);
    47. }
    48. else{
    49.  icon.setImageResource(R.drawable.bookmar);
    50. }
    51.  
    52. return first;
    53. }
    54. }
    55. String[] DayOfWeek = {"Opportunities", "Accounts", "Contacts",
    56. "Analytics", "Bookmarks"}; // this is the array for the icons of the list
    57. String[] choose = {"opportunities", "accounts", "contacts",
    58.   "analytics", "bookmarks"}; //this is the array for the the different activities.
    59. /** Called when the activity is first created. */
    60. @Override
    61. public void onCreate(Bundle savedInstanceState) {
    62.    super.onCreate(savedInstanceState);
    63.    // setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,DayOfWeek));
    64.    setListAdapter(new MyCustomAdapter (first.this, R.layout.first, DayOfWeek));
    65.  
    66. }
    67. @Override
    68. protected void onListItemClick(ListView lv, View view, int position, long id) {
    69. // TODO Auto-generated method stub
    70.  
    71.  //ArrayAdapter = (MyCustomAdapter)
    72. super.onListItemClick(lv, view, position, id);
    73.  //setContentView(R.layout.main);
    74. //String selection = lv.getItemAtPosition(position).toString();
    75. String openClass = choose[position];
    76.  
    77. try{
    78.  Class selected = Class.forName("mCRM.android.hp."+ openClass);
    79.  Intent selectedIntent = new Intent(this,selected);
    80.  startActivity(selectedIntent);
    81. }
    82. catch(ClassNotFoundException e){
    83.  e.printStackTrace();
    84. }
    85. Toast.makeText(this, openClass, Toast.LENGTH_LONG).show();
    86. }
    87.  
    88. }
    89.  

    Thanks in advance
     

    Advertisement

  2. miXer

    miXer Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    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 (Text):
    1. Intent i = null;
    2.  
    3. switch (position)
    4. {
    5.     case 0:
    6.         i = new Intent(this, opportunities.class);
    7.         break;
    8.     case 1:
    9.         i = new Intent(this, accounts.class);
    10.         break;
    11.     case 2:
    12.         i = new Intent(this, contacts.class);
    13.         break;
    14.     case 3:
    15.         i = new Intent(this, analytics.class);
    16.         break;
    17.     case 4:
    18.         i = new Intent(this, bookmarks.class);
    19.         break;
    20.     default:
    21.         return;
    22. }
    23.  
    24. if (i != null)
    25.     startActivity(i);
    26.  
     
  3. louis84

    louis84 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Sep 8, 2011

    Sep 8, 2011
    4
    0
    5

Share This Page

Loading...