Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 1st, 2012, 05:06 AM
Authorized User
 
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>
 
Old December 28th, 2013, 08:10 AM
Authorized User
 
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.





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.