所以我有一个对话框,其中启用了EditText
,当有人键入某个内容并点击添加按钮时,他们的文本会被添加到数据库中。这是我的主要活动"名为IndivItem.java
的代码。
public class IndivItem extends Activity {
TextView name, price;
Button descB, revB;
ImageView converse;
ImageButton fav, buy, compare;
TableLayout table_layout;
ReviewController revcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_indiv_item);
revcon = new ReviewController(this);
price = (TextView) findViewById(R.id.picPrice1);
name = (TextView) findViewById(R.id.text1);
descB = (Button) findViewById(R.id.desc);
revB = (Button) findViewById(R.id.review);
converse = (ImageView) findViewById(R.id.shoee1);
fav = (ImageButton) findViewById(R.id.fv1);
buy = (ImageButton) findViewById(R.id.co1);
compare = (ImageButton) findViewById(R.id.cp1);
table_layout = (TableLayout) findViewById(R.id.tableLayout1);
BuildTable();
revB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View arg0) {
// TODO Auto-generated method stub
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
IndivItem.this);
// Setting Dialog Title
alertDialog.setTitle("Review");
// Setting Dialog Message
alertDialog.setMessage("Add a Review");
final EditText input = new EditText(IndivItem.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
alertDialog
.setView(input)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
})
.setCancelable(false)
.setPositiveButton("Add",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
new MyAsync().execute();
// toast saying review added
Toast.makeText(getApplicationContext(),
"Review Added!",
Toast.LENGTH_SHORT).show();
Intent home = new Intent(
getApplicationContext(),
IndivItem.class);
startActivity(home);
}
// close dialog
});
// show alert
alertDialog.show();
}
});
fav.setOnHoverListener(new OnHoverListener() {
@Override
public boolean onHover(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
Toast.makeText(IndivItem.this, "Add to Favorites",
Toast.LENGTH_SHORT).show();
return false;
}
});
compare.setOnHoverListener(new OnHoverListener() {
@Override
public boolean onHover(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
Toast.makeText(IndivItem.this, "Add to Compare List",
Toast.LENGTH_SHORT).show();
return false;
}
});
}
private void BuildTable() {
// TODO Auto-generated method stub
revcon.open();
Cursor c = revcon.readEntry();
int rows = c.getCount();
int cols = c.getColumnCount();
c.moveToFirst();
// outer for loop
for (int i = 0; i < rows; i++) {
TableRow row = new TableRow(IndivItem.this);
row.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// inner for loop
for (int j = 0; j < cols; j++) {
TextView tv = new TextView(IndivItem.this);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(18);
tv.setPadding(0, 5, 0, 5);
tv.setText(c.getString(j));
row.addView(tv);
}
c.moveToNext();
table_layout.addView(row);
}
revcon.close();
}
private class MyAsync extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
String review = input.getText().toString();
return null;
revcon.open();
revcon.createEntry(review);
// BuildTable();
return null;
}}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
}
这一行会发生什么:String review = input.getText().toString();
,
给我一个错误,说错误无法解决。我不知道如何链接代码):或者我做错了什么,请赐教,谢谢!