用 this 声明一个上下文方法
如果我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
onClick 方法与 View 参数一起
您总是可以调用 view.getContext() 来获取 addBook 的上下文实例
onClick method goes with View parameter
You allways can call view.getContext() to obtain context instanse for your addBook
您应该将
book_edit.this
设置为您的方法的参数,这样应该可以工作。You should set
book_edit.this
as a parameter for your method, that should work.