Problem displaying ListView

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problem displaying ListView

Postby chouman » Fri Jan 04, 2008 6:51 am

I am trying to create a main menu page with a header image and then a menu list that user can select which will bring them further into each item.

For some reason (maybe the way i am using the adapter is wrong), the imageView displays on the top but not the listView. here is the code snippet:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public MenuView (Context context)
  2.  
  3.         {
  4.  
  5.                 super(context);
  6.  
  7.        
  8.  
  9.                 //create a new imageview to store the drawable header
  10.  
  11.                 iView = new ImageView(context);
  12.  
  13.                 iView.setImageDrawable(getResources().getDrawable(R.drawable.header));
  14.  
  15.                
  16.  
  17.                 //create an arraylist to store the menu items
  18.  
  19.                 ArrayList<String> menuTitle = new ArrayList<String>();
  20.  
  21.                 menuTitle.add("Schedule");
  22.  
  23.                 menuTitle.add("Detail");
  24.  
  25.                 menuTitle.add("Help");
  26.  
  27.                                
  28.  
  29.                 //create the view with the list of menu title
  30.  
  31.                 lView = new ListView(context);
  32.  
  33.                 aAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, menuTitle);
  34.  
  35.                 lView.setAdapter(aAdapter);
  36.  
  37.                
  38.  
  39.                 //now add the image and list view to the parent
  40.  
  41.                 addView(iView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  42.  
  43.                 addView(lView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  44.  
  45.                
  46.  
  47.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Also, do i just have to set listener on each seletable to open subactivity for each item?

thanks for all your help
chouman
Developer
Developer
 
Posts: 27
Joined: Thu Jan 03, 2008 2:59 pm

Top

Postby plusminus » Fri Jan 04, 2008 11:40 am

Hello chouman,

with me it looks like what i attached (second image) (changed image to icon.png). You want the image in the top followed by the list :?:

Then create a Linearlayout, add image and list to it, then add the Linearlayout to the View itself.

[Edit]
The upper attached screenshot results from this code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 //create a new imageview to store the drawable header
  2.  
  3.                 ImageView iView = new ImageView(this);
  4.  
  5.                 iView.setEnabled(false);
  6.  
  7.                 iView.setImageDrawable(getResources().getDrawable(R.drawable.icon));
  8.  
  9.  
  10.  
  11.                 //create an arraylist to store the menu items
  12.  
  13.                 ArrayList<String> menuTitle = new ArrayList<String>();
  14.  
  15.                 menuTitle.add("Schedule");
  16.  
  17.                 menuTitle.add("Detail");
  18.  
  19.                 menuTitle.add("Help");
  20.  
  21.  
  22.  
  23.                 //create the view with the list of menu title
  24.  
  25.                 ListView lView = new ListView(this);
  26.  
  27.                 ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(this,
  28.  
  29.                                 android.R.layout.simple_list_item_1, menuTitle);
  30.  
  31.                 lView.setAdapter(aAdapter);
  32.  
  33.  
  34.  
  35.                 LinearLayout l = new LinearLayout(this);
  36.  
  37.                 l.setOrientation(LinearLayout.VERTICAL);
  38.  
  39.                 l.addView(iView, 0, new LinearLayout.LayoutParams(
  40.  
  41.                                 LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  42.  
  43.                 l.addView(lView, 1, new LinearLayout.LayoutParams(
  44.  
  45.                                 LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  46.  
  47.  
  48.  
  49.                 //now add the image and list view to the parent
  50.  
  51.                 this.addContentView(l, new LinearLayout.LayoutParams(
  52.  
  53.                                 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


[/Edit]

Regards,
plusminus
Attachments
layout.png
layout.png (7.04 KiB) Viewed 2201 times
layout.png
layout.png (6.18 KiB) Viewed 2207 times
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby chouman » Fri Jan 04, 2008 6:37 pm

hey plusminus,

so what i did was i had a MenuView extends LinearLayout then i have the code in there. that's why i just did this.addView(listview) and then this.addView(imageview)

does that not work?
chouman
Developer
Developer
 
Posts: 27
Joined: Thu Jan 03, 2008 2:59 pm

Postby plusminus » Sat Jan 05, 2008 12:41 am

Hello chouman,

didn't have your whole class, so I put all into an onCreate()-method. I'm pretty convinced that both will work.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

how do i start subactivity with the menu created

Postby chouman » Wed Jan 16, 2008 12:52 am

I am not sure how to start on this.
What I am trying to do is when the user select something from the menu, a subactivity will start. When a user selected setup then it will open a subacitivity of setting up an alarm. Do i use a SetOnClickListener? If so how do I set it up?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.util.ArrayList;
  2.  
  3.  
  4.  
  5. import com.asurion.android.alarm.alarm;
  6.  
  7.  
  8.  
  9. import android.app.Activity;
  10.  
  11. import android.content.Intent;
  12.  
  13. import android.os.Bundle;
  14.  
  15. import android.view.View;
  16.  
  17. import android.widget.AdapterView;
  18.  
  19. import android.widget.ArrayAdapter;
  20.  
  21. import android.widget.ImageView;
  22.  
  23. import android.widget.LinearLayout;
  24.  
  25. import android.widget.ListView;
  26.  
  27. import android.widget.AdapterView.OnItemClickListener;
  28.  
  29. import android.widget.LinearLayout.LayoutParams;
  30.  
  31.  
  32.  
  33. public class MobileBackup extends Activity
  34.  
  35. {
  36.  
  37.     private LinearLayout lLayout;
  38.  
  39.     private ImageView iView;
  40.  
  41.     private ListView lView;
  42.  
  43.    
  44.  
  45.     private static final int ACTIVITY_SETUP = 0;
  46.  
  47.     private static final int ACTIVITY_DETAIL = 1;
  48.  
  49.        
  50.  
  51.         /** Called when the activity is first created. */
  52.  
  53.     @Override
  54.  
  55.     public void onCreate(Bundle icicle)
  56.  
  57.     {
  58.  
  59.         super.onCreate(icicle);
  60.  
  61.                
  62.  
  63.         //create a new image view to store the header
  64.  
  65.         iView = new ImageView(this);
  66.  
  67.         iView.setEnabled(false);
  68.  
  69.         //set the header to be the image view
  70.  
  71.         iView.setImageDrawable(getResources().getDrawable(R.drawable.header));
  72.  
  73.        
  74.  
  75.         //create an array list to store the menu
  76.  
  77.         ArrayList<String> menuTitle = new ArrayList<String> ();
  78.  
  79.         menuTitle.add("Backup");
  80.  
  81.         menuTitle.add("Setup");
  82.  
  83.         menuTitle.add("Help");
  84.  
  85.        
  86.  
  87.         //create the menu with the list view and set the adapter
  88.  
  89.         lView = new ListView(this);
  90.  
  91.         ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuTitle);
  92.  
  93.         lView.setAdapter(aAdapter);
  94.  
  95.        
  96.  
  97.         //Create a new LinearLayout for the main menu
  98.  
  99.         lLayout = new LinearLayout(this);
  100.  
  101.        
  102.  
  103.         //adding the image view and list view to the linear layout
  104.  
  105.         lLayout.setOrientation(LinearLayout.VERTICAL);
  106.  
  107.         lLayout.addView(iView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  108.  
  109.         lLayout.addView(lView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  110.  
  111.        
  112.  
  113.         //add the layout onto the activity
  114.  
  115.         this.addContentView(lLayout, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  116.  
  117.        
  118.  
  119.         //int selectedItem = lView.getSelectedItemIndex();
  120.  
  121.         lView.setOnItemClickListener(mItemClickListener);
  122.  
  123.        
  124.  
  125.         int selected = lView.getSelectedItemIndex();
  126.  
  127.        
  128.  
  129.         switch (selected)
  130.  
  131.         {
  132.  
  133.                 case 0:
  134.  
  135.                         break;
  136.  
  137.                 case 1:
  138.  
  139.                         Intent i = new Intent(this, alarm.class);
  140.  
  141.                         startSubActivity(i, ACTIVITY_SETUP);
  142.  
  143.                         break;
  144.  
  145.                 case 2:
  146.  
  147.                         break;
  148.  
  149.         }
  150.  
  151.     }
  152.  
  153.    
  154.  
  155.     private OnItemClickListener mItemClickListener = new OnItemClickListener()
  156.  
  157.     {
  158.  
  159.         public void onItemClick(AdapterView parent, View v, int position, long id)
  160.  
  161.         {
  162.  
  163.  
  164.  
  165.                
  166.  
  167.         }
  168.  
  169.     };
  170.  
  171.    
  172.  
  173.    
  174.  
  175. }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4


This is what I have. I am not sure how to setup the OnItemClickListener.
chouman
Developer
Developer
 
Posts: 27
Joined: Thu Jan 03, 2008 2:59 pm

continue

Postby chouman » Wed Jan 16, 2008 12:54 am

can you also explain a little how the onclicklistener work?
chouman
Developer
Developer
 
Posts: 27
Joined: Thu Jan 03, 2008 2:59 pm

Top

Postby plusminus » Wed Jan 16, 2008 1:02 am

Hello chouman,

the OnClickListener can be set to Views (like the Button) like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. myOkButton.setOnClickListener(new OnClickListener(){
  3.  
  4.         @Override
  5.  
  6.         public void onClick(View viewParam) {
  7.  
  8.                 // Do what needs to be done...
  9.  
  10.         }
  11.  
  12. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


You do the menu stuff like this (place it within your MainActivity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.  
  3.         public boolean onMenuItemSelected(int featureId, Item item) {
  4.  
  5.                 switch(item.getId()){
  6.  
  7.                         case 0:
  8.  
  9.                                 Intent i = new Intent(this, alarm.class);
  10.  
  11.                                 startSubActivity(i, ACTIVITY_SETUP);
  12.  
  13.                                 break;
  14.  
  15.                         case 1:
  16.  
  17.                                 // ... other menu-items here
  18.  
  19.                                 break;
  20.  
  21.                 }
  22.  
  23.                 return super.onMenuItemSelected(featureId, item);
  24.  
  25.         }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Hope this helped you :)

Sorry, no time for extensive explanation, its 1 am(!) now :(

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

problem with starting an subactivity

Postby chouman » Thu Jan 17, 2008 1:15 am

Hey Plusminus,

Thanks for your quick response. It was helpful but a little different than what i was looking for. Basically what i am doing is that I am having a main page with a list of different things (like a main menu) that the user can pick (almost like a list activity) and then the user can just click on it which then will invoke a subacitivty.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.Backup;
  2.  
  3.  
  4.  
  5. import java.util.ArrayList;
  6.  
  7.  
  8.  
  9. import com.google.android.alarm.alarm;
  10.  
  11.  
  12.  
  13. import android.app.Activity;
  14.  
  15. import android.content.Intent;
  16.  
  17. import android.os.Bundle;
  18.  
  19. import android.util.Log;
  20.  
  21. import android.view.View;
  22.  
  23. import android.widget.AdapterView;
  24.  
  25. import android.widget.ArrayAdapter;
  26.  
  27. import android.widget.ImageView;
  28.  
  29. import android.widget.LinearLayout;
  30.  
  31. import android.widget.ListView;
  32.  
  33. import android.widget.AdapterView.OnItemClickListener;
  34.  
  35. import android.widget.LinearLayout.LayoutParams;
  36.  
  37.  
  38.  
  39. public class MobileBackup extends Activity
  40.  
  41. {
  42.  
  43.     private LinearLayout lLayout;
  44.  
  45.     private ImageView iView;
  46.  
  47.     private ListView lView;
  48.  
  49.    
  50.  
  51.     private static final int ACTIVITY_SETUP = 0;
  52.  
  53.     private static final int ACTIVITY_DETAIL = 1;
  54.  
  55.    
  56.  
  57.     private static final String DEBUG = "DEBUG";
  58.  
  59.        
  60.  
  61.         /** Called when the activity is first created. */
  62.  
  63.     @Override
  64.  
  65.     public void onCreate(Bundle icicle)
  66.  
  67.     {
  68.  
  69.         super.onCreate(icicle);
  70.  
  71.                
  72.  
  73.         //create a new image view to store the header
  74.  
  75.         iView = new ImageView(this);
  76.  
  77.         iView.setEnabled(false);
  78.  
  79.         //set the header to be the image view
  80.  
  81.         iView.setImageDrawable(getResources().getDrawable(R.drawable.header));
  82.  
  83.        
  84.  
  85.         //create an array list to store the menu
  86.  
  87.         ArrayList<String> menuTitle = new ArrayList<String> ();
  88.  
  89.         menuTitle.add("Backup");
  90.  
  91.         menuTitle.add("Setup");
  92.  
  93.         menuTitle.add("Help");
  94.  
  95.        
  96.  
  97.         //create the menu with the list view and set the adapter
  98.  
  99.         lView = new ListView(this);
  100.  
  101.         ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuTitle);
  102.  
  103.         lView.setAdapter(aAdapter);
  104.  
  105.        
  106.  
  107.         //Create a new LinearLayout for the main menu
  108.  
  109.         lLayout = new LinearLayout(this);
  110.  
  111.        
  112.  
  113.         //adding the image view and list view to the linear layout
  114.  
  115.         lLayout.setOrientation(LinearLayout.VERTICAL);
  116.  
  117.         lLayout.addView(iView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  118.  
  119.         lLayout.addView(lView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  120.  
  121.        
  122.  
  123.         //add the layout onto the activity
  124.  
  125.         this.addContentView(lLayout, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  126.  
  127.        
  128.  
  129.         //int selectedItem = lView.getSelectedItemIndex();
  130.  
  131.         lView.setOnItemClickListener(mItemClickListener);
  132.  
  133.     }
  134.  
  135.    
  136.  
  137.     private OnItemClickListener mItemClickListener = new OnItemClickListener()
  138.  
  139.     {
  140.  
  141.         public void onItemClick(AdapterView parent, View v, int position, long id)
  142.  
  143.         {
  144.  
  145.                 Log.d(DEBUG, " the position is " + position);
  146.  
  147.                
  148.  
  149.                 switch (position)
  150.  
  151.                 {
  152.  
  153.                         case 0:
  154.  
  155.                                 break;
  156.  
  157.                         case 1:
  158.  
  159.                                 Intent i = new Intent(parent.getContext(), com.asurion.android.alarm.alarm.class);
  160.  
  161.                                 startSubActivity(i, ACTIVITY_SETUP);
  162.  
  163.                                 break;
  164.  
  165.                         case 2:
  166.  
  167.                                 break;
  168.  
  169.                 }
  170.  
  171.         }
  172.  
  173.     };
  174.  
  175.    
  176.  
  177.    
  178.  
  179. }
Parsed in 0.049 seconds, using GeSHi 1.0.8.4


So that's what I have and it seems to work ok. But i am having problem starting the alarm activity. The error i get is "An error has occurred in process com.google.Backup. Unable to find explicit activity class

I think the cause of this error maybe because i have the alarm.java in another package. so i have the Backup.java in com.google.android.Backup; and alarm.java in com.google.android.alarm;

How do I change the android manifest editor to make this work?
chouman
Developer
Developer
 
Posts: 27
Joined: Thu Jan 03, 2008 2:59 pm

Postby plusminus » Thu Jan 17, 2008 9:34 pm

Hello chouman,

I assume you are creating the Intent incorrectly:
You:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent i = new Intent(parent.getContext(), com.asurion.android.alarm.alarm.class);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

Working from Christmas-Calendar-Tutorial:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent i = new Intent(ChristmasCalendar.this, ImageDisplayer.class);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


So for you it would be sth like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent i = new Intent(MobileBackup.this, com.asurion.android.alarm.alarm.class);
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Is all your code within the same Project :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby chouman » Thu Jan 17, 2008 9:42 pm

hi plusminus,

i got it to work now. thanks for your help, but i am running into more issues haha.

i want to work with AlarmManager. I put AlarmManager within my alarm.java class and want to set it at a certain time to wake up my Backup.java. here is what i did:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private TimePicker.OnTimeSetListener mTimeSetListener = new TimePicker.OnTimeSetListener()
  2.  
  3.     {
  4.  
  5.  
  6.  
  7.         public void timeSet(TimePicker view, int hourOfDay, int minute)
  8.  
  9.             {
  10.  
  11.                 mHour = hourOfDay;
  12.  
  13.                 mMinute = minute;
  14.  
  15.                 Intent i = new Intent(alarm.this, MobileBackup.class);
  16.  
  17.                 updateDisplay();
  18.  
  19.                            
  20.  
  21.                 long alarmTime = Date.UTC(mYear, mMonth, mDay, mHour, mMinute, 0);
  22.  
  23.                            
  24.  
  25.                 Log.d(DEBUG, "The alarm time in long format is " + alarmTime);
  26.  
  27.                 try
  28.  
  29.                 {
  30.  
  31.                         AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
  32.  
  33.  
  34.  
  35.                         am.set(AlarmManager.RTC_WAKEUP, alarmTime, i);
  36.  
  37.                                
  38.  
  39.                 }
  40.  
  41.                 catch (Exception e)
  42.  
  43.                 {
  44.  
  45.                         Log.d(DEBUG, "There is something wrong with setting alarm");
  46.  
  47.                 }
  48.  
  49.             }
  50.  
  51.     };
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


then in the manifest i have:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <receiver class=".Backup" android:process="remote" >
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


i looked at the APIDemo AlarmController.java and also the tutorial on launch App on Emulator startup and the React on incoming SMS but still have no clue on how to set up the receiver, and how to work the AlarmManager
chouman
Developer
Developer
 
Posts: 27
Joined: Thu Jan 03, 2008 2:59 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests