I want a multiple selection on this GridView, but I've a casting problem. This is my code:
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Checkable;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivityTest extends ActionBarActivity {
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setMultiChoiceModeListener(new MultiChoiceModeListener());
gridView.setAdapter(new NumberAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int numero = position+1;
LayoutInflater inflater = getLayoutInflater();
final View layout = inflater.inflate(R.layout.custom_toast_layout,
(ViewGroup) findViewById(R.id.custom_toast_layout));
TextView selectedNumber = (TextView) layout.findViewById(R.id.selected_number);
selectedNumber.setText(""+numero);
Context context = getBaseContext();
String text = ""+numero;
int duration = Toast.LENGTH_SHORT;
final Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(layout);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 600);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class CheckableLayout extends FrameLayout implements Checkable {
private boolean mChecked;
public CheckableLayout(Context context) {
super(context);
}
@SuppressWarnings("deprecation")
public void setChecked(boolean checked) {
mChecked = checked;
setBackgroundDrawable(checked ? getResources().getDrawable(
R.drawable.rounded_edittext_denim) : null);
}
public boolean isChecked() {
return mChecked;
}
public void toggle() {
setChecked(!mChecked);
}
}
public class MultiChoiceModeListener implements GridView.MultiChoiceModeListener {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Select Items");
mode.setSubtitle("One item selected");
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}
public void onDestroyActionMode(ActionMode mode) {
}
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
int selectCount = gridView.getCheckedItemCount();
switch (selectCount) {
case 1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + selectCount + " items selected");
break;
}
}
}
public class NumberAdapter extends BaseAdapter {
private Context context;
public NumberAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return 60;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckableLayout checkableLayout;
TextView numberView;
if (convertView == null) {
numberView = new TextView(context);
numberView.setLayoutParams(new GridView.LayoutParams(110, 110));
numberView.setPadding(0, 0, 0, 0);
numberView.setTextColor(Color.WHITE);
numberView.setBackgroundResource(R.drawable.rounded_edittext_zaffiro);
numberView.setGravity(Gravity.CENTER);
numberView.setTextSize(25);
checkableLayout = new CheckableLayout(MainActivityTest.this);
checkableLayout.addView(numberView);
} else {
//ERROR AT THIS POINT
checkableLayout = (CheckableLayout) convertView;
numberView = (TextView) checkableLayout.getChildAt(0);
}
numberView.setText("" + (position + 1));
return numberView;
}
}
}
java.lang.ClassCastException: android.widget.TextView cannot be cast to kangel.customtest.MainActivityTest$CheckableLayout
Anyone can help me to highlight selected item. Where is the error in my code?
you are returning the wrong object, that's the reason why you are getting the
ClassCastException
. In your customgetView
's changewith