gpt4 book ai didi

android - OnItemClickListener 在 ListPopupWindow 下既没有获得有效的父 ID 也没有获得 View ID

转载 作者:搜寻专家 更新时间:2023-11-01 08:48:37 29 4
gpt4 key购买 nike

我已经实现了一对 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

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

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

先谢谢你了。

最佳答案

已解决。

我得到 -1 的原因是 view 上的

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

没有关联的id,所以,解决这个问题的方法如下(我只是发布了一个 list_item 布局,但我们需要为每个适配器使用一个 list_item 布局,它们的区别仅在于 id 以区分 onItemClick 方法的切换,因此,正确编写 self身份证)

<?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;
}
}

关于android - OnItemClickListener 在 ListPopupWindow 下既没有获得有效的父 ID 也没有获得 View ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25787168/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com