Android OnItemClickListener not working - android

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

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.

how to move from one main listview fragment to other different listview fragments by clicking main listview's list items

I am trying to make call from my main listview Fragment to another differnt listview fragments by clickig different items on main list view.
For that i have written following code:
My main listview fragment ShopFragment.java:
(from this list items i want to call another fragments by clicking on different items)
package fragments.h.safmical;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import safmical.h.R;
/**
* Created by hadvani on 4/13/2017.
*/
public class ShopFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View rootview= inflater.inflate(R.layout.fragment_shop,container,false);
String[] list1={"x","y","z"};
ListView listView= (ListView) rootview.findViewById(R.id.mainlist);
ArrayAdapter<String> listviewadapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list1);
listView.setAdapter(listviewadapter);
FragmentManager fm =getFragmentManager();
listView.setOnClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
fm.beginTransaction().replace(R.id.content_frame,new ListOneFragment()).commit();
}
if(position==1){
fm.beginTransaction().replace(R.id.content_frame,new ListTwoFragment()).commit();
}
if(position==2){fm.beginTransaction().replace(R.id.content_frame,new ListThreeFragment()).commit();}
}
});
return rootview;
}
}
xml file of main listview fragment fragment_shop.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#e6e6fa">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainlist"/>
</FrameLayout>
ListOneFragment.java:
package fragments.h.safmical;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import safmical.h.R;
/**
* Created by hadvani on 4/14/2017.
*/
public class ListOneFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_listone, container, false);
String[] list1 = {"a", "b", "c"};
ListView listView = (ListView) rootview.findViewById(R.id.list1);
ArrayAdapter<String> listviewadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list1);
listView.setAdapter(listviewadapter);
return rootview;
}
}
fragment_listone.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list1">
</ListView>
</FrameLayout>
Same way i have implemented ListTwoFragment.java , ListThreeFragment.java , fragment_listtwo.xml and fragment_listthree.xml.
but its not working. Gradle build shows following error:
Error:(30, 17) error: no suitable method found for setOnClickListener(<anonymous OnItemClickListener>)
method View.setOnClickListener(OnClickListener) is not applicable
(argument mismatch; <anonymous OnItemClickListener> cannot be converted to OnClickListener)
method AdapterView.setOnClickListener(OnClickListener) is not applicable
(argument mismatch; <anonymous OnItemClickListener> cannot be converted to OnClickListener)
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Should i make any correction or is there any differnet method to do this?
Please help me to solve this.
You need to change
listView.setOnClickListener(new AdapterView.OnItemClickListener() {
to
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
What's happening is that it's trying OnClickListener instead of OnItemClickListener
You can't use OnClickListener for ListView. Use OnItemClickListener instead, documented here:
https://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)

onListItemClick not being invoked

I am following a online course and I can across this exercise that for some reason is not working. The onListItemClick() is not being invoked. I have tried troubleshooting this but I couldn't get rid of the issue
Please help me solve this issue.
My code for MainActivity.java is
package hk.ust.cse.comp107x.greetfriend;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity{
String names[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
names=getResources().getStringArray(R.array.friends);
//setListAdapter((ListAdapter)new ArrayAdapter<String>(this,R.layout.friend_item,names));
setListAdapter( new ArrayAdapter<String>(this, R.layout.friend_item, names));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent in=new Intent(this,ShowMessage.class);
in.putExtra("message","Good Day "+names[(int) id]+"!!");
startActivity(in);
}
}
Code for ShowMessage.java is
package hk.ust.cse.comp107x.greetfriend;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ShowMessage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_message);
Intent in=getIntent();
String message=in.getStringExtra("message");
TextView textMessage=(TextView)findViewById(R.id.textMessage);
textMessage.setText(message);
}
}
My code for friend.xml is
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:text="Friend Name"
android:gravity="center_vertical|center_horizontal"
android:autoText="true"
android:textSize="24sp"
android:padding="20dp"
android:id="#+id/textview">
</TextView>
And finally my code for strings.xml is
<resources>
<string name="app_name">GreetFriend</string>
<string-array name="friends">
<item>John</item>
<item>Paul</item>
<item>George</item>
<item>Ringo</item>
</string-array>
</resources>
Use the following code for your Friend.xml and run the code...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:text="Friend Name"
android:gravity="center_vertical|center_horizontal"
android:textSize="24sp"
android:padding="20dp"
android:id="#+id/textviewfriend">
</TextView>
</LinearLayout>

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!

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