Android 中自定义列表视图单击上的 Toast 消息

发布于 2024-10-01 23:44:20 字数 1613 浏览 5 评论 0原文

我有一个自定义列表视图,其中每一行都有一个文本视图和一个复选框。 当我单击复选框时,我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难忘№最初的完美 2024-10-08 23:44:20

直接这样烤就可以了

Toast.makeText(Favourites.this, "Toast!", Toast.LENGTH_SHORT).show();

You can directly toast like this,

Toast.makeText(Favourites.this, "Toast!", Toast.LENGTH_SHORT).show();

水染的天色ゝ 2024-10-08 23:44:20

Toast 通知提供主要活动的上下文以显示它。我不太确定为什么您要在 onCheckedChanged() 方法中再次实例化您的活动,但我猜您正在尝试通过它来传递 Toast 上下文。试试这个方法。

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();
    }
}

并且请不要将您的 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 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.

怀中猫帐中妖 2024-10-08 23:44:20

我对您的代码有疑问...如果 convertview != null 会怎样
是否有任何假设让您跳过该检查?

I've a question about your code....what if convertview != null
is there any assumption why you skip that check?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文