在我的项目中,有必要实现将数据从 ms sql 过程加载到 listview 中。我创建了我的列表视图布局,根据此示例配置了适配器 - https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView。
但是我遇到了一个问题 - 在我的列表视图中没有创建新行,但是第一行被覆盖了。
这是我的代码:
调用sql过程并填充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();
}
}
}
货车类:
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, 0, 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;
}
}
我需要做什么才能不覆盖第一行,而是添加一个新行?帮帮我,我被困在这一刻。谢谢。