我在从数据库接收数据时遇到问题。根据我的代码确定,我的数据库(TBL_MAHRIE)包含三列(ID,COL_SAL,COL_NERKH)。运行我的项目,我在spinner和ListView中都收到了重复的数据。
Mahrie.java(我的SPINNER在这里加载)
package ir.dadpardaz.mahrie_dadpardaz;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Mahrie extends Activity {
private Spinner spinner_sal1, spinner_sal2;
private Button btn_calc;
EditText ed_shakhes_Nekah, ed_shakhes_Motalebe;
TextView tv_res;
float shakhes_Nekah, shakes_Motalebe, final_Res;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mahrie);
ed_shakhes_Motalebe = (EditText) findViewById(R.id.ed_shakhes_Motalebe);
ed_shakhes_Nekah = (EditText) findViewById(R.id.ed_shakes_Nekah);
tv_res = (TextView) findViewById(R.id.tv_res);
// DATABASE
DataBaseHandler databaseAdapter = new DataBaseHandler(this);
Shakhes shakhes1 = new Shakhes("1392", "250");
databaseAdapter.insertShakhes(shakhes1);
Shakhes shakhes2 = new Shakhes("1391", "200");
databaseAdapter.insertShakhes(shakhes2);
Shakhes shakhes3 = new Shakhes("1390", "150");
databaseAdapter.insertShakhes(shakhes3);
Shakhes shakhes4 = new Shakhes("1389", "100");
databaseAdapter.insertShakhes(shakhes4);
Shakhes shakhes5 = new Shakhes("1388", "50");
databaseAdapter.insertShakhes(shakhes5);
Shakhes shakhes6 = new Shakhes("1387", "25");
databaseAdapter.insertShakhes(shakhes6);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner_sal2 = (Spinner) findViewById(R.id.spinner_Sal2);
List<String> list = new ArrayList<String>();
DataBaseHandler databaseAdapter = new DataBaseHandler(this);
List<Shakhes> data_shakhes = null;
data_shakhes = databaseAdapter.getAllShakhes();
Context context = getApplicationContext();
for (int i = 0; i < data_shakhes.size(); i++) {
list.add((data_shakhes.get(i).getSal()));
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_sal2.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner_sal1 = (Spinner) findViewById(R.id.spinner_Sal1);
spinner_sal1
.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
// get the selected dropdown list value
public void addListenerOnButton() {
spinner_sal1 = (Spinner) this.findViewById(R.id.spinner_Sal1);
spinner_sal2 = (Spinner) findViewById(R.id.spinner_Sal2);
btn_calc = (Button) findViewById(R.id.btn_calc);
btn_calc.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
shakhes_Nekah = Float.parseFloat(ed_shakhes_Nekah.getText()
.toString());
shakes_Motalebe = Float.parseFloat(ed_shakhes_Motalebe
.getText().toString());
final_Res = shakes_Motalebe / shakhes_Nekah;
tv_res.setText(final_Res + "");
}
});
}
}
2.DataBase Handler.java
package ir.dadpardaz.mahrie_dadpardaz;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.ToggleButton;
public class DataBaseHandler {
SQLiteDatabase database;
public DataBaseHandler(Context context) {
ShakhesDatabaseOpenHelper shakhesDatabaseOpenHelper = new ShakhesDatabaseOpenHelper(
context, "shakhsdb.db", null, 1);
database = shakhesDatabaseOpenHelper.getWritableDatabase();
}
public class ShakhesDatabaseOpenHelper extends SQLiteOpenHelper {
public ShakhesDatabaseOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//db.execSQL("DROP TABLE IF EXISTS "+ "TBL_MAHRIE");
String query = "create table IF NOT EXISTS TBL_MAHRIE (ID INTEGER PRIMARY KEY, COL_SAL TEXT, COL_NERKH TEXT)";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ "TBL_MAHRIE");
}
}
public long insertShakhes(Shakhes shakhes) {
ContentValues values = new ContentValues();
values.put("COL_SAL", shakhes.getSal());
values.put("COL_NERKH", shakhes.getNerkh());
return database.insert("TBL_MAHRIE", null, values);
}
boolean isTableExists(SQLiteDatabase db, String tableName) {
if (tableName == null || db == null || !db.isOpen()) {
return false;
}
Cursor cursor = db
.rawQuery(
"SELECT COUNT(*) FROM sqlite_master WHERE type = ? AND name = ?",
new String[] { "table", tableName });
if (!cursor.moveToFirst()) {
return false;
}
int count = cursor.getInt(0);
cursor.close();
return count > 0;
}
public List<Shakhes> getAllShakhes() {
List<Shakhes> shakhess = null;
Cursor c = database.rawQuery("SELECT * FROM TBL_MAHRIE",null);
//Cursor c = database.query("TBL_MAHRIE", new String[] { "ID", "COL_SAL", "COL_NERKH" }, null, null, null, null, null);
shakhess = new ArrayList<Shakhes>();
Shakhes p = new Shakhes();
if (c.moveToFirst()) {
do {
p.setId((int) c.getLong(c.getColumnIndex("ID")));
p.setSal(c.getString(c.getColumnIndex("COL_SAL")));
p.setNerkh(c.getString(c.getColumnIndex("COL_NERKH")));
shakhess.add(p);
} while (c.moveToNext());
return shakhess;
}
return shakhess;
}
}
Mahrie_JadvalActivity(我的ListView在这里充气)
package ir.dadpardaz.mahrie_dadpardaz;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Mahrie_JadvalActivity extends Activity {
ListView personListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mahrie__jadval);
personListView = (ListView) findViewById(R.id.personListView);
DataBaseHandler databaseAdapter = new DataBaseHandler(this);
Shakhes shakhes1 = new Shakhes("2392" , "250");
databaseAdapter.insertShakhes(shakhes1);
Shakhes shakhes2 = new Shakhes("2391" , "200");
databaseAdapter.insertShakhes(shakhes2);
Shakhes shakhes3 = new Shakhes("2390" , "150");
databaseAdapter.insertShakhes(shakhes3);
Shakhes shakhes4 = new Shakhes("2389" , "100");
databaseAdapter.insertShakhes(shakhes4);
Shakhes shakhes5 = new Shakhes("2388" , "50");
databaseAdapter.insertShakhes(shakhes5);
Shakhes shakhes6 = new Shakhes("2387" , "25");
databaseAdapter.insertShakhes(shakhes6);
List<Shakhes> shakhes =
databaseAdapter.getAllShakhes() ;
ShakhesListViewAdapter shakhesListViewAdapter = new ShakhesListViewAdapter(this, R.layout.mahrie_list_view_item, shakhes);
personListView.setAdapter(shakhesListViewAdapter);
}
public class ShakhesListViewAdapter extends ArrayAdapter<Shakhes> {
List<Shakhes> data;
Context context;
public ShakhesListViewAdapter(Context context, int resourceId, List<Shakhes> data) {
super(context, resourceId, data);
this.data = data;
this.context = context;
}
@Override
public View getView(final int position, View item, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
item = inflater.inflate(R.layout.mahrie_list_view_item, parent , false);
TextView familyTextView = (TextView) item.findViewById(R.id.familyTextView);
familyTextView.setText(data.get(position).getSal()) ;
//familyTextView.setTypeface(bYekan);
TextView nameTextView = (TextView) item.findViewById(R.id.nameTextView);
nameTextView.setText(data.get(position).getNerkh()) ;
//nameTextView.setTypeface(bYekan);
Button detailsButton = (Button) item.findViewById(R.id.detailsButton);
detailsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(context,
// data.get(position).getSal() + " " + data.get(position).getNerkh(),
// Toast.LENGTH_LONG).show();
Toast.makeText(context,
data.size()+"" + " and ID is " + position + "" ,
Toast.LENGTH_LONG).show();
}
});
//item.startAnimation(animation);
return item;
}
} }
4.Shakhes.java
package ir.dadpardaz.mahrie_dadpardaz;
public class Shakhes {
public int id ;
public static String sal ;
public static String nerkh ;
public Shakhes(int id, String sal, String nerkh) {
super();
this.id = id;
this.sal = sal;
this.nerkh = nerkh;
}
public Shakhes(String sal, String nerkh) {
super();
this.sal = sal;
this.nerkh = nerkh;
}
public Shakhes() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getSal() {
return sal;
}
public void setSal(String sal) {
this.sal = sal;
}
public String getNerkh() {
return nerkh;
}
public void setNerkh(String nerkh) {
this.nerkh = nerkh;
}
}
答案 0 :(得分:0)
在DataBaseHandler.java中,进行以下更改
public List<Shakhes> getAllShakhes() {
List<Shakhes> shakhess = null;
Cursor c = database.rawQuery("SELECT * FROM TBL_MAHRIE",null);
//Cursor c = database.query("TBL_MAHRIE", new String[] { "ID", "COL_SAL", "COL_NERKH" }, null, null, null, null, null);
shakhess = new ArrayList<Shakhes>();
if (c.moveToFirst()) {
do {
Shakhes p = new Shakhes();
p.setId((int) c.getLong(c.getColumnIndex("ID")));
p.setSal(c.getString(c.getColumnIndex("COL_SAL")));
p.setNerkh(c.getString(c.getColumnIndex("COL_NERKH")));
shakhess.add(p);
} while (c.moveToNext());
return shakhess;
}
return shakhess;
}
}
在do-while循环内编码行Shakhes p = new Shakhes();
。否则它将覆盖旧的,并且会在arraylist中添加相同的内容。