Android listView within tabwidget activity gives exception - android

Using Google's "Hello TabWidget" tutorial found here I was able to build a tabWidget layout, but I'm having a seriously difficult time trying to stick a listview within one of the tabs.
I've also read through a couple threads here about this issue, but they all pertain to assigning the ListView to the specific id "list", which I have done, but I still get a null pointer exception.
I have spent wayyyyyyy to much time on this elementary objective, so I've asked God for help, but I think he's busy right now. Anyone on a lunch break?
the activity in which the ListView is called
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class SettingsActivity extends Activity {
private ListView lv;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
String[] settingsList = getResources().getStringArray(R.array.settingsStringArray);
lv = (ListView) findViewById(R.id.list);
lv.setAdapter(new ArrayAdapter<String>(this, R.id.TextView01, settingsList));
lv.setOnItemClickListener
(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),Toast.LENGTH_SHORT).show();
}
});
}
}
xml file from res/layout called within setContentView(R.layout.settings)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/list">
</ListView>
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
Thanks, everyone.

Your TextView01 needs to go into a separate XML file, say textview01. (The name needs to be lowercase, although the only error message you'll see about this will be in the console.) You would then access it as R.layout.textview01.

Related

onListItemClick shows as "never used"

Below is the code for my first attempt at setting up a list fragment that clicks through to an activity (just as a test).
I've followed the Android docs example at: List Fragment Example
However, onListItemClick shows as "never used". When I run the app my list shows up, but when I click on the items in it I just get the following returned:
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
The XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrationuser.piclo.ListViewFragment"
tools:showIn="#layout/activity_list_view"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp"
>
<FrameLayout
android:id="#+id/list_container_id"
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" >
</ListView>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
And here is the list fragment:
package com.example.administrationuser.piclo;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
public class ListViewFragment extends ListFragment {
public ListViewFragment() {}
String[] myStringArray = new String[]{"aa","bbb","ccccc","dddddd"};
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Populate list with our static array of titles.
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_activated_1, myStringArray));
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
void showDetails(int index) {
int mCurCheckPosition = index;
Intent intent = new Intent();
intent.setClass(getActivity(), MessageDetails.class);
intent.putExtra("Inty", mCurCheckPosition);
startActivity(intent);
startActivity(intent);
}
}
I have solved the problem.
It is trivial and would seem obvious to any seasoned Android developer, but here it is for the beginners out there who may fall into this trap....
...do not name your parent activity 'ListView'... it confuses the OnListItemClick override cause it doesn't know which class of ListView it should be overriding... the actual one or the one that you may have unfortunately created through the worst possible choice of name for your parent activity.

Displaying list in Android fragment

I got a fragment that should only display a list but every time I try to display the list the previous activity is displayed in the background.
So my question is, how do I display the list on a empty page? What can I put instead of getActivity() in the ArrayAdapter.
My XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/fundraiser"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
And my code looks like this
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class FundraiserFragment extends ListFragment{
String[] values = new String[] { "Item 1", "Item 2", "Item 3"};
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getActivity(),android.R.layout.simple_list_item_1,values);
setListAdapter(adapter);
}
Don't forget that layouts are transparent by default, unless you assign them a background color.
Assign a background color to the LinearLayout of your fragment in the XML file and the parent activity should disappear.
android:background="#android:color/white"

How to get multiple selected items from a ListActivity in Android

I am basically trying to get which items have been selected within a multiple choice enabled ListActivity. I am sure that there are many beginning Android programmers that would love this to be clarified. This app runs fine until it gets to the FOR loop and tries to assign the values of the multi-choice items to ArrayList 'items'. I'm simply trying to use Toast to see if the multi-choice items are being assigned to the Arraylist 'items'. The app crashes when the button is clicked. testing is done in android 2.2 emulator
Here is my Main.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Selected Items" />
<ListView android:id="#android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:choiceMode="multipleChoice"
android:textFilterEnabled="true" android:fastScrollEnabled="true" >
</ListView>
</LinearLayout>
and here is the code
package com.tests.TestCode;
import android.app.ListActivity;
import android.os.Bundle;
import java.util.ArrayList;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] list = {"wrenches","hammers","drills","screwdrivers","saws","chisels","fasteners"};
// Initializing An ArrayAdapter Object for the ListActivity
final ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_multiple_choice, list);
setListAdapter(adapter);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ListView thelistview = (ListView) findViewById(android.R.id.list);
ArrayList<String> items = null;
SparseBooleanArray checkedItems = thelistview.getCheckedItemPositions();
if (checkedItems != null) {
for (int i=0; i<checkedItems.size(); i++) {
if (checkedItems.valueAt(i)) {
String item = adapter.getItem(checkedItems.keyAt(i)).toString();
items.add(item);
}
}
Toast.makeText(getBaseContext(), items.toString(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "checkedItems == null", Toast.LENGTH_LONG).show();
}
}
});
}
}
Much obliged to anyone that can provide a good answer as to the best way to go about accomplishing this ListActivity scenario, or anyone that can explain why the app crashes at the point of assignment to the ArrayList. Thanks!
Your items list is null and you try to use it before initializing it.
You need to use items = new ArrayList<String>(); somewhere before trying to use it, or you will keep getting NullPointerExceptions.

Android OnItemClickListener not working

I'm using android 2.3.3. I set up a layout like,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/mainList" android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
And I'm manipulating it with
package org.dewsworld.ui;
import org.dewsworld.core.NBConfig;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class NewsBotActivity extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter( new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
NBConfig.topics));
ListView listView = (ListView) findViewById(R.id.mainList) ;
listView.setOnItemClickListener( new OnItemClickListener() {
});
}
}
Using eclise IDE, when I setthe OnItemClickListener it gives me the error
The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (new
OnItemClickListener(){})
I can't fix this. [I've added an image with the error]
It seems you have imported the wrong OnItemClickListener, try this one instead, and remove import of android.view.View.OnClickListener
import android.widget.AdapterView.OnItemClickListener;
how about filling in the body of your new object by defining the onItemClick() function:
public void onItemClick(AdapterView parent, View v, int position, long id)
{
// Display a messagebox.
Toast.makeText(this,"Your Listener Works!",Toast.LENGTH_SHORT).show();
}
try using ctrl+shift+o in eclipse to organize all your imports automatically...
Just implement OnItemClickListener to your class.
Like This:
public class ClassName extends Activity implements OnItemClickListener{}

Problem with onItemClick

This might be really long and I'm sorry for the poor soul who reads through it all.
Goal: I want to create a Pokemon Pokedex application (Yeah I know very childish). I thought this would be great for a first application to build because there is tons of data to deal with. For those not familiar with Pokemon it's basically creatures in a list from #001 - #649. I want to display all the creatures in a list and have the user click on that creature. Once the creature is clicked on it will display statistics about it on a different screen.
Ok back to programming...
package com.pokemon.pokdex;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
public class PokedexActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
R.array.pokemon_titles, R.layout.list_item));
final String[] links = getResources().getStringArray(R.array.pokemon_stats);
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String content = links[position];
}
});
}
}
I'm getting errors on getListView().setOnItemClickListener(new OnItemClickListener() { where is says OnItemClickListener() and View view,. it says The type new AdapterView.OnItemClickListener(){} must implement the inherited abstract method AdapterView.OnItemClickListener.onItemClick(AdapterView<?>, View, int, long) for OnItemClickListener() and View cannot be resolved to a type for View view,
package com.pokemon.pokdex;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
public class PokedexViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pokemon_stats);
};
}
On this file I'm getting an error on setContentView(R.layout.pokemon_stats); Here is the error pokemon_stats cannot be resolved or is not a field
My string.xml file looks like
(this is where the pokemon will be set as well as the stats. I did the first pokemon below.)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Pokemon Pokedex</string>
<string name="app_name">Pokedex</string>
<string-array name="pokemon_titles">
<item>#001 - Bulbasaur</item>
</string-array>
<string-array name="pokemon_stats">
<item>Grass Type</item>
</string-array>
</resources>
My main.xml looks like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
My list_item.xml looks like
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="6dp" />
Any help is appreciated and thank you if you made it this far :)
EDIT: Updated and added the exact errors I'm getting.
For your error setContentView(R.layout.pokemon_stats); pokemon_stats is not a layout. main.xml is your layout. pokemon_stats is a string array, that's your problem. There is no layout called pokemon_stats.xml, I think you want R.layout.main.
Also, why do you have a semi-colon after } here?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pokemon_stats);
};
Edit: At the top of your other error, the one with view, you forgot to import android.view.View; add this. You can keep your original code.
import android.view.View;
That should be it, really.
Try now
package com.pokemon.pokdex;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
public class PokedexActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
R.array.pokemon_titles, R.layout.list_item));
});
}
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final String[] links = getResources().getStringArray(R.array.pokemon_stats);
String content = links[position];
}
}
You don't have any ID on the textView you are using to hold the information, where is this information going to be displayed?
Also, for your resources, your data should really be held in a single array with CSV or something, it will be much simpler than holding different information in different arrays. Parse the data in with tags or by array order, and bind them to your view that way.
Your main.xml is going to be what is displayed here, as well. If you don't have anything defined for main.xml, the program is going to error out. setContentView() sets the content view for the ENTIRE screen. If you want to add an item to a view, use View.addItem(Layout).
Hope this helps. It's the best I can do without any error messages!

Categories

Resources