Android 中自定义列表视图单击上的 Toast 消息
我有一个自定义列表视图,其中每一行都有一个文本视图和一个复选框。 当我单击复选框时,我想将 TextView 中的值添加到数据库并在视图中显示 Toast 消息。数据库插入工作正常。但应用程序突然停止,并且 Toast 消息未显示在视图中。
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();
}
}
}
}
}
}
请帮助我...谢谢...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
直接这样烤就可以了
You can directly toast like this,
为
Toast
通知提供主要活动的上下文以显示它。我不太确定为什么您要在onCheckedChanged()
方法中再次实例化您的活动,但我猜您正在尝试通过它来传递Toast
上下文。试试这个方法。并且请不要将您的
Toast
显示时间设置为一毫秒,这对您没有帮助。时间参数以毫秒为单位设置。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 theonCheckedChanged()
method but I guess you were trying to pass yourToast
a context with that. Try it this way.And please don't set your
Toast
display time to one millisecond that won't help you. The time parameters is set in milliseconds.我对您的代码有疑问...如果
convertview != null
会怎样是否有任何假设让您跳过该检查?
I've a question about your code....what if
convertview != null
is there any assumption why you skip that check?