如何从对话框ListView中检索值...?

时间:2015-05-07 05:53:26

标签: android

我正在使用对话框显示列表...该列表包含Text ViewEdit Text个小部件以及一个(完成)Button

点击(完成)Button将从列表中检索包含Edit Text中值的行。

问题是滚动时行是否超出视图范围。单击Button时,我没有收到这些行。

doneButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        AddedItems = new ArrayList<productinfo>();
        try {
            for(int i = 0;i<allProducts.size();i++) {   
                View view = productListView.getChildAt(i);
                TextView product = (TextView) view.findViewById(R.id.product_textview);
                String pName = product.getText().toString();
                TextView p_code = (TextView)  view.findViewById(R.id.product_code);
                String pCode = p_code.getText().toString();
                TextView p_price = (TextView) view.findViewById(R.id.price_textview);
                String pPrice = p_price.getText().toString();
                EditText qty = (EditText) view.findViewById(R.id.quantity_edittext);
                String qtyVal = qty.getText().toString();

                if(qty.getText().toString().matches("")) {
                    //do nothing
                } else {
                    AddedItems.add(new productinfo(pCode, pName, "", pPrice, "",qtyVal));
                    addedProduct.removeAllViews();
                }
            }

这是适配器:

private List<productinfo> products;
    private List<productinfo> arrayList;
    private Context context;
    private String[] tmp;
    private int value;

    public ProductListAdapter(Context context, int resource,ArrayList<productinfo> products) {
        // TODO Auto-generated constructor stub
        super(context, resource, products);
        this.context=context;
        this.products=products;
        this.arrayList = new ArrayList<productinfo>();
        this.arrayList.addAll(products);
        tmp = new String[products.size()];
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return products.size();
    }

    @Override
    public productinfo getItem(int position) {
        // TODO Auto-generated method stub
        return products.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }


    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return products.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHoldr holder;

        if(convertView == null){

            holder = new ViewHoldr();

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.product_list_row_layout, null);

            holder.productCode = (TextView) convertView.findViewById(R.id.product_code);
            holder.productDescription = (TextView) convertView.findViewById(R.id.product_textview);
            holder.productPrice = (TextView) convertView.findViewById(R.id.price_textview);
            holder.quantity = (EditText) convertView.findViewById(R.id.quantity_edittext);

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHoldr) convertView.getTag();          
        }

        holder.ref = position;

        final productinfo product = products.get(position);

         //int colorPos = position % colors.length;
           // convertView.setBackgroundColor(colors[colorPos]);

        if(product !=null){


            if(holder.productCode!=null)
                holder.productCode.setText(product.getP_CODE());

            if(holder.productDescription!=null)
                holder.productDescription.setText(product.getP_NAME());

            if(holder.productPrice!=null)
                holder.productPrice.setText(product.getMRP());

            //holder.quantity.setText(product.getP_qty());

            holder.quantity.setText(tmp[position]);

            holder.quantity.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                    tmp[holder.ref] = s.toString(); 
                }
            });


               //check for odd or even to set alternate colors to the row background
               if(position % 2 == 0){  
                convertView.setBackgroundColor(Color.parseColor("#6897bb"));
               }
               else {
                convertView.setBackgroundColor(Color.parseColor("#cbd3db"));
               }
        }

        return convertView;
    }

    private class ViewHoldr
    {
        TextView productCode;
        TextView productDescription;
        TextView productPrice;
        EditText quantity;
        int ref;
    }

这是行的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="10"
    >

       <TextView
        android:id="@+id/product_code"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2.5"
        android:gravity="top|left"
        android:textColor="#ffffff"
        />

    <TextView
        android:id="@+id/product_textview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4.3"
        android:text="jshadb"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/price_textview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.2"
        android:text="464.89"
        android:textColor="#ffffff" />

    <EditText 
        android:id="@+id/quantity_edittext"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:inputType="number"/>

</LinearLayout>

这是我的活动中用于显示行的布局:

<LinearLayout 
        android:id="@+id/addedProduct"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/header_layout"
        android:orientation="vertical"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        android:showDividers="middle"
        android:divider="@drawable/vertical_divider" 
        android:dividerPadding="5dp"      
        >

    </LinearLayout>

这是对话:

<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"
 >
     <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/header_layout"
            android:layout_above="@id/done_button"
            android:id="@+id/product_listview"
             />


    <Button
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:text="Done"
            android:layout_alignParentBottom="true"
            android:id="@+id/done_button" />
<RelativeLayout />

1 个答案:

答案 0 :(得分:1)

您无法从ListView获取所有子行视图,因为ListView仅包含可见行的视图。做你想做的事的正确方法是在适配器的数据中存储任何数据并从那里检索它。

所以,你应该使用它:

public View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}

而不只是来自getChild

ListView