0

我已經問過像這樣的東西,但我沒有找到任何解決方案..現在我想嘗試找到一種方式發佈整個Java代碼。 問題是,當我點擊列表視圖中的一個項目什麼都沒有發生。似乎onListItemClick不解僱。這是代碼:onListItemClick不能在列表視圖中使用片段

@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; 
    } 

} 

請我需要幫助..我們的目標是點擊一個項目並打開其他活動。由於

回答

2

你沒有連接監聽器的ListView:

list.setOnItemClickListener(this); 

然後你的類必須實現OnItemClickListener接口。然後,你必須實現此方法:

@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); 
} 
+0

我嘗試添加實現,但它說:'類型sensorsbattcpu必須實現繼承的抽象方法AdapterView.OnItemClickListener.onItemClick(AdapterView ,View,int,long)' –

+0

@David_D仔細檢查我的答案。 – Sajmon

+0

當你寫:'//做你的東西'正是我要寫的東西?和我保護的無效onListItemClick部分必須留在那裏? –

0

你的類需要實現OnItemClickListener

public class sensorsbattcpu extends Fragment implements OnItemClickListener{ 

然後

listView.setOnItemClickListener(this); 

覆蓋onItemClick

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

    } 
+0

'類型sensorsbattcpu必須實現繼承的抽象方法AdapterView.OnItemClickListener.onItemClick(適配器視圖,查看,的int,long)' –

+0

@David_D覆蓋onItemClick,做你的東西有 – Raghunandan