簡體   English   中英

動態插入列表項到android中的listview

[英]Dynamically inserting list items to listview in android

朋友我是Android的新手並且自己學習。 我正在為學習創建發票管理器應用程序。 這里的產品由管理員添加,工作正常。 用戶只有創建發票的權限。 當用戶進入create_invoice活動時,他有一組框架布局,其中一個用於添加項目。 當用戶按下框架布局時,他會看到另一個活動,他可以在列表視圖中找到所有列表項以及管理員已添加的產品價格。 現在,當用戶按下某個項目時,他再次返回到create_invoice活動,並出現一個警告框,詢問所需的數量。 當用戶輸入數量並單擊“確定”按鈕時,對於第一個,列表項將根據需要正確顯示。 但是當我添加第二項時,第一項被替換。 所以現在我的問題是如何解決這個問題..伙計們請幫助我。 你發現的代碼可能非常愚蠢,但我還在學習。 提前致謝。

Create_invoice活動

 //data from activity invoice add_item
  product_name = intent.getStringExtra("product_name");
  product_price = intent.getDoubleExtra("product_price",0);
  //product_qty = intent.getIntExtra("product_qty",0);
  product_code = intent.getIntExtra("product_code",0);

  if(product_name!= null && product_price!= 0 && product_code!= 0)
  {
    try {
        builder = new AlertDialog.Builder(this);
        builder.setTitle("Product Qty");
        layoutInflater = LayoutInflater.from(Create_invoice.this);
      view = layoutInflater.inflate(R.layout.dialoglayout_invoice,null);
        builder.setView(view);

 builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
  EditText etxt_dialog_qty=(EditText)view.findViewById(R.id. 
   etxt_dialog_qty);

                int qty = Integer. parseInt (etxt_dialog_qty.getText(). 
             toString().trim());

                invoice_product_list products = new invoice_product_list 
  (product_name, product_price, qty, product_code);

         //arraylist
        ArrayList<invoice_product_list> productList = new ArrayList<>();

         //customAdapter
     customAdapterInvoice = new custom_adapter_invoice 
   (Create_invoice.this, productList);

                customAdapterInvoice.add(products);

                customAdapterInvoice.notifyDataSetChanged();

                //listview in create_invoice activity
                listView_additem = (ListView) 
 findViewById(R.id.listview_additem);

                listView_additem.setAdapter(customAdapterInvoice);

                alertDialog.dismiss();
            }
        });

        builder.setNegativeButton("Cancel", new 
      DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
            }
        });

        alertDialog = builder.create();
        alertDialog.show();

    }
    catch (Exception e)
    {
        System.out.print(e);
    }
}

customAdapter

  public class custom_adapter_invoice extends ArrayAdapter 
   <invoice_product_list> {

   public custom_adapter_invoice(Context context,   
  ArrayList<invoice_product_list> product_details) {
   super(context, R.layout.custom_row_invoice_item, product_details);
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View view =   
layoutInflater.inflate(R.layout.custom_row_invoice_item,parent,false);

invoice_product_list products = getItem(position);

TextView txt_product_name = 
(TextView)view.findViewById(R.id.txt_product_name);

TextView txt_product_price = 
 (TextView)view.findViewById(R.id.txt_product_price);

  TextView txt_product_qty = 
 (TextView)view.findViewById(R.id.txt_product_qty);

  TextView txt_product_code = 
  (TextView)view.findViewById(R.id.txt_product_code);

  txt_product_name.setText(products.getProduct_name());

txt_product_price.setText(String.valueOf(products.getProduct_price()));

txt_product_qty.setText(String.valueOf(products.getProduct_qty()));

txt_product_code.setText(String.valueOf(products.getProduct_code()));

return view;

}

創建發票活動

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v4.widget.DrawerLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
android:layout_height="match_parent"
 app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.tournonstop.m.invoicemanager.Create_invoice"
tools:showIn="@layout/activity_create_invoice">

  <LinearLayout
  android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/darker_gray">

<include layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="85dp"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@android:color/white"
    android:id="@+id/invoice_frame_company">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/company_name"
        android:id="@+id/txt_company_name"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/invoice_date"
        android:id="@+id/txt_invoice_date"
        android:layout_gravity="right"
        android:layout_marginRight="15dp"
        android:layout_marginTop="15dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/invoice_no"
        android:id="@+id/textView3"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="45dp"/>

</FrameLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@android:color/white"
    android:id="@+id/invoice_frame_client"
    android:clickable="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/to"
        android:id="@+id/txt_to"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/txt_client_address"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:hint="@string/client_hint"/>

</FrameLayout>

 ----listview to add items-----
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="210dp"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@android:color/white"
    android:id="@+id/invoice_frame_add_item"
    android:clickable="true">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:id="@+id/listview_additem"
        android:divider="#040404" />
</FrameLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_marginTop="2dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@android:color/white"
    android:id="@+id/invoice_frame_sub_total">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/total_label"
        android:id="@+id/txt_sub_total_label"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:textStyle="bold"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/total"
        android:id="@+id/txt_sub_total"
        android:layout_gravity="right"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:textStyle="bold"
        android:hint="@string/total_hint" />

</FrameLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="40dp"
    android:text="@string/invoice_btn"
    android:textStyle="bold"
    android:background="@color/colorPrimaryDark"
    android:textColor="@android:color/white"
    android:clickable="true"
    android:id="@+id/btn_invoice_save" />

   </LinearLayout>

  < /android.support.v4.widget.DrawerLayout>

發票產品清單(吸氣劑和二傳手)

   package com.tournonstop.m.invoicemanager;

 public class invoice_product_list {

 private String product_name;
 private double product_price;
private int product_qty;
 private int product_code;

  public invoice_product_list(){

 }

  public invoice_product_list(String product_name,double 
  product_price,int product_qty,int product_code){
  this.product_name = product_name;
  this.product_price = product_price;
  this.product_qty = product_qty;
  this.product_code = product_code;
  }

 public String getProduct_name() {
  return product_name;
 }

   public void setProduct_name(String product_name) {
  this.product_name = product_name;
  }

  public int getProduct_code() {
  return product_code;
 }

  public void setProduct_code(int product_code) {
   this.product_code = product_code;
 }

  public double getProduct_price() {
  return product_price;
 }

  public void setProduct_price(double product_price) {
  this.product_price = product_price;
   }

  public int getProduct_qty() {
  return product_qty;
  }

  public void setProduct_qty(int product_qty) {
  this.product_qty = product_qty;
   }
 }

將項目添加到您在適配器中使用的列表中(在驗證適配器不為null之后)在適配器對象上調用方法notifyDataSetChanged()。

create_invoice活動

 //arraylist
    ArrayList<invoice_product_list> productList = new ArrayList<>();

     //customAdapter
 customAdapterInvoice = new custom_adapter_invoice 
(Create_invoice.this, productList);

            customAdapterInvoice.addProduct(products)

定制適配器

public class custom_adapter_invoice extends ArrayAdapter <invoice_product_list> 
{
ArrayList<invoice_product_list> productList = new ArrayList<>();
public custom_adapter_invoice(Context context,   
ArrayList<invoice_product_list> product_details) {
super(context, R.layout.custom_row_invoice_item, product_details);
this.productList =product_details
}

public void addProduct(Product products
productList.add(products);
notifyDataSetChanged();
}


 ......
}

不,它不會改變任何東西。 如果你想要最新的工作方法,那么我更喜歡RecyclerView自定義列表適配器。 這個鏈接非常適合recyclerview

recyclerview演示系列鏈接

暫無
暫無

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

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