使用addView元素到LinearLayout的Android只显示添加的最后一个元素

时间:2017-04-17 18:10:51

标签: android postgresql android-layout android-scrollview

我正在尝试为我从postgreSQL数据库恢复的每一行动态添加textView到嵌套在ScrollView中的LinearLayout,因为后者只能有一个直接子项。但是,只显示添加到LinearLayout的最后一个子项。

activity_main.xml中:

            <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:id="@+id/scrollView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            </LinearLayout>
        </ScrollView>

MainActivity.java(出于安全原因,我用X替换了数据库连接字符串):

//On the onCreate:
scrollView = (LinearLayout) findViewById(R.id.scrollView);

public void startThread(){

    currentThread = new Thread(){
        public void run(){

            alertHandler = new Handler(Looper.getMainLooper());

            try{

                Class.forName("org.postgresql.Driver");

                String url = "jdbc:postgresql://x";
                Connection conn = DriverManager.getConnection(url,"x","x");

                String statementSQL = "SELECT * FROM x.User;";
                Statement statement = conn.createStatement();
                ResultSet resultSet = statement.executeQuery(statementSQL);

                while(resultSet.next()){
                    try{
                        incident = new Incident();
                        incident.setID(resultSet.getInt(1));
                        incident.setType(resultSet.getString(2));
                        incident.setNature(resultSet.getString(3));
                        resultsData.add(incident);

                        alertHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                setScrollViewElements(incident.getID());
                            }
                        });
                    }catch (Exception ex){
                        ex.printStackTrace();
                    }
                }

                conn.close();

            } catch (SQLException se){
                se.printStackTrace();

            } catch (ClassNotFoundException e){
                e.printStackTrace();
            } catch (Exception ex){
                ex.printStackTrace();
            }
            Thread.currentThread().interrupt();
        }
    };

    if (currentThread.getState() == Thread.State.NEW){
        currentThread.start();
    }
}

public void setScrollViewElements(int id){
    try{
        TextView scrollElement = new TextView(this);
        scrollElement.setOnClickListener(listener);

        scrollElement.setId(id);
        scrollElement.setText("Incidencia No." + id);

        scrollView.addView(scrollElement);
    }catch (Exception ex){
        ex.printStackTrace();
    }
}

public class Incident {
    private String type;
    private String nature;
    private Date date;
    private int id;
    private double latitude;
    private double longitude;

    public void setType(String type) {
        this.type = type;
    }
    public String getType() {
        return type;
    }

    public void setNature(String nature) {
        this.nature = nature;
    }
    public String getNature() {
        return nature;
    }

    public void setDate(Date date) {
        this.date = date;
    }
    public Date getDate() {
        return date;
    }

    public void setID(int id) {
        this.id = id;
    }
    public int getID() {
        return id;
    }

    public void setLatitude(double lat) {
        this.latitude = lat;
    }
    public double getLatitude() {
        return latitude;
    }

    public void setLongitude(double lon) {
        this.longitude = lon;
    }
    public double getLongitude() {
        return longitude;
    }

}

public View.OnClickListener listener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        for (Incident inc: resultsData){
            if(inc.getID() == v.getId()){
                Log.d("Error",""+inc.getID());
            }
        }
    }
};

0 个答案:

没有答案