【问题标题】:Toast message on Custom Listview Click in Android自定义列表视图上的 Toast 消息单击在 Android 中
【发布时间】:2010-11-12 10:04:44
【问题描述】:

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

请帮帮我……谢谢……

【问题讨论】:

    标签: android listview toast


    【解决方案1】:

    可以这样直接吐司,

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

    【讨论】:

      【解决方案2】:

      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 显示时间设置为一毫秒,这对您没有帮助。时间参数以毫秒为单位设置。

      【讨论】:

      • 非常感谢......它工作正常......但 Toast 显示持续时间“1”对我来说工作正常......
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-19
        • 1970-01-01
        • 1970-01-01
        • 2012-06-24
        • 2015-07-26
        • 2023-03-29
        相关资源
        最近更新 更多