【问题标题】:OnItemClickListener gets neither valid parent id nor view id under ListPopupWindowOnItemClickListener 在 ListPopupWindow 下既没有得到有效的父 id,也没有得到视图 id
【发布时间】:2014-09-11 12:12:58
【问题描述】:

我已经实现了一对EditText,它们都关联了ListPopopWindowadd 操作工作正常,所以,我在EditText 字段上写了一些东西,然后按enter 虚拟键盘的味道,这个值被保存并添加到ListPopWindow,但是不能正常工作的是onItemClick 事件。 这个方法应该做的是将项目的值写入EditText 字段,但它没有做。

我已经用 LogCat 进行了调试,发现 onItemClick 的典型签名的 parentview 的 id :

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

return -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's

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

谁能告诉我我做错了什么?

提前谢谢你。

【问题讨论】:

    标签: android xml listview view onitemclicklistener


    【解决方案1】:

    已解决。

    我得到 -1 的原因是,view

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

    没有关联id,所以,解决这个问题的方法如下 (我只是发布了一个 list_item 布局,但我们需要为每个适配器设置一个 list_item 布局,它们的不同之处仅在于 id 以区分 onItemClick 的切换方法,因此,请正确写入 self 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中替换了3行

    @覆盖 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;
    }   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多