Question

Still trying to get my head around this, lost of posts pointing to android:descendantFocusability="blocksDescendants" as a culprit, but I still cant seem to get onclick events for my ListView.

I should point out that when I tap the list, the background does go blue as if the tap is registered, just nothing happens in the code..

from my main.class:

    package com.whatsonwhere.app;

import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
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 java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;

import static com.whatsonwhere.app.R.layout.results;


/** Called when the activity is first created. */

public class Home extends ListActivity {

    @Override
    //what happens when the app is launched this is the main proc...
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] pages = getResources().getStringArray(R.array.pages_array);

        setListAdapter(new ArrayAdapter<String>(this, results, pages));

        ListView lv = getListView();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

         @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("Click", "Click");
                Intent myIntent = null;
                Toast.makeText(getApplicationContext(), "You Pushed a button.",
                        Toast.LENGTH_SHORT).show();
                if(((TextView) view).getText().equals("About")){
                    myIntent = new Intent(view.getContext(), About.class);
                }

                if(((TextView) view).getText().equals("Find events near me")){
                    myIntent = new Intent(view.getContext(), Results.class);
                }

                if(((TextView) view).getText().equals("Somewhere Else")){
                    myIntent = new Intent(view.getContext(), Search.class);
                }

                //if(((TextView) view).getText().equals("Switch to button mode")){
                //myIntent = new Intent(view.getContext(), ButtonPage.class);
                //}

                startActivity(myIntent);
            }
        });




        setContentView(R.layout.activity_home);
        //ok try and find the users location...
        //get Your Current Location
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        MyCurrentLoctionListener locationListener = new MyCurrentLoctionListener();
        //try and get GPS (Fine) Position
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

        //also get location from network provider - speeds location aquisition up in case user is inside..
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        //now we have the location wait for the update before it is displayed in onLocationChanged
        TextView textview=(TextView)findViewById(R.id.textView2);
        textview.setText("Searching...");

    }
        public class MyCurrentLoctionListener extends Context implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            location.getLatitude();
            location.getLongitude();

            //String myLocation = "Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude();

            //make a log to see the results
            // Log.e("MY CURRENT LOCATION", myLocation);

            //maybe this is a good place to set the location string..
            //TextView textview = (TextView) findViewById(R.id.textView2);
            //textview.setText(myLocation);
            List<Address> addresses = null;
            try{
                Geocoder geocoder;
                geocoder = new Geocoder(this, Locale.getDefault());
                addresses=(geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1));

            }catch(IOException e){
                e.printStackTrace();
            }

            String address = addresses.get(0).getAddressLine(0);
            String city = addresses.get(0).getAddressLine(1);
            String country = addresses.get(0).getAddressLine(2);

//set the string to our new location

            TextView tv = (TextView) findViewById(R.id.textView2);
            tv.setText(address+"\n"+city+"\n"+country);


        }


            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }


            @Override
            public AssetManager getAssets() {
                return null;
            }

            @Override
            public Resources getResources() {
                return null;
            }

            @Override
            public PackageManager getPackageManager() {
                return null;
            }

            @Override
            public ContentResolver getContentResolver() {
                return null;
            }

            @Override
            public Looper getMainLooper() {
                return null;
            }

            @Override
            public Context getApplicationContext() {
                return null;
            }

            @Override
            public void setTheme(int i) {

            }

            @Override
            public Resources.Theme getTheme() {
                return null;
            }

            @Override
            public ClassLoader getClassLoader() {
                return null;
            }

            @Override
            public String getPackageName() {
                return null;
            }

            @Override
            public ApplicationInfo getApplicationInfo() {
                return null;
            }

            @Override
            public String getPackageResourcePath() {
                return null;
            }

            @Override
            public String getPackageCodePath() {
                return null;
            }

            @Override
            public SharedPreferences getSharedPreferences(String s, int i) {
                return null;
            }

            @Override
            public FileInputStream openFileInput(String s) throws FileNotFoundException {
                return null;
            }

            @Override
            public FileOutputStream openFileOutput(String s, int i) throws FileNotFoundException {
                return null;
            }

            @Override
            public boolean deleteFile(String s) {
                return false;
            }

            @Override
            public File getFileStreamPath(String s) {
                return null;
            }

            @Override
            public File getFilesDir() {
                return null;
            }

            @Override
            public File getExternalFilesDir(String s) {
                return null;
            }

            @Override
            public File[] getExternalFilesDirs(String s) {
                return new File[0];
            }

            @Override
            public File getObbDir() {
                return null;
            }

            @Override
            public File[] getObbDirs() {
                return new File[0];
            }

            @Override
            public File getCacheDir() {
                return null;
            }

            @Override
            public File getExternalCacheDir() {
                return null;
            }

            @Override
            public File[] getExternalCacheDirs() {
                return new File[0];
            }

            @Override
            public String[] fileList() {
                return new String[0];
            }

            @Override
            public File getDir(String s, int i) {
                return null;
            }

            @Override
            public SQLiteDatabase openOrCreateDatabase(String s, int i, SQLiteDatabase.CursorFactory cursorFactory) {
                return null;
            }

            @Override
            public SQLiteDatabase openOrCreateDatabase(String s, int i, SQLiteDatabase.CursorFactory cursorFactory, DatabaseErrorHandler databaseErrorHandler) {
                return null;
            }

            @Override
            public boolean deleteDatabase(String s) {
                return false;
            }

            @Override
            public File getDatabasePath(String s) {
                return null;
            }

            @Override
            public String[] databaseList() {
                return new String[0];
            }

            @Override
            public Drawable getWallpaper() {
                return null;
            }

            @Override
            public Drawable peekWallpaper() {
                return null;
            }

            @Override
            public int getWallpaperDesiredMinimumWidth() {
                return 0;
            }

            @Override
            public int getWallpaperDesiredMinimumHeight() {
                return 0;
            }

            @Override
            public void setWallpaper(Bitmap bitmap) throws IOException {

            }

            @Override
            public void setWallpaper(InputStream inputStream) throws IOException {

            }

            @Override
            public void clearWallpaper() throws IOException {

            }

            @Override
            public void startActivity(Intent intent) {

            }

            @Override
            public void startActivity(Intent intent, Bundle bundle) {

            }

            @Override
            public void startActivities(Intent[] intents) {

            }

            @Override
            public void startActivities(Intent[] intents, Bundle bundle) {

            }

            @Override
            public void startIntentSender(IntentSender intentSender, Intent intent, int i, int i2, int i3) throws IntentSender.SendIntentException {

            }

            @Override
            public void startIntentSender(IntentSender intentSender, Intent intent, int i, int i2, int i3, Bundle bundle) throws IntentSender.SendIntentException {

            }

            @Override
            public void sendBroadcast(Intent intent) {

            }

            @Override
            public void sendBroadcast(Intent intent, String s) {

            }

            @Override
            public void sendOrderedBroadcast(Intent intent, String s) {

            }

            @Override
            public void sendOrderedBroadcast(Intent intent, String s, BroadcastReceiver broadcastReceiver, Handler handler, int i, String s2, Bundle bundle) {

            }

            @Override
            public void sendBroadcastAsUser(Intent intent, UserHandle userHandle) {

            }

            @Override
            public void sendBroadcastAsUser(Intent intent, UserHandle userHandle, String s) {

            }

            @Override
            public void sendOrderedBroadcastAsUser(Intent intent, UserHandle userHandle, String s, BroadcastReceiver broadcastReceiver, Handler handler, int i, String s2, Bundle bundle) {

            }

            @Override
            public void sendStickyBroadcast(Intent intent) {

            }

            @Override
            public void sendStickyOrderedBroadcast(Intent intent, BroadcastReceiver broadcastReceiver, Handler handler, int i, String s, Bundle bundle) {

            }

            @Override
            public void removeStickyBroadcast(Intent intent) {

            }

            @Override
            public void sendStickyBroadcastAsUser(Intent intent, UserHandle userHandle) {

            }

            @Override
            public void sendStickyOrderedBroadcastAsUser(Intent intent, UserHandle userHandle, BroadcastReceiver broadcastReceiver, Handler handler, int i, String s, Bundle bundle) {

            }

            @Override
            public void removeStickyBroadcastAsUser(Intent intent, UserHandle userHandle) {

            }

            @Override
            public Intent registerReceiver(BroadcastReceiver broadcastReceiver, IntentFilter intentFilter) {
                return null;
            }

            @Override
            public Intent registerReceiver(BroadcastReceiver broadcastReceiver, IntentFilter intentFilter, String s, Handler handler) {
                return null;
            }

            @Override
            public void unregisterReceiver(BroadcastReceiver broadcastReceiver) {

            }

            @Override
            public ComponentName startService(Intent intent) {
                return null;
            }

            @Override
            public boolean stopService(Intent intent) {
                return false;
            }

            @Override
            public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
                return false;
            }

            @Override
            public void unbindService(ServiceConnection serviceConnection) {

            }

            @Override
            public boolean startInstrumentation(ComponentName componentName, String s, Bundle bundle) {
                return false;
            }

            @Override
            public Object getSystemService(String s) {
                return null;
            }

            @Override
            public int checkPermission(String s, int i, int i2) {
                return 0;
            }

            @Override
            public int checkCallingPermission(String s) {
                return 0;
            }

            @Override
            public int checkCallingOrSelfPermission(String s) {
                return 0;
            }

            @Override
            public void enforcePermission(String s, int i, int i2, String s2) {

            }

            @Override
            public void enforceCallingPermission(String s, String s2) {

            }

            @Override
            public void enforceCallingOrSelfPermission(String s, String s2) {

            }

            @Override
            public void grantUriPermission(String s, Uri uri, int i) {

            }

            @Override
            public void revokeUriPermission(Uri uri, int i) {

            }

            @Override
            public int checkUriPermission(Uri uri, int i, int i2, int i3) {
                return 0;
            }

            @Override
            public int checkCallingUriPermission(Uri uri, int i) {
                return 0;
            }

            @Override
            public int checkCallingOrSelfUriPermission(Uri uri, int i) {
                return 0;
            }

            @Override
            public int checkUriPermission(Uri uri, String s, String s2, int i, int i2, int i3) {
                return 0;
            }

            @Override
            public void enforceUriPermission(Uri uri, int i, int i2, int i3, String s) {

            }

            @Override
            public void enforceCallingUriPermission(Uri uri, int i, String s) {

            }

            @Override
            public void enforceCallingOrSelfUriPermission(Uri uri, int i, String s) {

            }

            @Override
            public void enforceUriPermission(Uri uri, String s, String s2, int i, int i2, int i3, String s3) {

            }

            @Override
            public Context createPackageContext(String s, int i) throws PackageManager.NameNotFoundException {
                return null;
            }

            @Override
            public Context createConfigurationContext(Configuration configuration) {
                return null;
            }

            @Override
            public Context createDisplayContext(Display display) {
                return null;
            }


        }
//The next two are menu options...
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        Toast.makeText(getApplicationContext(), "Not Implemented Yet.",
                Toast.LENGTH_SHORT).show();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

Custem results.xml

    <?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="fill_parent"
        android:padding="10dp"
        android:textSize="18sp" >
    </TextView>

and my main activity

   <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#ffffff">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"

        android:src="@drawable/logo" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/l_location"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"

        android:layout_toRightOf="@+id/imageView"
        android:layout_alignEnd="@+id/textView2"
        android:singleLine="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
        android:text="@string/l_mylocation"
        android:id="@+id/textView2"

        android:layout_below="@+id/textView"
        android:layout_toRightOf="@+id/imageView"
        android:layout_alignParentEnd="true" />
    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_below="@+id/imageView" />

</RelativeLayout>

There is also some GPS stuff going on in the background that updates textview2 everytime the location changes.

Was it helpful?

Solution 4

So after a couple of weeks, I have worked out that the way I had implemented the Location Listener was causing the issue - I have since completely re-written the code from the ground up, and all is now working.

OTHER TIPS

Its seems to be work now Please check your import part of AdapterView or AdapterView.OnItemClickListener Classes,

First of all you have 3 If lines. This is totally inappropriate. Use if with else if. This can easily cause the problem.

Second, you are using a custom ListView which means each row has a TextView inside. The rows itself have no values, but the objects in each row. For this reason the onItemClick always results in null because the program cannot retrieve the value of the TextView. What if you had an image and a button in the row as well? How would the program know what value you need when you click on an item?

Take a look at this tutorial and how it uses an Adapter class and implements clicks on rows. It's not easy but if you follow it, you can create a custom ListView in 10 minutes.

try your Custom layout like this.

<?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="fill_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="10dp"
android:textSize="18sp" >
</TextView>

and also you have to remove android:descendantFocusability="blocksDescendants" from your listView because The Doc says blocksDescendants : The ViewGroup will block its descendants from receiving focus.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top