簡體   English   中英

使用 ms sql 過程中的多列填充列表視圖

[英]Populate listview with multiple columns from ms sql procedure

在我的項目中,有必要實現從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;
    }
}

我需要做什么才能不覆蓋第一行,而是添加新行? 幫幫我,我被困在這一刻。 謝謝。

我在方法中修改了這部分:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM