Android ListView中的数据重复

时间:2017-07-04 16:35:21

标签: android sql sql-server-2008 listview

我刚刚开始专门为一个项目学习android编程,并且陷入了一个非常基本的事情,尽管这不应该。问题是我使用Sql数据库在Android ListView中显示产品详细信息。这工作正常。我再次使用按钮点击添加产品数据。当我添加产品时,ListView会重复出现。与新添加的产品一样,之前的产品详细信息会在ListView中重复两次,如下图所示:

Android ListView
注意:在图像中,诺基亚(诺基亚Lumia)是新输入的产品。

添加按钮中,我刚刚调用ShowProducts方法刷新ListView,如下所示:

if(isSuccess == true) {
     ShowProducts showProducts = new ShowProducts();
     showProducts.execute();
}

我不确定为什么会这样。已调试但无法弄清楚。甚至使用 SimpleAdapter notifyDataSetChanged方法进行刷新但无法正常工作。这是表结构:

CREATE TABLE [dbo].[Products](
 [ProductId] [int] IDENTITY(1,1) PRIMARY KEY,
 [ProductName] [nvarchar](60) NOT NULL, 
 [Code] [nvarchar](60) NOT NULL, 
 [Code] [nvarchar](60) NOT NULL, 
 [Date] [datetime] NOT NULL
)  

在项目文件夹 app \ src \ main \ java \ com.example.at.projectname 文件夹中,创建了几个类:

Products.java

public class Products {
    private String code;
    private String name;
    private String price;

    public String getCode(String productCode) {
        code = productCode;
        return code;
    }

    public String getName(String productName) {
        name = productName;
        return name;
    }

    public String getPrice(String productPrice) {
        price = productPrice;
        return price;
    }
}

ConnectionClass.java

public class ConnectionClass {
    String ip = "";
    String classs = "net.sourceforge.jtds.jdbc.Driver";
    String db = "sampleDB";

    @SuppressLint("NewApi")
    public Connection CONN() {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();

        StrictMode.setThreadPolicy(policy);
        Connection conn = null;
        String ConnURL = null;

        try {
            Class.forName(classs);
            ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
                    + "databaseName=" + db + ";user=" + ";password="
                    + ";";
            conn = DriverManager.getConnection(ConnURL);
        } catch (SQLException se) {
            Log.e("Error: ", se.getMessage());
        } catch (ClassNotFoundException e) {
            Log.e("Error: ", e.getMessage());
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
        return conn;
    }
}

ProductActivity.java

public class ProductActivity extends Activity {

    ConnectionClass connectionClass;
    EditText edtpProductName, edtProductCode, edtProductPrice;
    Button btnAdd, btnUpdate, btnDelete;
    ProgressBar pbbar;
    ListView lstProduct;
    String productCode;

    List<Map<String, String>> productList = new ArrayList<Map<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.products);

        connectionClass = new ConnectionClass();

        edtpProductName = (EditText) findViewById(R.id.edtProductName);
        edtProductCode = (EditText) findViewById(R.id.edtProductCode);
        edtProductPrice = (EditText) findViewById(R.id.edtProductPrice);

        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnUpdate = (Button) findViewById(R.id.btnUpdate);
        btnDelete = (Button) findViewById(R.id.btnDelete);

        pbbar = (ProgressBar) findViewById(R.id.pbbar);
        pbbar.setVisibility(View.GONE);

        lstProduct = (ListView) findViewById(R.id.lstProducts);

        productCode = "";

        lstProduct.addHeaderView(getLayoutInflater().inflate(R.layout.header, null, false));

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddProducts addProducts = new AddProducts();
                addProducts.execute();
            }
        });

        ShowProducts showProducts = new ShowProducts();
        showProducts.execute();
    }

    public class ShowProducts extends AsyncTask<String, String, String> {
        String msg = "";

        @Override
        protected void onPreExecute() {
            pbbar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(String r) {
            pbbar.setVisibility(View.GONE);
            Toast.makeText(ProductActivity.this, r, Toast.LENGTH_SHORT).show();

            final Products products = new Products();

            String[] from = { products.getCode("Code"), products.getName("ProductName"), products.getPrice("Price") };
            int[] views = {R.id.lblProductCode, R.id.lblProductName, R.id.lblProductPrice};
            final SimpleAdapter ADA = new SimpleAdapter(ProductActivity.this, productList, R.layout.lsttemplate, from, views);
            lstProduct.setAdapter(ADA);

            ADA.notifyDataSetChanged();

            lstProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                                        int arg2, long arg3) {
                    HashMap<String, Object> obj = (HashMap<String, Object>) ADA
                            .getItem(arg2);

                    productCode = (String) obj.get(products.getCode("Code"));
                    String productName = (String) obj.get(products.getName("ProductName"));
                    String productPrice = (String) obj.get(products.getPrice("Price"));

                    edtProductCode.setText(productCode);
                    edtpProductName.setText(productName);
                }
            });
        }

        @Override
        protected String doInBackground(String... params) {

            try {
                Connection con = connectionClass.CONN();
                if (con == null) {
                    msg = "Error in connection with SQL server";
                } else {
                    String query = "SELECT ProductName, Code, Price FROM Products";
                    PreparedStatement ps = con.prepareStatement(query);
                    ResultSet rs = ps.executeQuery();

                    while (rs.next()) {
                        Products products = new Products();
                        Map<String, String> productList = new HashMap<String, String>();

                        String code = products.getCode(rs.getString("Code"));
                        String name = products.getName(rs.getString("ProductName"));
                        String price = products.getName(rs.getString("Price"));

                        productList.put("Code", code);
                        productList.put("ProductName", name);
                        productList.put("Price", price);

                        ProductActivity.this.productList.add(productList);
                    }
                    msg = "Success";
                }
            } catch (Exception ex) {
                msg = "Error retrieving data from table";
            }
            return msg;
        }
    }

    public class AddProducts extends AsyncTask<String, String, String> {
        String msg = "";
        Boolean isSuccess = false;

        String proname = edtpProductName.getText().toString();
        String procode = edtProductCode.getText().toString();
        String proprice = edtProductPrice.getText().toString();

        @Override
        protected void onPreExecute() {
            pbbar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(String r) {
            pbbar.setVisibility(View.GONE);
            Toast.makeText(ProductActivity.this, r, Toast.LENGTH_SHORT).show();

            if(isSuccess == true) {
                ShowProducts showProducts = new ShowProducts();
                showProducts.execute();
            }
        }

        @Override
        protected String doInBackground(String... params) {
            if (proname.trim().equals("")) {
                msg = "Please enter product name!";
            } else if (procode.trim().equals("")) {
                msg = "Please enter product code!";
            } else if (proprice.trim().equals("")) {
                msg = "Please enter product price!";
            } else {
                try {
                    Connection con = connectionClass.CONN();
                    if (con == null) {
                        msg = "Error in connection with SQL server";
                    } else {

                        String dates = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).format(Calendar.getInstance().getTime());
                        String query = "INSERT INTO Products (ProductName, Code, Price, Date) VALUES ('" + proname + "', '" + procode + "', '" + proprice + "', '" + dates + "')";

                        PreparedStatement ps = con.prepareStatement(query);
                        ps.executeUpdate();

                        msg = "Added Successfully";
                        isSuccess = true;
                    }
                } catch (Exception ex) {
                    isSuccess = false;
                    msg = "Exceptions";
                }
            }
            return msg;
        }
    }
}

最后是布局。有两种布局。 i)products.xml - 主要布局ii)lsttemplate.xml - 用于修改ListView

products.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#282828"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:padding="2dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/app_name_addproducts"
            android:layout_marginTop="7dp"
            android:typeface="sans"
            android:textSize="35sp"
            android:textColor="#ffffff"
            android:gravity="center" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff"
            android:textStyle="bold"
            android:background="#5d5d5d"
            android:padding="10dp"
            android:hint="@string/app_name_addproductName"
            android:textSize="20sp"
            android:id="@+id/edtProductName" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:hint="@string/app_name_addproductCode"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff"
            android:textStyle="bold"
            android:background="#5d5d5d"
            android:padding="10dp"
            android:inputType="textMultiLine"
            android:maxLines="3"
            android:minLines="2"
            android:layout_gravity="top|left"
            android:layout_marginTop="5dp"
            android:id="@+id/edtProductCode" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:hint="@string/app_name_addproductPrice"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff"
            android:textStyle="bold"
            android:background="#5d5d5d"
            android:padding="10dp"
            android:inputType="textMultiLine"
            android:maxLines="3"
            android:minLines="2"
            android:layout_gravity="top|left"
            android:layout_marginTop="5dp"
            android:id="@+id/edtProductPrice" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="3"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorDarkForest"
                android:layout_weight="1"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:layout_margin="2dp"
                android:padding="7dp"
                android:layout_marginTop="10dp"
                android:id="@+id/btnAdd"
                android:text="@string/app_name_addproduct" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorDarkForest"
                android:layout_weight="1"
                android:layout_margin="2dp"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:padding="7dp"
                android:layout_marginTop="10dp"
                android:id="@+id/btnUpdate"
                android:text="@string/app_name_updateproduct" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorDarkForest"
                android:layout_weight="1"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:layout_margin="2dp"
                android:padding="7dp"
                android:layout_marginTop="10dp"
                android:id="@+id/btnDelete"
                android:text="@string/app_name_deleteproduct" />
        </LinearLayout>

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:id="@+id/pbbar" />

        <ListView
            android:id="@+id/lstProducts"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="#808080"
            android:layout_marginTop="5dp"
            android:dividerHeight="1dp"
            android:padding="5dp">
        </ListView>
    </LinearLayout>
</RelativeLayout>

lsttemplate.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="5"
    android:padding="5dp"
    android:layout_marginTop="2dp"
    >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:layout_weight="1"
        android:textSize="15sp"
        android:id="@+id/lblProductCode"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:width="40dip"
        android:id="@+id/lblProductName"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:paddingLeft="25dp"
        android:id="@+id/lblProductPrice"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

只需在onPreExecute

中添加波纹管代码即可
try{
    productList.clear();
    lstProduct.removeAllViews();
}catch (Exception e){

}

您还可以将自定义适配器与ArrayAdapter一起使用