Trouble with ListView intent

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

Trouble with ListView intent

Postby malianx » Sun Jan 17, 2010 8:01 am

I can't seem to get this right, I'm trying to make a set of Tabs, one of which displays a listview. I tried working with examples I found searching around here, and the developer page. Here's what I have:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.malwiz.ansible;
  3.  
  4.  
  5.  
  6. import android.app.ListActivity;
  7.  
  8. import android.app.TabActivity;
  9.  
  10. import android.content.Intent;
  11.  
  12. import android.os.Bundle;
  13.  
  14. import android.widget.ArrayAdapter;
  15.  
  16. import android.widget.ListAdapter;
  17.  
  18. import android.widget.ListView;
  19.  
  20. import android.widget.TabHost;
  21.  
  22. import android.widget.TextView;
  23.  
  24.  
  25.  
  26. public class Ansible extends TabActivity {
  27.  
  28.     /** Called when the activity is first created. */
  29.  
  30.     @Override
  31.  
  32.     public void onCreate(Bundle savedInstanceState) {
  33.  
  34.         super.onCreate(savedInstanceState);
  35.  
  36.         setContentView(R.layout.main);
  37.  
  38.        
  39.  
  40.         // local data
  41.  
  42.        
  43.  
  44.        
  45.  
  46.         // ui elements
  47.  
  48.         TabHost uTabHost =      getTabHost();
  49.  
  50.         uTabHost.addTab(uTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(new Intent(this, conversationList.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
  51.  
  52.         uTabHost.addTab(uTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.tvTest2));
  53.  
  54.        
  55.  
  56.         uTabHost.setCurrentTab(0);
  57.  
  58.        
  59.  
  60.         /*
  61.  
  62.         TextView tvTest1 = (TextView)findViewById(R.id.tvTest1);
  63.  
  64.         tvTest1.setText("conversationList");
  65.  
  66.         */
  67.  
  68.        
  69.  
  70.         TextView tvTest2 = (TextView)findViewById(R.id.tvTest2);
  71.  
  72.         tvTest2.setText("contactsList");
  73.  
  74.        
  75.  
  76.         // listViewWrapper listView = new listViewWrapper();
  77.  
  78.        
  79.  
  80.         // listViewWrapper conversationList = new listViewWrapper();
  81.  
  82.         // tvTest2.setText( conversationList.toString() );
  83.  
  84.        
  85.  
  86.     }
  87.  
  88.    
  89.  
  90.     public class conversationList extends ListActivity {
  91.  
  92.        
  93.  
  94.         @Override
  95.  
  96.         protected void onCreate(Bundle savedInstanceState) {
  97.  
  98.                 super.onCreate(savedInstanceState);
  99.  
  100.                 setContentView(R.layout.main);
  101.  
  102.                
  103.  
  104.                 /*
  105.  
  106.                 setListAdapter(new ArrayAdapter<String>
  107.  
  108.             (this, android.R.layout.simple_list_item_1, COUNTRIES));
  109.  
  110.                 */
  111.  
  112.                
  113.  
  114.                 ListView conversationList = (ListView)findViewById(R.id.conversationList);
  115.  
  116.                 setListAdapter(new ArrayAdapter<String>
  117.  
  118.             (this, android.R.layout.simple_list_item_1, COUNTRIES));
  119.  
  120.                
  121.  
  122.                 conversationList.setAdapter(getListAdapter());
  123.  
  124.                
  125.  
  126.                
  127.  
  128.         }
  129.  
  130.                
  131.  
  132.     }
  133.  
  134.    
  135.  
  136.     static final String[] COUNTRIES = new String[] {
  137.  
  138.         "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
  139.  
  140.         "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
  141.  
  142.         "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
  143.  
  144.         "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
  145.  
  146.         "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
  147.  
  148.         "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
  149.  
  150.         "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
  151.  
  152.         "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
  153.  
  154.         "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
  155.  
  156.         "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
  157.  
  158.         "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
  159.  
  160.         "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
  161.  
  162.         "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
  163.  
  164.         "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",
  165.  
  166.         "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia",
  167.  
  168.         "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",
  169.  
  170.         "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
  171.  
  172.         "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",
  173.  
  174.         "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica",
  175.  
  176.         "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
  177.  
  178.         "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
  179.  
  180.         "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
  181.  
  182.         "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
  183.  
  184.         "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
  185.  
  186.         "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand",
  187.  
  188.         "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas",
  189.  
  190.         "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru",
  191.  
  192.         "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
  193.  
  194.         "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena",
  195.  
  196.         "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon",
  197.  
  198.         "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal",
  199.  
  200.         "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
  201.  
  202.         "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea",
  203.  
  204.         "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden",
  205.  
  206.         "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas",
  207.  
  208.         "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
  209.  
  210.         "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
  211.  
  212.         "Ukraine", "United Arab Emirates", "United Kingdom",
  213.  
  214.         "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan",
  215.  
  216.         "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara",
  217.  
  218.         "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
  219.  
  220.       };
  221.  
  222.    
  223.  
  224.     // end ansible class
  225.  
  226. }
  227.  
  228.  
Parsed in 0.069 seconds, using GeSHi 1.0.8.4


and here's the main.xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:id="@android:id/tabhost"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent">
  11.  
  12.  
  13.  
  14.     <LinearLayout
  15.  
  16.        android:orientation="vertical"
  17.  
  18.        android:layout_width="fill_parent"
  19.  
  20.        android:layout_height="fill_parent">
  21.  
  22.         <TabWidget
  23.  
  24.            android:id="@android:id/tabs"
  25.  
  26.            android:layout_width="fill_parent"
  27.  
  28.            android:layout_height="wrap_content" />
  29.  
  30.                 <FrameLayout android:id="@android:id/tabcontent"
  31.  
  32.                         android:layout_width="fill_parent"
  33.  
  34.                         android:layout_height="fill_parent">
  35.  
  36.                         <ListView android:id="@+id/conversationList"
  37.  
  38.                                 android:layout_width="fill_parent"
  39.  
  40.                                 android:layout_height="wrap_content">
  41.  
  42.                                 </ListView>
  43.  
  44.                                 <TextView android:id="@+id/tvTest2"
  45.  
  46.                                 android:layout_width="fill_parent"
  47.  
  48.                                 android:layout_height="wrap_content">
  49.  
  50.                         </TextView>
  51.  
  52.                 </FrameLayout>         
  53.  
  54.     </LinearLayout>
  55.  
  56.  
  57.  
  58. </TabHost>
  59.  
  60.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


any help untangling this would be appreciated.
malianx
Freshman
Freshman
 
Posts: 4
Joined: Sat Jan 16, 2010 8:41 pm

Top

Postby jwei512 » Mon Jan 18, 2010 10:56 am

Hmm... not sure what your problem is exactly... but here's just some suggestions to try based off of what i've done in the past

don't set the id of your ListView to android:id="@+id/conversationList", I've always seen it done as:

android:id="@android:id/android:list"

Then, because your conversationList already extends ListActivity, I don't think you need to actually retrieve the list view and set it like that, so basically just do

setContentView(R.layout.main)
setListAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, COUNTRIES));

And I think that should be enough...

Not sure try that for now.
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby malianx » Mon Jan 18, 2010 12:11 pm

*EDIT*

I seperated conversationList to another file, added that to the manifest and filled it out like so:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4. public void onCreate(Bundle savedInstanceState) {
  5.  
  6.   super.onCreate(savedInstanceState);
  7.  
  8.  
  9.  
  10.   setListAdapter(new ArrayAdapter<String>(this,
  11.  
  12.           android.R.layout.simple_list_item_1, COUNTRIES));
  13.  
  14.   getListView().setTextFilterEnabled(true);
  15.  
  16. }
  17.  
  18.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


-exactly how hello views has it-

That made it work, some of my tangled mess of code had been giving me a null pointer.

Thanks again for the assistance.
malianx
Freshman
Freshman
 
Posts: 4
Joined: Sat Jan 16, 2010 8:41 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests