Reputation: 547
I have a custom listview in which one textview and one checked box is in each row. When I click the ckeckbox, I want to add the value from TextView to database and to display a Toast message in the view. The database insertion is working fine. But the application is stopping suddently, and the Toast message is not shown in the view.
public class Favourites extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_list);
ListView listitems = (ListView) findViewById(R.id.ListView_01);
listitems.setAdapter(new EfficientAdapter(this));
}
private static class EfficientAdapter extends BaseAdapter
{
//imp
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
int itemId=SubListIdList.get(position);
TextView text = (TextView) convertView.findViewById(R.id.TextView_02);
CheckBox star = (CheckBox) convertView.findViewById(R.id.star);
star.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
Favourites f=new Favourites();
query="UPDATE "+SUB_TABLE_NAME+" SET FavoriteIndicator='Y' WHERE SubListID="+itemId;
db.execSQL(query);
Toast.makeText(f, "Insertion successfull!", 1).show();
}
}
}
}
}
}
Please help me.... Thank you...
Upvotes: 1
Views: 2206
Reputation: 262
I've a question about your code....what if convertview != null
is there any assumption why you skip that check?
Upvotes: 0
Reputation: 3466
You can directly toast like this,
Toast.makeText(Favourites.this, "Toast!", Toast.LENGTH_SHORT).show();
Upvotes: 1
Reputation: 39604
Give the Toast
notification your main activitiy's context to display it. I'm not quite sure why you are instantiating your activity again in the onCheckedChanged()
method but I guess you were trying to pass your Toast
a context with that. Try it this way.
Context ctx = null;
onCreate(Bundle bundle) {
super.onCreate(bundle);
// more stuff
ctx = getApplication();
// even more stuff
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
query="UPDATE "+SUB_TABLE_NAME+" SET FavoriteIndicator='Y' WHERE" +
" SubListID="+itemId;
db.execSQL(query);
Toast.makeText(ctx, "Insertion successfull!", 4000).show();
}
}
And please don't set your Toast
display time to one millisecond that won't help you. The time parameters is set in milliseconds.
Upvotes: 0