用 this 声明一个上下文方法

发布于 2024-11-17 20:39:53 字数 1928 浏览 0 评论 0原文

如果我想在 onClick 方法中执行此方法,我将如何声明此方法:

public void addBook(Context context) {

它是 addBook(context); 吗?

我在实施它时遇到了麻烦。我认为它一直导致我的活动强制关闭,因为我输入错误:

public class book_edit extends Activity {

public String book_name;
public String isbn;
public String author;
public float ratings;
public EditText title;
public EditText book_author;
public EditText isbn_number;
public EditText name;
public RatingBar ratingbar;
public Button save;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_book_list);

    title = (EditText)findViewById(R.id.title);
    book_author = (EditText)findViewById(R.id.author);
    isbn_number = (EditText)findViewById(R.id.isbn);
    save = (Button)findViewById(R.id.save);

    save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

        }
    });

    final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingBar1);
    ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

        public void onRatingChanged(RatingBar ratingBar, float rating,      boolean fromUser) {

            ratingbar.setRating(rating);

        }
    });

    }

public void addBook(Context context) {
String tag = "Excersising BookProvider";
Log.d(tag, "adding a book");

ContentValues cv = new ContentValues();

cv.put(BookProviderMetaData.BookTableMetaData.BOOK_NAME, book_name);

cv.put(BookProviderMetaData.BookTableMetaData.BOOK_ISBN, isbn);

cv.put(BookProviderMetaData.BookTableMetaData.BOOK_AUTHOR, author);

ContentResolver cr = context.getContentResolver();

Uri uri    =  BookProviderMetaData.BookTableMetaData.CONTENT_URI;

Log.d(tag, "book insert uri: " +uri);

Uri insertedUri = cr.insert(uri, cv);

Log.d(tag, "inserted    uri:" + insertedUri);

   }


}

How would I declare this method if I wanted to do it in an onClick method:

public void addBook(Context context) {

Would it be addBook(context);?

I'm having trouble implementing it. I think it keeps causing my activity to force a close because I'm typing it wrong:

public class book_edit extends Activity {

public String book_name;
public String isbn;
public String author;
public float ratings;
public EditText title;
public EditText book_author;
public EditText isbn_number;
public EditText name;
public RatingBar ratingbar;
public Button save;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_book_list);

    title = (EditText)findViewById(R.id.title);
    book_author = (EditText)findViewById(R.id.author);
    isbn_number = (EditText)findViewById(R.id.isbn);
    save = (Button)findViewById(R.id.save);

    save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

        }
    });

    final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingBar1);
    ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

        public void onRatingChanged(RatingBar ratingBar, float rating,      boolean fromUser) {

            ratingbar.setRating(rating);

        }
    });

    }

public void addBook(Context context) {
String tag = "Excersising BookProvider";
Log.d(tag, "adding a book");

ContentValues cv = new ContentValues();

cv.put(BookProviderMetaData.BookTableMetaData.BOOK_NAME, book_name);

cv.put(BookProviderMetaData.BookTableMetaData.BOOK_ISBN, isbn);

cv.put(BookProviderMetaData.BookTableMetaData.BOOK_AUTHOR, author);

ContentResolver cr = context.getContentResolver();

Uri uri    =  BookProviderMetaData.BookTableMetaData.CONTENT_URI;

Log.d(tag, "book insert uri: " +uri);

Uri insertedUri = cr.insert(uri, cv);

Log.d(tag, "inserted    uri:" + insertedUri);

   }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

柠檬心 2024-11-24 20:39:53

onClick 方法与 View 参数一起

onClick(View view);

您总是可以调用 view.getContext() 来获取 addBook 的上下文实例

save.setOnClickListener(new View.OnClickListener() {
      @Override
     public void onClick(View view) {
        addBook(view.getContext());
     }     
}); 

onClick method goes with View parameter

onClick(View view);

You allways can call view.getContext() to obtain context instanse for your addBook

save.setOnClickListener(new View.OnClickListener() {
      @Override
     public void onClick(View view) {
        addBook(view.getContext());
     }     
}); 
笔落惊风雨 2024-11-24 20:39:53

您应该将 book_edit.this 设置为您的方法的参数,这样应该可以工作。

You should set book_edit.this as a parameter for your method, that should work.

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