Problem with onItemClick - android

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!

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.

ListView - setOnItemLongClickListener not getting called

I tried various solutions mentioned in SO but still my issue is not resolved. Mentioned the code below.
Example.java
package com.android.Example;
import java.util.Collections;
import java.util.List;
import com.android.Example.db.Tbl_example_Title_DAO;
import com.Example.database.handler.example_Titles;
import com.android.Example.adapter.ArrayListAdapter;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
public class Example extends Activity {
Tbl_example_Title_DAO tbl_example_title;
example_Titles example_Titles;
ListView lvlist;
List<String> exampleTitlesList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_singleitems_list);
lvlist = (ListView)findViewById(R.id.list);
tbl_example_title = new Tbl_example_Title_DAO(this);
lvlist.setClickable(true);
lvlist.setLongClickable(true);
lvlist.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String s=exampleTitlesList.get(arg2);
Log.w("HHHHHHHHHHHHHH", s);
return true;
}
});
showlist();
}
private void showlist() {
example_Titles = new example_Titles();
example_Titles.setexample("E");
//List<example_Titles> exampleTitlesList = tbl_example_title.getTbl_example_Detailss(example_Titles);
exampleTitlesList = tbl_example_title.getTbl_example_Details_str("E");
Collections.sort(exampleTitlesList);
ArrayListAdapter arrayAdapter = new ArrayListAdapter(Example.this, exampleTitlesList);
lvlist.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
listview_singleitems_list.xml file
<RelativeLayout 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"
>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/transparent"
android:dividerHeight="10dp"
android:drawSelectorOnTop="true"
android:footerDividersEnabled="false"
android:padding="10dp"
android:scrollbarStyle="outsideOverlay" />
</RelativeLayout>
Each row of the listview - amc.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/lvi"
android:descendantFocusability="blocksDescendants"
>
<TextView
android:id="#+id/tvCategory_listItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>
</LinearLayout>
Data is getting populated but not responding to any click events.
Kindly help me understand what i am missing in the code. Thanks
Update 1 - Tried adding the below method. But Did not work.
lvlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
Log.w("HHHHHHHHHHHHHH", item);
}
});
Update 2 - I Tried to use the below code and now the onclick works fine.So, the issue is with the way i have created the amc.xml file only. If anybody knows what is the issue, please let me know
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, categoryTitlesList);
lvlist.setAdapter(adapter);
adapter.notifyDataSetChanged();
instead of
ArrayListAdapter arrayAdapter = new ArrayListAdapter(ExpenseCategoryList.this, categoryTitlesList);
lvlist.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
get rid of these codes..
lvlist.setClickable(true);
lvlist.setLongClickable(true);
listview responds to item click and long click events.. so i could say it's a viewgroup..
but when you specify a listview to respond to click events like you've done you are telling the listview which is a view itself to respond to click events not its child..
and if you look at your code its
setOnItemLongClickListener or setOnItemClickListener respectively
not
setOnClickListener or setOnLongClickListener
so test it and let me know if it helps

Android ListView wont scroll

I have tried so many things to get my android ListView to scroll. However I have been unsuccessful in my attempt. Here is my xml code below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
android:listSelector="#color/theme_colour"
android:scrollbarStyle="outsideInset">
</ListView>
</RelativeLayout>
I have also added my java code below. I am not sure how to fix this problem and your help would be greatly appreciated.
package com.outdeh;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;
/**
* Created by horacedaley on 2/15/14.
*/
public class EventsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);
populateListView();
}
public void populateListView(){
ParseQueryAdapter<ParseObject> adapter =
new ParseQueryAdapter<ParseObject>(this, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery<ParseObject> create() {
// Here we can configure a ParseQuery to our heart's desire.
ParseQuery query = new ParseQuery("Events");
query.orderByAscending("eDate");
return query;
}
});
adapter.setTextKey("eTitle");
ListView listView = (ListView) findViewById(R.id.listview);
listView.setFastScrollEnabled(true);
listView.setAdapter(adapter);
}
}
Try changing android:layout_height="wrap_content"s to android:layout_height="match_parent". I guess it doesn't scroll because when you set the height to "wrap_content" the view will go beyond the visible area in activity, and since all items are considered displayed (in spite of not being in the visible area), there won't be scrolling available.

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{}

Android listView within tabwidget activity gives exception

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.

Categories

Resources