2017-07-04 105 views
0

我刚刚开始专门为一个项目学习android编程,并陷入一个非常基本的东西,虽然这不应该。问题是我正在使用Sql数据库在Android ListView中显示产品详细信息。这工作正常。我再次使用按钮点击添加产品数据。当我添加产品时,ListView重复。像新添加的产品一起,以前的产品细节得到了ListView重复两次,如下图所示:数据在Android ListView中重复

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

添加按钮,我只是叫ShowProducts方法刷新ListView如下:

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

我不知道为什么它的发生。调试但无法弄清楚。即使使用SimpleAdapternotifyDataSetChanged刷新方法,但没有奏效。下面是表的结构:

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 
) 

在项目文件夹,应用程序的\ src \主\ 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; 
     } 
    } 
} 

最后是布局。有两种布局。 ⅰ)products.xml - 主布局ⅱ)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> 
+0

使用此行的航向的** **的ListView - 'lstProduct.addHeaderView(getLayoutInflater()膨胀(R.layout.header,空,假)) ;'。后来取消了。 –

回答

1

只需将波纹管代码添加到预执行

try{ 
    productList.clear(); 
    lstProduct.removeAllViews(); 
}catch (Exception e){ 

} 

您也可以使用自定义适配器ArrayAdapter

+0

此行是否包含在'AddProducts'和'ShowProducts'的onExecute方法中?非常感谢@Enamul Haque的建议。我会试着让你知道。 –

+0

完美工作。非常感谢@Enamul Haque。 –