我对我的节目有些怀疑。 我已成功将日期成功插入我的" producttable"。
我可以直接成功添加到表中,但不能直接添加到表中 " EDITTEXT&#34 ;.一旦我点击ADD。
记录的ID确实增加了,但另一列仍为空。
在这里输入代码
+++ +++感谢
stockmanagement.java
package ping.stock.check;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import ping.stock.check.DBAdapter.DatabaseHelper;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import android.R.id;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.database.*;
public class stockmanagement extends Activity implements OnClickListener{
Button button;
final Context context = this;
public Button scanBtn, AddBtn;
public EditText proidTxt, pronameTxt, prounitTxt, prolowTxt, prorackTxt;
DBAdapter DB = new DBAdapter(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stock_man);
cleartextfield();
tomenu();
scanBtn = (Button)findViewById(R.id.SCANBttn);
scanBtn.setOnClickListener(this);
prounitTxt = (EditText)findViewById(R.id.PRQTxt);
prolowTxt = (EditText)findViewById(R.id.LSATxt);
AddBtn = (Button)findViewById(R.id.ADDBttn);
AddBtn.setOnClickListener(new OnClickListener() {
String name = prounitTxt.getText().toString();
String code = prolowTxt.getText().toString();
@Override
public void onClick(View view) {
DB.open();
//long id = DB.insertProduct("DATA1","DATA2");
long id = DB.insertProduct(name, code);
DB.close();
}
});
}
private void tomenu() {
Button MMenuButton = (Button) findViewById(R.id.MMENUBttn);
MMenuButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent SMintent = new Intent(context, Welcome.class);
startActivity(SMintent);
stockmanagement.this.finish();
}
});
}
public void cleartextfield() {
Button CLRButton = (Button)findViewById(R.id.CLRBttn);
final EditText PRIDText = (EditText) findViewById(R.id.PRIDTxt);
final EditText PRNAMEText = (EditText) findViewById(R.id.PRNAMETxt);
final EditText PRQText = (EditText) findViewById(R.id.PRQTxt);
final EditText LSAText = (EditText) findViewById(R.id.LSATxt);
final EditText RACKText = (EditText) findViewById(R.id.RACKTxt);
CLRButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
PRIDText.setText("");
PRNAMEText.setText("");
PRQText.setText("");
LSAText.setText("");
RACKText.setText("");
}
});
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.SCANBttn){
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
pronameTxt.setText("FORMAT: " + scanFormat);
proidTxt.setText("CONTENT: " + scanContent);
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
DBAdapter.java
package ping.stock.check;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
static final String KEY_ROWID = "_id";
static final String KEY_NAME = "PRONAME";
static final String KEY_CODE= "PROCODE";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "PINGSTOCKCHECKDATA";
static final String DATABASE_TABLE = "producttable";
static final int DATABASE_VERSION = 2;
public static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE +"("
+ KEY_ROWID + "integer primary key autoincrement,"
+ KEY_NAME + "text not null,"
+ KEY_CODE + "text not null);";
public final Context context;
public DatabaseHelper DBHelper;
public SQLiteDatabase DB;
public DBAdapter (Context ctx){
this.context = ctx;
DBHelper = new DatabaseHelper (context);
}
public static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper (Context context){
super (context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
db.execSQL(DATABASE_CREATE);
}
catch(Exception e){
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS producttable");
onCreate(db);
}
}
// open the Database
public DBAdapter open() throws SQLException {
DBHelper = new DatabaseHelper(context);
DB = DBHelper.getWritableDatabase();
return this;
}
public void close(){
DBHelper.close();
}
public long insertProduct(String name, String code) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_CODE, code);
return DB.insert(DATABASE_TABLE, null, initialValues);
}
stock_man.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabletlayoutx"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="2"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/bg_pic"
>
<TableRow
android:id="@+id/tableRowx"
android:layout_width="wrap_content"
android:layout_height="5dip" >
</TableRow>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:stretchColumns="2" >
<TableRow
android:id="@+id/tableRow0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<View
android:layout_height="5dip"
android:background="#FF0000" />
<TextView
android:id="@+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Stock Management"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0000CC"
android:textStyle="bold"
android:typeface="monospace" />
<View
android:layout_height="5dip"
android:background="#FF0001" />
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView6"
android:layout_width="10dip"
android:layout_height="wrap_content"
android:layout_margin="3dip"
android:layout_weight="1"
android:gravity="center"
android:padding="0dip"
android:text="Product"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/textView7"
android:layout_width="5dip"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="3dip"
android:layout_weight="1"
android:padding="0dip"
android:text="Description"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="10dip "
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="5dip"
android:text="ID No."
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/PRIDTxt"
android:layout_width="5dip "
android:layout_height="wrap_content"
android:layout_margin="0dip"
android:layout_weight="1"
android:ems="10"
android:hint="The Product ID"
android:padding="5dip"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="30dip "
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="5dip"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/PRNAMETxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:ems="10"
android:hint="The Product Name"
android:padding="5dip"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="10dip "
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="5dip"
android:text="Unit"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/PRQTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:ems="10"
android:hint="Quantites (Number)"
android:padding="0dip"
android:maxLength="9"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="5dip"
android:text="Low Stock Alert"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/LSATxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_weight="1"
android:ems="10"
android:hint="Alert for LOW STOCK"
android:padding="0dip"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView5"
android:layout_width="10dip "
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:padding="5dip"
android:text="Rack No."
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/RACKTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="1"
android:ems="10"
android:hint="Product Rack Location"
android:padding="0dip"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</TableRow>
<TableRow
android:id="@+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/ADDBttn"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:layout_weight="1"
android:padding="5dip"
android:text="ADD"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<Button
android:id="@+id/CLRBttn"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:layout_weight="1"
android:padding="5dip"
android:text="CLEAR"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</TableRow>
<TableRow
android:id="@+id/tableRow07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Button
android:id="@+id/SCANBttn"
style="?android:attr/buttonStyleSmall"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="Scan" />
</TableRow>
<TableRow
android:id="@+id/tableRow8"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/MMENUBttn"
style="?android:attr/buttonStyleSmall"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="MAIN MENU"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</TableRow>
<View
android:layout_height="5dip"
android:background="#FF0000"
/>
</TableLayout>
</RelativeLayout>
</ScrollView>
</TableLayout>
答案 0 :(得分:0)
在设置getText()
侦听器时,您过早地在edittexts上调用onClick()
。将getText()
移至onClick()
本身以获取点击数据。