У меня есть пара AutocompleteTextView как элементов в ListView; эти элементы динамически загружаются с помощью специального адаптера. Когда элемент выбран в AutoCompleteTextView, мне нужно обновить другой TextView, который находится в той же строке, что и AutoCompleteTextView. Мне не удается определить позицию 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;
}
Это выбрасывает исключение ...
Не могли бы вы обновить свой код? Также где и какое исключение происходит?
Привет @I_A_Mok, Спасибо, что отметились. Вот фрагмент кода: Toast.makeText (OrderApplication.getCustomAppContext (), (CharSequence) parent.getItemAtPosition (pos), Toast.LENGTH_LONG) .show (); Объект temp = parent.getParent (); LinearLayout itemRoot = (LinearLayout) temp; AutoCompleteTextView productName = (AutoCompleteTextView) itemRoot.findViewById (R.id.product_name_row_item); temp имеет тип android.widget.PopupWindow $ PopupBackgroundView. Выдает исключение при приведении типа в LinearLayout




вы должны использовать -
adapter.getItem(pos)
внутри вашего onItemClick
Это приведет к появлению ссылки на элемент в раскрывающемся списке автокоррекции. Я ищу ссылку на AutoCompleteTextView
Я нашел обходной путь, чтобы получить ссылку на 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();
}
}
});
Попробуйте:
LinearLayout itemRoot = (LinearLayout) parent.getParent(); AutoCompleteTextView productName = (AutoCompleteTextView) itemRoot.findViewById(R.id.product_name_row_item);внутри вашего onItemClick () для AutoCompleteTextView [productCode] .. Надеюсь, что это поможет!