Question

I've already ask something like this one but i didn't find any solution.. Now i want try to find a way out posting the entire java code. The problem is that when i click over a item in the listview nothing happen. Seems that the onListItemClick is not fired.. This is the code:

@SuppressLint("NewApi")
public class sensorsbattcpu extends Fragment{
    private ListView listView;
    private SensorManager mSensorManager;
    private List<Sensor> deviceSensors = null;
    public Context context;
    ListView list;

    public static final String PREF_CAPTURE_STATE = "captureState";
    public static final String PREF_CAPTURE_FILE = "captureStatePrefs";
    static final int MENU_CAPTURE_ON = 1;
    static final int MENU_CAPTURE_OFF = 2;

    /** Called when the activity is first created. */
    @SuppressLint("NewApi")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Retrieving the currently selected item number
        int position = getArguments().getInt("position");
        // List of rivers
        String[] menus = getResources().getStringArray(R.array.menus);
        View v = inflater.inflate(R.layout.sensorsfragment, container, false);
        // Listview nel sensors.xml layout
        listView = ((ListView)v.findViewById(R.id.listView1));


            mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
            ArrayList<SensorItem> items = new ArrayList<SensorItem>();
            List<Sensor> sensors = mSensorManager.getSensorList( Sensor.TYPE_ALL );
            for( int i = 0 ; i < sensors.size() ; ++i )
            items.add( new SensorItem( sensors.get( i ) ) );

            sensorAdapter = new ArrayAdapter( getActivity(),R.layout.customlistview,R.id.text1,items );
            listView.setAdapter( sensorAdapter );
            SharedPreferences appPrefs = getActivity().getSharedPreferences( PREF_CAPTURE_FILE, Context.MODE_PRIVATE );
            captureState = appPrefs.getBoolean( PREF_CAPTURE_STATE, false );


            return v;
        }


    public void onPause() {
        super.onPause();
        SharedPreferences appPrefs = getActivity().getSharedPreferences( PREF_CAPTURE_FILE, Context.MODE_PRIVATE );
        SharedPreferences.Editor ed = appPrefs.edit();
        ed.putBoolean( PREF_CAPTURE_STATE, captureState );
        ed.commit();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = onCreateOptionsMenu(menu);
        menu.add(0, MENU_CAPTURE_ON, 1, R.string.capture_on );
        menu.add(0, MENU_CAPTURE_OFF, 2, R.string.capture_off );
        return result;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch( id ) {
            case MENU_CAPTURE_ON:
                captureState = true;
                break;

            case MENU_CAPTURE_OFF:
                captureState = false;
                break;
        }
        return true;
    }

    @SuppressLint("NewApi")
    protected void onListItemClick(ListView l, View v, int positionItem, long id) {

        Sensor sensor = sensorAdapter.getItem( positionItem ).getSensor();
        String sensorName = sensor.getName();
Intent i = new Intent();
        i.setClassName( "com.ab.myapp","com.ab.myapp.SensorMonitor" );
        i.putExtra( "sensorname",sensorName );
        startActivity( i );
    }


    private ArrayAdapter<SensorItem> sensorAdapter;
    private boolean captureState = false;

    class SensorItem {
        SensorItem( Sensor sensor ) {
            this.sensor = sensor;
        }

        @SuppressLint("NewApi")
        public String toString() {
            return sensor.getName();
        }

        Sensor getSensor() {
            return sensor;
        }

        private Sensor sensor;
    }

}

Please i need a help.. The goal is click on a item and open the other activity. Thanks

Was it helpful?

Solution

You didn't attached listener to ListView:

list.setOnItemClickListener(this);

And then your class have to implements OnItemClickListener interface. And then you must implement this method:

@Override
public void onItemClick(AdapterView<?> parent, View row, int pos, long id) { 
   Sensor sensor = sensorAdapter.getItem(pos).getSensor();
   String sensorName = sensor.getName();
   Intent i = new Intent();
   i.setClassName( "com.ab.myapp","com.ab.myapp.SensorMonitor" );
   i.putExtra( "sensorname",sensorName );
   startActivity( i );
}

OTHER TIPS

Your class needs to implement OnItemClickListener

public class sensorsbattcpu extends Fragment implements OnItemClickListener{

Then

listView.setOnItemClickListener(this);

Override onItemClick

@Override
public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          //do something

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