Android ArrayList中显示的结果集的最后一个结果
这是来自Android ArrayList中显示的结果集的最后一个结果,android,listview,jtds,Android,Listview,Jtds,这是来自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
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();
}
在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
为什么在while循环之后,ArrayList
不断被替换?我从另一个活动中得到了一个类似的代码,这个代码很好用,但是这个不行
这就是全部代码
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;
}
}
}
我认为这样做更好:
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();
}
List templast=new ArrayList();
试一试{
int i=0;
而(s.next()){
添加(新的TableEmployee(s.getString(“名称”),
s、 getString(“性别”),s.getString(“生日”),
s、 getString(“PositionName”);
i++;
}
employeearray.addAll(圣殿骑士);
adapter.notifyDataSetChanged();
}捕获(SQLE异常){
e、 printStackTrace();
}
您能检查一下您的构造函数吗。错误地使TableEmployee单例具有错误的构造函数声明(使用静态声明和其他东西)。您是否也可以添加TableEmployee类的构造函数。一些参考问题它看起来是什么类型的“employeearray”,你调试过吗?在catch语句之后,“employeearray”的内容是否会更改?能否添加构造函数。错误地使TableEmployee单例具有错误的构造函数声明我添加了TableEmployee,是的employeearray的值在catch语句之后更改,调试时没有错误it@kapsym这太愚蠢了,我之前没有注意到,你的权利问题在TableEmployee中,我声明字符串为static,当我删除它时,catch语句之后的数组值现在与resultset中的数组值相同,非常感谢!ArrayList employeearray=新的ArrayList();我就是这样做的,我使用customtable,我将编辑我的问题并显示整个代码
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();
}