Android-ArrayList 中仅显示 ResultSet 的最后一个结果
Android-Only last result from ResultSet showing in ArrayList
这是 onPostExecute
上 AsyncTask
的代码块,在将值添加到 ArrayList
后,它会被最后一个索引值替换。
protected void onPostExecute(ResultSet s){
try {
int i = 0;
while(s.next()){
employeearray.add(new TableEmployee(s.getString("Name"),s.getString("Gender"),s.getString("Birthday"), s.getString("PositionName")));
Log.d("Process -->Getting data in resultset...", employeearray.get(i).getNameEmp());
i++;
}
} catch (SQLException e) {
e.printStackTrace();
}
//view inside list
for(int i = 0; i < employeearray.size(); i++){
Log.d("Process -->Getting data in array index "+i , employeearray.get(i).getNameEmp());
}
adapter.notifyDataSetChanged();
}
这是我的 logcat
添加 employeearray
时显示的内容
08-02 20:39:25.747 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Alfred Jones
08-02 20:39:25.750 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Ricardo Patnubayan
08-02 20:39:25.752 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Sam Makiling
08-02 20:39:25.754 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Nunally Sandatahan
08-02 20:39:25.757 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Barry Kennyard
08-02 20:39:25.760 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Jonnathan Teague
08-02 20:39:25.765 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Bill Mandy
08-02 20:39:25.766 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: ha lhahaha
这是在 while
循环之后
08-02 20:39:25.767 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 0: ha lhahaha
08-02 20:39:25.767 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 1: ha lhahaha
08-02 20:39:25.767 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 2: ha lhahaha
08-02 20:39:25.767 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 3: ha lhahaha
08-02 20:39:25.768 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 4: ha lhahaha
08-02 20:39:25.768 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 5: ha lhahaha
08-02 20:39:25.769 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 6: ha lhahaha
08-02 20:39:25.769 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 7: ha lhahaha
为什么 ArrayList
在 while 循环后不断被替换?我有另一个 activity 的类似代码,那个代码工作正常,但这个不行。
这是全部代码
public class EmployeeSearch extends AppCompatActivity {
ConnectionClass connectionClass;
ArrayList<TableEmployee> employeearray = new ArrayList<>();
ArrayAdapter<TableEmployee> adapter;
ListView employees;
ToggleButton filter;
CheckBox admin, engineer, manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_search);
connectionClass = new ConnectionClass();
filter = (ToggleButton) findViewById(R.id.toggleButton);
admin = (CheckBox) findViewById(R.id.cb_admin);
engineer = (CheckBox) findViewById(R.id.cb_engineer);
manager = (CheckBox) findViewById(R.id.cb_manager);
adapter = new propertyArrayAdapter(EmployeeSearch.this, 0, employeearray);
employees = (ListView) findViewById(R.id.lv_employee);
employees .setAdapter(adapter);
new AsyncProcess().execute();
}
protected class AsyncProcess extends AsyncTask<String, String, ResultSet> {
ResultSet rs ;
protected ResultSet doInBackground(String... params){
try {
Connection con = connectionClass.CONN();
if (con == null) {
Toast.makeText(EmployeeSearch.this,"Error in connection with SQL server",Toast.LENGTH_SHORT);
} else {
Log.d("Process -->", "Entering Query");
String query = "select (a.FirstName + ' ' + a.LastName) as Name, a.Gender, a.Birthday, b.PositionName from Employee as a, Position as b where a.PositionID = b.PositionID AND b.PositionName IN ('Administrator' , 'Project Engineer', 'Project Manager')";
Statement stmt = con.createStatement();
rs = stmt.executeQuery(query);
}
}
catch (Exception ex)
{
Log.e("Exceptions --->", ex.toString());
}
return rs;
}
protected void onPostExecute(ResultSet s){
try {
int i = 0;
while(s.next()){
employeearray.add(new TableEmployee(s.getString("Name"),s.getString("Gender"),s.getString("Birthday"), s.getString("PositionName")));
Log.d("Process -->Getting data in resultset...", employeearray.get(i).getNameEmp());
i ++;
}
} catch (SQLException e) {
e.printStackTrace();
}
//view inside list
for(int j = 0; j < employeearray.size(); j++){
Log.d("Process -->Getting data in array index "+j , employeearray.get(j).getNameEmp());
}
adapter.notifyDataSetChanged();
}
}
class propertyArrayAdapter extends ArrayAdapter<TableEmployee> {
private Context context;
private List<TableEmployee> emparray;
public propertyArrayAdapter(Context context, int resource, ArrayList<TableEmployee> objects) {
super(context, resource, objects);
this.context = context;
this.emparray = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
TableEmployee property = emparray.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.listitem_employesearch, null);
TextView name = (TextView) view.findViewById(R.id.tv_employeename);
TextView gender = (TextView) view.findViewById(R.id.tv_gender);
TextView birthday = (TextView) view.findViewById(R.id.tv_birthday);
TextView positionname = (TextView) view.findViewById(R.id.tv_position);
name.setText(property.getNameEmp());
gender.setText(property.getGender());
birthday.setText(property.getBirthday());
positionname.setText(property.getPosition());
return view;
}
}
}
表员工
public class TableEmployee extends Application {
public static String Name, Gender, Birthday, Position;
public TableEmployee(String Name, String Gender, String Birthday, String Position) {
super();
this.Name = Name;
this.Gender = Gender;
this.Birthday = Birthday;
this.Position = Position;
}
public TableEmployee() {
super();
this.Name = null;
this.Gender = null;
this.Birthday = null;
this.Position = null;
}
public void setNameEmp(String Name){
this.Name = Name;
}
public String getNameEmp(){
return Name;
}
public void setGender(String Gender){
this.Gender = Gender;
}
public String getGender(){
return Gender;
}
public void setBirthday(String Birthday){
this.Birthday = Birthday;
}
public String getBirthday(){
return Birthday;
}
public void setPosition(String Position){
this.Position = Position;
}
public String getPosition(){
return Position;
}
}
我认为这样做更好:
List<TableEmployee> tempList = new ArrayList();
try {
int i = 0;
while(s.next()){
tempList.add(new TableEmployee(s.getString("Name"),
s.getString("Gender"),s.getString("Birthday"),
s.getString("PositionName")));
i ++;
}
employeearray.addAll(tempList);
adapter.notifyDataSetChanged();
} catch (SQLException e) {
e.printStackTrace();
}
你能检查一下你的构造函数吗?您错误地使用错误的构造函数声明(使用静态声明和其他东西)创建了 TableEmployee 单例。
这是 onPostExecute
上 AsyncTask
的代码块,在将值添加到 ArrayList
后,它会被最后一个索引值替换。
protected void onPostExecute(ResultSet s){
try {
int i = 0;
while(s.next()){
employeearray.add(new TableEmployee(s.getString("Name"),s.getString("Gender"),s.getString("Birthday"), s.getString("PositionName")));
Log.d("Process -->Getting data in resultset...", employeearray.get(i).getNameEmp());
i++;
}
} catch (SQLException e) {
e.printStackTrace();
}
//view inside list
for(int i = 0; i < employeearray.size(); i++){
Log.d("Process -->Getting data in array index "+i , employeearray.get(i).getNameEmp());
}
adapter.notifyDataSetChanged();
}
这是我的 logcat
添加 employeearray
08-02 20:39:25.747 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Alfred Jones
08-02 20:39:25.750 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Ricardo Patnubayan
08-02 20:39:25.752 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Sam Makiling
08-02 20:39:25.754 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Nunally Sandatahan
08-02 20:39:25.757 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Barry Kennyard
08-02 20:39:25.760 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Jonnathan Teague
08-02 20:39:25.765 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: Bill Mandy
08-02 20:39:25.766 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in resultset...: ha lhahaha
这是在 while
循环之后
08-02 20:39:25.767 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 0: ha lhahaha
08-02 20:39:25.767 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 1: ha lhahaha
08-02 20:39:25.767 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 2: ha lhahaha
08-02 20:39:25.767 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 3: ha lhahaha
08-02 20:39:25.768 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 4: ha lhahaha
08-02 20:39:25.768 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 5: ha lhahaha
08-02 20:39:25.769 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 6: ha lhahaha
08-02 20:39:25.769 9295-9295/services.construction.dedase.jtdsdedase D/Process -->Getting data in array index 7: ha lhahaha
为什么 ArrayList
在 while 循环后不断被替换?我有另一个 activity 的类似代码,那个代码工作正常,但这个不行。
这是全部代码
public class EmployeeSearch extends AppCompatActivity {
ConnectionClass connectionClass;
ArrayList<TableEmployee> employeearray = new ArrayList<>();
ArrayAdapter<TableEmployee> adapter;
ListView employees;
ToggleButton filter;
CheckBox admin, engineer, manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_search);
connectionClass = new ConnectionClass();
filter = (ToggleButton) findViewById(R.id.toggleButton);
admin = (CheckBox) findViewById(R.id.cb_admin);
engineer = (CheckBox) findViewById(R.id.cb_engineer);
manager = (CheckBox) findViewById(R.id.cb_manager);
adapter = new propertyArrayAdapter(EmployeeSearch.this, 0, employeearray);
employees = (ListView) findViewById(R.id.lv_employee);
employees .setAdapter(adapter);
new AsyncProcess().execute();
}
protected class AsyncProcess extends AsyncTask<String, String, ResultSet> {
ResultSet rs ;
protected ResultSet doInBackground(String... params){
try {
Connection con = connectionClass.CONN();
if (con == null) {
Toast.makeText(EmployeeSearch.this,"Error in connection with SQL server",Toast.LENGTH_SHORT);
} else {
Log.d("Process -->", "Entering Query");
String query = "select (a.FirstName + ' ' + a.LastName) as Name, a.Gender, a.Birthday, b.PositionName from Employee as a, Position as b where a.PositionID = b.PositionID AND b.PositionName IN ('Administrator' , 'Project Engineer', 'Project Manager')";
Statement stmt = con.createStatement();
rs = stmt.executeQuery(query);
}
}
catch (Exception ex)
{
Log.e("Exceptions --->", ex.toString());
}
return rs;
}
protected void onPostExecute(ResultSet s){
try {
int i = 0;
while(s.next()){
employeearray.add(new TableEmployee(s.getString("Name"),s.getString("Gender"),s.getString("Birthday"), s.getString("PositionName")));
Log.d("Process -->Getting data in resultset...", employeearray.get(i).getNameEmp());
i ++;
}
} catch (SQLException e) {
e.printStackTrace();
}
//view inside list
for(int j = 0; j < employeearray.size(); j++){
Log.d("Process -->Getting data in array index "+j , employeearray.get(j).getNameEmp());
}
adapter.notifyDataSetChanged();
}
}
class propertyArrayAdapter extends ArrayAdapter<TableEmployee> {
private Context context;
private List<TableEmployee> emparray;
public propertyArrayAdapter(Context context, int resource, ArrayList<TableEmployee> objects) {
super(context, resource, objects);
this.context = context;
this.emparray = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
TableEmployee property = emparray.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.listitem_employesearch, null);
TextView name = (TextView) view.findViewById(R.id.tv_employeename);
TextView gender = (TextView) view.findViewById(R.id.tv_gender);
TextView birthday = (TextView) view.findViewById(R.id.tv_birthday);
TextView positionname = (TextView) view.findViewById(R.id.tv_position);
name.setText(property.getNameEmp());
gender.setText(property.getGender());
birthday.setText(property.getBirthday());
positionname.setText(property.getPosition());
return view;
}
}
}
表员工
public class TableEmployee extends Application {
public static String Name, Gender, Birthday, Position;
public TableEmployee(String Name, String Gender, String Birthday, String Position) {
super();
this.Name = Name;
this.Gender = Gender;
this.Birthday = Birthday;
this.Position = Position;
}
public TableEmployee() {
super();
this.Name = null;
this.Gender = null;
this.Birthday = null;
this.Position = null;
}
public void setNameEmp(String Name){
this.Name = Name;
}
public String getNameEmp(){
return Name;
}
public void setGender(String Gender){
this.Gender = Gender;
}
public String getGender(){
return Gender;
}
public void setBirthday(String Birthday){
this.Birthday = Birthday;
}
public String getBirthday(){
return Birthday;
}
public void setPosition(String Position){
this.Position = Position;
}
public String getPosition(){
return Position;
}
}
我认为这样做更好:
List<TableEmployee> tempList = new ArrayList();
try {
int i = 0;
while(s.next()){
tempList.add(new TableEmployee(s.getString("Name"),
s.getString("Gender"),s.getString("Birthday"),
s.getString("PositionName")));
i ++;
}
employeearray.addAll(tempList);
adapter.notifyDataSetChanged();
} catch (SQLException e) {
e.printStackTrace();
}
你能检查一下你的构造函数吗?您错误地使用错误的构造函数声明(使用静态声明和其他东西)创建了 TableEmployee 单例。