簡體   English   中英

在ListView項目中識別AutocompleteTextView

[英]Identify AutocompleteTextView in ListView Item

我有幾個AutocompleteTextView作為ListView中的項目; 這些項目是使用“自定義”適配器動態加載的。 當在AutoCompleteTextView中選擇一個項目時,我需要更新與AutoCompleteTextView在同一行的另一個TextView。 我在確定選擇項目的AutoCompleteTextView的位置時遇到麻煩。 任何指針將不勝感激:

我的活動如下所示,其中定義了Listview:

<ListView
            android:id="@+id/listview_orders"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

ListView項目在另一個布局文件中定義為:

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

    <AutoCompleteTextView
        android:id="@+id/product_code_row_item"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"

        android:ems="10" />

    <AutoCompleteTextView android:id="@+id/product_name_row_item"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        />

    <TextView android:id="@+id/product_size_row_item"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        />



</LinearLayout>

ListView適配器的設置如下:

ListView view = (ListView) findViewById(R.id.listview_orders);
        ListViewItemAdapter adapter = new ListViewItemAdapter(this, orderItems, prodCodeList, prodNameList);
        view.setAdapter(adapter);

定制適配器的getView方法實現如下:

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

        if (convertView == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            convertView = inflater.inflate(R.layout.order_list_row, null);
        }
        AutoCompleteTextView  productCode = (AutoCompleteTextView ) convertView.findViewById(R.id.product_code_row_item);
        AutoCompleteTextView productName = (AutoCompleteTextView) convertView.findViewById(R.id.product_name_row_item);

        TextView productSize = (TextView) convertView.findViewById(R.id.product_size_row_item);
        TextView mQty = (TextView) convertView.findViewById(R.id.product_mqty_row_item);




        OrderItem orderItem = (OrderItem)orderItemList.get(position);

        productCode.setText(orderItem.getProductCode());
        productCode.setTag(position);

        productName.setText(orderItem.getDescription());
        productName.setTag(position);

        productSize.setText(orderItem.getProductSize());



        //Setting up autocorrect adapter for product codes
        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (activity, android.R.layout.select_dialog_item, prodCodeList);
        //Getting the instance of AutoCompleteTextView

        productCode.setThreshold(1);//will start working from first character
        productCode.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
        productCode.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View arg1, int pos,
                                    long id) {
                Toast.makeText(OrderApplication.getCustomAppContext(), (CharSequence)parent.getItemAtPosition(pos), Toast.LENGTH_LONG).show();


                int itemPosition = (Integer) arg1.getTag();

                //loadOrderItem(newOrderItem, (String) parent.getItemAtPosition(pos), true);
            }
        });

        //Setting up autocorrect adapter for product names
        adapter = new ArrayAdapter<String>
                (activity, android.R.layout.select_dialog_item, prodNameList);
        //Getting the instance of AutoCompleteTextView

        productName.setThreshold(2);//will start working from first character
        productName.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView

        return convertView;
    }

您應該使用-

adapter.getItem(pos)

在您的onItemClick中

我找到了一種變通辦法來獲取ListView中的AutoCompleteTextView引用。

holder.productCodeView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus) {
                currentFocusView = v;
            }

        }
    });

    holder.productCodeView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {


            if (currentFocusView != null) {
                int iPosition = (Integer) currentFocusView.getTag();

            }


        }
    });

暫無
暫無

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

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