Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning Android 4 Application Development
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Android 4 Application Development
This is the forum to discuss the Wrox book Beginning Android 4 Application Development by Wei-Meng Lee; ISBN: 978-1-1181-9954-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Android 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 1st, 2012, 05:06 AM
Authorized User
Points: 65, Level: 1
Points: 65, Level: 1 Points: 65, Level: 1 Points: 65, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 15
Thanks: 2
Thanked 1 Time in 1 Post
Default Can't get BasicViews5 tutorial to work

Hi, I'm wondering if anyone else has the same issue in that I can't get the BasicViews5 tutorial to work which appears on pages 191 - 199.

I've written the code as it appears in the book and tried it and also downloaded the source code and both fail.

I have 2 problems
  • In the early part of building up the example I should be able to click on one or several items in the list and a tick box appear against the chosen item. It doesn't
  • Also the additions at the end of the tutorial causes the emulator to crash on start up and TBH I can't say that I've got to grips with LogCat yet so can't track down the error.

Here's my code:-
Code:
package net.example.basicviews5;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class BasicViews5 extends ListActivity {

	String[] presidents;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_basic_views5);

		ListView lstView = getListView();

		// lstView.setChoiceMode(ListView.CHOICE_MODE_NONE);
		// lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		lstView.setTextFilterEnabled(true);

		presidents = getResources().getStringArray(R.array.presidents_array);

		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_checked, presidents));
	}

	public void onListItemClick(ListView parent, View v, int position, long id) {
		Toast.makeText(this, "You have selected " + presidents[position],
				Toast.LENGTH_SHORT).show();
	}

	public void onClick(View view) {
		ListView lstView = getListView();

		String itemsSelected = "Selected items: \n";
		for (int i = 0; i < lstView.getCount(); i++) {
			if (lstView.isItemChecked(i)) {
				itemsSelected += lstView.getItemAtPosition(i) + "\n";
			}
		}
		Toast.makeText(this, itemsSelected, Toast.LENGTH_LONG).show();
	}

}
And the layout file...
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Show selected items"
        android:onClick="onClick" />

</LinearLayout>
Reply With Quote
  #2 (permalink)  
Old December 28th, 2013, 08:10 AM
Authorized User
Points: 65, Level: 1
Points: 65, Level: 1 Points: 65, Level: 1 Points: 65, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 15
Thanks: 2
Thanked 1 Time in 1 Post
Default Resolved

OK, after some time off I've started going through all the examples in the book again. Don't know what I was doing last time around but notice that my problem lies in the layout file in that I'd failed to include the <ListView> in the layout so,
a) The Java code is unchanged
b) The layout file should look like this (note match_parent and fill_parent in width settings are interchangeable) :
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Show selected items" />

    <ListView
        android:id="@+id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>
And my problem is now RESOLVED!
Thanks for your interest.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SMS API does not work online but work fine offline. Tonik Beginning PHP 1 January 4th, 2013 08:30 AM
Tutorial surendran ASP.NET 2.0 Basics 0 May 24th, 2006 12:15 PM
Tutorial sithius Access 1 March 13th, 2006 03:44 PM
tutorial ajaikumar_2 BOOK: Beginning Visual C++ 6 1 January 22nd, 2005 07:32 AM



All times are GMT -4. The time now is 02:13 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.