OnItemClickListener gets neither valid parent id nor view id under ListPopupWindow



I have implemented a pair of EditText's and both of them have associated ListPopopWindow's. The add Operation works fine, so, I write something on EditText Field and I press enter Taste of Virtual Keyboard and this value is saved and added to the ListPopWindow but what doesn't work so fine, is onItemClick Event. What this Method should do, is writting the value of the item on EditText Field but it doesn't do.


I have debugged with LogCat and see that the id's of parent and view of the typical signature of onItemClick :



public void onItemClick(AdapterView<?> parent, View view, int position, long id)


return -1 and I don't understand why, so that I have assigned right id's in my xml-layout-File and I attached right the onItemClickListener to the ListPopupWindow's.


Following I post my code.



public class MainActivity extends Activity implements OnClickListener, OnItemClickListener, OnEditorActionListener{


private EditText product_name;
private ArrayAdapter<String> productAdapter;
private ListPopupWindow productListPopupWindow;
private ArrayList<String> products= new ArrayList<String>();


private EditText device;
private ArrayAdapter<String> deviceAdapter;
private ArrayList<String> devices= new ArrayList<String>();
private ListPopupWindow deviceListPopupWindow;


private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();

initAdapters();
configureActionItem();

}


Rest of code for better legibility



private void initAdapters() {
// Init Product List
products.add("JD 000");
products.add("JD 001");
products.add("JD 002");
products.add("JD 003");
products.add("JD 004");

// Init Product Adapter
productListPopupWindow = new ListPopupWindow(MainActivity.this);
productAdapter = new ArrayAdapter(MainActivity.this,R.layout.product_list_item, products);
productListPopupWindow.setAdapter(productAdapter);

// ****************************
// Init Device List
devices.add("DEV000");
devices.add("DEV001");
devices.add("DEV002");
devices.add("DEV003");
devices.add("DEV004");

// Init Device Adapter
deviceListPopupWindow = new ListPopupWindow(MainActivity.this);
deviceAdapter = new ArrayAdapter(MainActivity.this,R.layout.device_list_item, devices);
deviceListPopupWindow.setAdapter(deviceAdapter);
}

private void configureActionItem() {
product_name = (EditText) findViewById(R.id.edit_text_product);

productListPopupWindow.setAnchorView(product_name);
productListPopupWindow.setWidth(300);
productListPopupWindow.setHeight(400);

productListPopupWindow.setModal(true);
productListPopupWindow.setOnItemClickListener( MainActivity.this); <- attached Listener

product_name.setOnEditorActionListener(MainActivity.this);
product_name.setOnClickListener(MainActivity.this);

// --------------------------------------------------------------
device = (EditText) findViewById(R.id.edit_text_device_implement);

deviceListPopupWindow.setAnchorView(device);
deviceListPopupWindow.setWidth(300);
deviceListPopupWindow.setHeight(400);

deviceListPopupWindow.setModal(true);
deviceListPopupWindow.setOnItemClickListener( MainActivity.this); <- attached Listener

device.setOnEditorActionListener(MainActivity.this);
device.setOnClickListener(MainActivity.this);
}

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

// It's the same: parent.getId() or view.getId(), both of them return -1
switch ( parent.getId()) {
case R.id.edit_text_product :
product_name.setText(products.get(position));
productListPopupWindow.dismiss();
break;
case R.id.edit_text_device_implement :
device.setText(devices.get(position));
deviceListPopupWindow.dismiss();
break;
}
}


@Override
public void onClick(View v) {
// If I click on EditText Field, then ListPopupWindow will be expanded and shown
switch ( v.getId()) {
case R.id.edit_text_product :
productListPopupWindow.show();
break;
case R.id.edit_text_device_implement :
deviceListPopupWindow.show();
break;
}
}

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event == null || event.getAction() == KeyEvent.ACTION_UP) {

switch ( v.getId()) {
case R.id.edit_text_product :
productAdapter.add(v.getText().toString());
v.setText("");
productListPopupWindow.show();
break;
case R.id.edit_text_device_implement :
deviceAdapter.add(v.getText().toString());
v.setText("");
deviceListPopupWindow.show();
break;
}


InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

}

return(true);
}


And last my xml's



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

<EditText
android:id="@+id/edit_text_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:imeOptions="actionGo"
android:inputType="text"
android:singleLine="true"
android:textSize="12dp" />

<EditText
android:id="@+id/edit_text_device_implement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:imeOptions="actionGo"
android:inputType="text"
android:singleLine="true"
android:textSize="12dp" />

</RelativeLayout>


und xml Layout of items



<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dp"
android:textSize="12dp"
android:textStyle="bold" />


Can anyone tell me what I do wrong?


I thank you in advance.


No comments:

Post a Comment