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

时间:2021-01-13 13:43:49

I'm showing a list using dialog...the list contains Text View and Edit Text widgets and a (Done)Button.

我正在使用对话框显示列表...列表包含文本视图和编辑文本小部件以及(完成)按钮。

Clicking on (Done)Button will retrieve those rows from the list which contain values in Edit Text.

单击(完成)按钮将从列表中检索包含编辑文本中的值的那些行。

The problem is if rows goes out of the view when scrolling. I'm not getting those rows when the Button is clicked.

问题是滚动时行是否超出视图。点击按钮时,我没有得到那些行。

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();
                }
            }

And this is the adapter:

这是适配器:

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;
    }

This is the layout for row:

这是行的布局:

<?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>

And this is the layout used in my activity to display rows:

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

<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>

And this is for dialog:

这是对话:

<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 个解决方案

#1


You can't get all the child row views from a ListView simply because a ListView holds only the views for the visible rows. The correct way to do what you want is to store whatever data in the adapter's data and retrieve it from there.

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

So, you should use this :

所以,你应该使用这个:

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);
    }
}

Instead of just getChild from ListView

而不仅仅是ListView中的getChild

#1


You can't get all the child row views from a ListView simply because a ListView holds only the views for the visible rows. The correct way to do what you want is to store whatever data in the adapter's data and retrieve it from there.

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

So, you should use this :

所以,你应该使用这个:

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);
    }
}

Instead of just getChild from ListView

而不仅仅是ListView中的getChild