我已经实施了一对EditText
,并且他们都已关联ListPopopWindow
' s。 add
操作正常,因此,我在EditText
字段上写了一些内容,然后按enter
品尝虚拟键盘,此值将保存并添加到ListPopWindow
但是onItemClick
事件不是很好。
这个方法应该做的是在EditText
字段上写下项目的值,但它没有。
我已使用LogCat进行调试,并查看parent
典型签名的view
和onItemClick
的ID:
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
返回-1
并且我不明白为什么,所以我在我的xml-layout-File中分配了正确的ID并且我将onItemClickListener附加到ListPopupWindow
我可以在我的LogCat上显示view
的值,并且是正确的,唯一的问题与id
我发布了我的代码。
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();
}
其余代码以提高易读性
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);
}
最后我的xml
<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"
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项目布局
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dp"
android:textSize="12dp"
android:textStyle="bold" />
谁能告诉我我做错了什么?
我提前感谢你。
答案 0 :(得分:3)
<强>解决。强>
原因,为什么我得到-1,view
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
没有关联的id
,因此,解决此问题的方法如下
(我只发布了一个list_item布局,但是我们需要每个适配器的list_item布局,它们只是在id
区别,以区分切换onItemClick
方法,所以,写自己正确的id)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/id_you_want" <- What I added
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dp"
android:textSize="12dp"
android:textStyle="bold" />
然后我在onItemClick
@覆盖 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 ( view.getId()) { // What I replaced
case R.id.id_you_want_1: // What I replaced
product_name.setText(products.get(position));
productListPopupWindow.dismiss();
break;
case R.id.id_you_want_2: // What I replaced
device.setText(devices.get(position));
deviceListPopupWindow.dismiss();
break;
}
}