in my project it is necessary to implement loading data from ms sql procedure into listview. I created my listview layout, configured the adapter according to this example - https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView .
But I ran into a problem - no new lines are created in my listview, but the first one is overwritten.
Here is my code:
Method to call sql procedure and populate 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 class:
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 class:
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;
}
}
What do I need to do so that the first line is not overwritten, but a new one is added? Help me out, i'm stucked on this moment. Thanks.
i modified this part in method :
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.