GridView
Добавлено: 26 сен 2014, 13:20
Здравствуйте. Не подскажете почему не работает нажатие на GridView? Вроде все делаю правильно, но что - то нажатие на элемент ничего не выявляет
Код: Выделить всё
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity2 extends ActionBarActivity implements LoaderManager.LoaderCallbacks<Cursor> {
int position;
DataBaseHelper myDbHelper;
SQLiteDatabase mydb;
SimpleCursorAdapter scAdapter;
GridView grid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Intent intent = getIntent();
position = Integer.parseInt(intent.getStringExtra("number"));
grid = (GridView) findViewById(R.id.gridView2);
grid.setEnabled(true);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "sadsa", Toast.LENGTH_SHORT).show();
}
});
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException e) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
mydb = myDbHelper.getWritableDatabase();
String[] from = new String[] { "pic", "otvet", "indikator" };
int[] to = new int[] { R.id.imagepart, R.id.textpart, R.id.cBox};
scAdapter = new SimpleCursorAdapter(this, R.layout.cellgrid, null, from, to, 0);
scAdapter.setViewBinder(new MyViewBinder());
grid.setAdapter(scAdapter);
getLoaderManager().initLoader(0, null, this);
}
void startIntent(){
Intent intent1 = new Intent(this, MyActivity4.class);
startActivity(intent1);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bnd) {
return new MyCursorLoader(this, mydb, position);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
scAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
static class MyCursorLoader extends CursorLoader {
SQLiteDatabase db;
int position;
public MyCursorLoader(Context context, SQLiteDatabase db, int position) {
super(context);
this.db = db;
this.position = position;
}
@Override
public Cursor loadInBackground() {
Cursor cursor = db.query("myTable" + Integer.toString(position), null, null, null, null, null, null );
return cursor;
}
}//MyCursorLoader
class MyViewBinder implements SimpleCursorAdapter.ViewBinder {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int numb;
switch (view.getId()) {
case R.id.imagepart:
return true;
case R.id.textpart:
String otv = cursor.getString(cursor.getColumnIndex("_id"));
((TextView)view).setText(otv);
return true;
case R.id.cBox:
numb = Integer.parseInt(cursor.getString(cursor.getColumnIndex("indikator")));
if (numb == 0){
((CheckBox)view).setChecked(false);
}
else{
((CheckBox)view).setChecked(true);
}
return true;
}
return false;
}
}
}