问题描述
在我的项目中,需要实现从ms sql过程中将数据加载到listview中。我创建了我的列表视图布局,根据这个例子配置了适配器 - https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView。
但我遇到了一个问题 - 我的列表视图中没有创建新行,但第一行被覆盖了。
这是我的代码:
public void fillVagonList ()
{
ResultSet rs = null;
PreparedStatement ps = null;
try {
ConnectTosql connectTosql = new ConnectTosql();
sqlConnect = connectTosql.connect();
if (sqlConnect != null)
{
String SPsql = "EXEC mobile3_GETCARLIST ?,?,?";
ps = sqlConnect.prepareStatement(SPsql);
ps.setEscapeProcessing(true);
ps.setQueryTimeout(1000);
ps.setInt(1,LoginActivity.SesId);
ps.setString(2,EquipmentWagon.deadend);
ps.setString(3,EquipmentWagon.sectorName);
ps.setInt(4,EquipmentWagon.operID);
rs = ps.executeQuery();
vagonNumberList = new ArrayList<String>();
vagonNatList = new ArrayList<String>();
vagonLengthList = new ArrayList<String>();
//completeList = new ArrayList<String>();
while (rs.next())
{
Vagon vagon = new Vagon(rs.getString(2),rs.getString(1),rs.getString(5));
ArrayList<Vagon> vagonArrayList = new ArrayList<Vagon>();
VagonAdapter adapter = new VagonAdapter(getContext(),vagonArrayList);
lvVagonList.setAdapter(adapter);
adapter.addAll(vagon);
}
} else {
ConnectionResult = "Check Connection";
}
} catch (sqlException se)
{
System.out.println("Error al ejecutar sql" + se.getMessage());
se.printstacktrace();
throw new IllegalArgumentException("Error al ejecutar sql: " + se.getMessage());
} finally
{
try
{
rs.close();
ps.close();
sqlConnect.close();
}
catch (sqlException ex)
{
ex.printstacktrace();
}
}
}
Vagon 类:
public class Vagon {
public String carNumber;
public String natList;
public String vagonLength;
public Vagon(String carNumber,String natList,String vagonLength)
{
this.carNumber = carNumber;
this.natList = natList;
this.vagonLength = vagonLength;
}
}
VagonAdapter 类:
public class VagonAdapter extends ArrayAdapter<Vagon> {
public VagonAdapter(Context context,ArrayList<Vagon> vagons) {
super(context,vagons);
}
@Override
public View getView(int position,View convertView,ViewGroup parent) {
// Get the data item for this position
Vagon vagon = getItem(position);
// Check if an existing view is being reused,otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.adapter_vagon_view,parent,false);
}
// Lookup view for data population
TextView tvNomVag = (TextView) convertView.findViewById(R.id.tvNomVag);
TextView tvNatList = (TextView) convertView.findViewById(R.id.tvNatList);
TextView tvCarLength = (TextView) convertView.findViewById(R.id.tvCarLength);
// Populate the data into the template view using the data object
tvNomVag.setText(vagon.carNumber);
tvNatList.setText(vagon.natList);
tvCarLength.setText(vagon.vagonLength);
// Return the completed view to render on screen
return convertView;
}
}
我需要做什么才能不覆盖第一行,而是添加一个新行?帮帮我,我被困在这一刻。谢谢。
解决方法
我在方法中修改了这部分:
ArrayList<Vagon> vagonArrayList = new ArrayList<>();
for (int i = 0; i < vagonNumberList.size(); i++) {
try {
vagonArrayList.add(new Vagon(vagonNumberList.get(i),vagonNatList.get(i),vagonLengthList.get(i)));
} catch (Exception e) {
e.printStackTrace();
}
}
VagonAdapter adapter = new VagonAdapter(getContext(),vagonArrayList);
lvVagonList.setAdapter(adapter);