我已经问了类似这样的东西,但我找不到任何解决方案..现在我想尝试找出一个发布整个java代码的方法。
问题是,当我点击列表视图中的某个项目时,没有任何反应。似乎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;
}
}
我需要帮助..目标是点击某个项目并打开其他活动。感谢
答案 0 :(得分: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 );
}
答案 1 :(得分: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
}