Android-仅显示在 ArrayList 中的 ResultSet 的最后一个结果

上校

这是来自AsyncTaskon的代码块onPostExecute,在将值添加到 后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 循环后不断被替换?我有来自另一个活动的类似代码,该代码工作正常,但没有。

这是整个代码

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;
    }


}
卡皮尔

你能检查你的构造函数吗?您错误地使用错误的构造函数声明(使用静态声明和东西)使 TableEmployee 单例。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Android中仅显示最后一个ArrayList

仅使用Mpchart在android中显示的第一个和最后一个标签

Java中的ArrayList仅打印列表中的最后一个对象

在android ListView中,最后一个元素重复显示

Android:MySQL的ListView仅显示最后一个元素

仅删除字符串ArrayList中的最后一个单词

ArrayList仅复制定界符文件中的最后一个值

如何仅修复Java ArrayList中重复的循环中的最后一个对象

仅显示列表中的最后一个数组

如何修复PHP数组爆炸结果在“表单选择”中仅显示最后一个值?

Android如何在Arraylist中显示来自另一个活动的意图的图像?

Android:如何在列表中的OnItemLongClick上显示来自另一个ArrayList的数据

结果仅作为最后一个结果追加到列表中

swift - 最后一个数组结果是仅在 tableview 中显示的结果

Android解密:结果文件中缺少最后一个块(16个字节)

我的Hashmap对象值列表对象仅返回我放置在ArrayList <Map <String,Object >>>();中的最后一个对象;

Arraylist哈希图仅添加最后一个元素

警报管理器仅触发 Android 中的最后一个计划任务

仅将最后一个元素添加到 Android 中的 RecyclerView (Kotlin)

从ArrayList中移除仅提供一个属性的对象

ruby 模板中的深度层次查找仅返回最后一个结果

Android arrayList整个项目被最后一个替换

如何仅显示最后一个ajax请求结果

JSON输出仅显示最后一个结果

Android Recyclerview中的多种视图类型仅显示一个视图

在android中创建一个空的ArrayList

只返回ArrayList中的最后一个元素

在Java中删除ArrayList的最后一个对象

在Java中显示Android计算器的最后一个运算符