Listview自定义适配器无法选择项目

Posted

技术标签:

【中文标题】Listview自定义适配器无法选择项目【英文标题】:Listview custom adapter cant select an item 【发布时间】:2019-03-03 08:43:43 【问题描述】:

我尝试了我在 *** 中找到的所有解决方案,但似乎它们都不起作用。

这是我的主要活动:

public class MerchantLocatorActivity extends AppCompatActivity implements OnMapReadyCallback 


    public void init()
        merchantLocatorResponseObject = new MerchantLocatorResponse();
        merchantLocatorResponseObject.setTitle("Spherical");
        merchantLocatorResponseObject.setAddress("8007 Pioneer St, Kapitolyo, Mandaluyong, 1550 Metro Manila");
        merchantLocatorResponseObject.setLatitude( 14.573249);
        merchantLocatorResponseObject.setLongitude(121.057022);
        merchantLocatorObjectArray.add(merchantLocatorResponseObject);
        merchantLocatorResponseObject = new MerchantLocatorResponse();
        merchantLocatorResponseObject.setTitle("Globe");
        merchantLocatorResponseObject.setAddress("SCT, 584 Shaw Blvd, Mandaluyong, 1552 Metro Manila");
        merchantLocatorResponseObject.setLatitude(14.585095);
        merchantLocatorResponseObject.setLongitude(121.048893);
        merchantLocatorObjectArray.add(merchantLocatorResponseObject);
        merchantLocatorResponseObject = new MerchantLocatorResponse();
        merchantLocatorResponseObject.setTitle("Sparndium");
        merchantLocatorResponseObject.setAddress("Xavier, San Juan, 1502 Metro Manila");
        merchantLocatorResponseObject.setLatitude(14.601918);
        merchantLocatorResponseObject.setLongitude(121.042169);
        merchantLocatorObjectArray.add(merchantLocatorResponseObject);
        addMarker();
    


    @OnClick(R.id.fab)
    public void showAccToDialog() 
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        View alertView = LayoutInflater.from(this).inflate(R.layout.dialog_biller, null);
        alertDialogBuilder.setView(alertView);
        final AlertDialog dialog = alertDialogBuilder.create();
        dialog.show();
        final ListView listViewBillers = (ListView) dialog.findViewById(R.id.biller_institutions_listview);
        if (listViewBillers != null) 
            MerchantLocatorAdapter adapter = new MerchantLocatorAdapter(
                    this, R.layout.merchant_locator_adapter,  merchantLocatorObjectArray);
            listViewBillers.setAdapter(adapter);
            listViewBillers.setOnItemClickListener(new AdapterView.OnItemClickListener() 
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
                    geoLocate(merchantLocatorObjectArray,position);
                    DebugUtils.log("TESTTESTACTIVITYZXC");
                    DebugUtils.showToast(MerchantLocatorActivity.this,"HAHAHAH");
                    dialog.dismiss();
                
            );

            final EditText mSearchedittext = (EditText) dialog.findViewById(R.id.search_edittext);
            mSearchedittext.addTextChangedListener(new TextWatcher() 
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) 

                

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) 
                    final ArrayList<MerchantLocatorResponse> searchResultObject = new ArrayList<>();
                    searchResultObject.clear();
                    for (int hay = 0; hay <= merchantLocatorObjectArray.size() - 1; hay++) 
                        if ( merchantLocatorObjectArray.get(hay).getTitle().toLowerCase().contains(charSequence)) 
                            searchResultObject.add( merchantLocatorObjectArray.get(hay));
                        
                    

                    MerchantLocatorAdapter adapter = new MerchantLocatorAdapter(
                            MerchantLocatorActivity.this, R.layout.merchant_locator_adapter,  searchResultObject);
                    listViewBillers.setAdapter(adapter);
                    listViewBillers.setOnItemClickListener(new AdapterView.OnItemClickListener() 
                        @Override
                        public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) 
                            geoLocate(searchResultObject,position);
                            dialog.dismiss();
                        
                    );
                

                @Override
                public void afterTextChanged(Editable editable) 

                
            );
        
    



我删除了代码的某些部分,因为我认为没有必要包含,但如果有某些部分我需要澄清,请告诉我。

目前在我的主要活动中,我正在调用一个包含列表视图的对话框,并且在我的列表视图中我有项目。

我的问题是我无法选择我的任何项目,即使我有我的 setOnitemclick 监听器。

这是我的适配器:

public class MerchantLocatorAdapter  extends BaseAdapter 

    private int resourceLayout;
    private Context mContext;
    ArrayList<MerchantLocatorResponse> merchantLocatorarray = new ArrayList<>();

    public MerchantLocatorAdapter(Context context, int resource, ArrayList<MerchantLocatorResponse> merchantLocatorResponsesobjectArray) 
        this.resourceLayout = resource;
        this.mContext = context;
        this.merchantLocatorarray = merchantLocatorResponsesobjectArray;
    

    @Override
    public int getCount() 
        return merchantLocatorarray.size();
    

    @Override
    public Object getItem(int position) 
        return null;
    

    @Override
    public long getItemId(int position) 
        return 0;
    

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
        if (convertView == null) 
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(resourceLayout, parent, false);
        

        TextView tt1 = (TextView) convertView.findViewById(R.id.field_name_textview);
        TextView tt2 = (TextView) convertView.findViewById(R.id.field_value_textview);
        ImageButton direction = (ImageButton) convertView.findViewById(R.id.direction);
        tt1.setText(merchantLocatorarray.get(position).getTitle());
        tt2.setText(merchantLocatorarray.get(position).getAddress());



        return convertView;
    

这是我的适配器布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_
    android:layout_>

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_
        android:layout_
        android:layout_gravity="center"
        android:layout_margin="8dp"
        android:elevation="3dp">

        <LinearLayout
            android:id="@+id/card_overflow"
            android:focusable="true"
            android:clickable="true"
            android:background="#fff"
            android:paddingLeft="16dp"
            android:paddingRight="0dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:layout_
            android:layout_
            android:orientation="vertical">

            <RelativeLayout
                android:layout_
                android:layout_
                android:weightSum="1">

                <TextView
                    android:id="@+id/field_name_textview"
                    android:layout_
                    android:layout_
                    android:layout_marginBottom="5dp"
                    android:layout_toLeftOf="@+id/branch_btns"
                    android:layout_alignParentLeft="true"
                    android:textSize="17sp"
                    android:textStyle="bold"
                    android:textColor="@color/edittext_text"
                    android:text="test"/>

                <LinearLayout
                    android:id="@+id/branch_btns"
                    android:layout_
                    android:layout_
                    android:layout_alignParentRight="true"
                    android:orientation="horizontal"
                    >
                    <ImageButton
                        android:id="@+id/direction"
                        android:layout_
                        android:layout_
                        android:src="@drawable/ic_direction"
                        android:scaleType="fitCenter"
                        android:background="@color/translucent_clear_bg"
                        />

                    <ImageButton
                        android:id="@+id/btn_branch_phone"
                        android:layout_
                        android:layout_
                        android:src="@drawable/ic_call_phone"
                        android:scaleType="fitCenter"
                        android:background="@color/translucent_clear_bg"
                        />
                </LinearLayout>
            </RelativeLayout>

            <View
                android:id="@+id/seperator"
                android:layout_
                android:layout_
                android:layout_marginBottom="5dp"
                android:background="@android:color/darker_gray"
                android:visibility="gone"
                android:layout_marginTop="2dp"/>
            <TextView
                android:id="@+id/field_value_textview"
                android:textSize="14sp"
                android:textColor="@color/edittext_tint"
                android:layout_marginTop="5dp"
                android:layout_
                android:layout_
                android:text="test"/>



        </LinearLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

我尝试了我在 *** 中找到的所有解决方案,但我仍然无法单击我的项目。所以请不要将此标记为重复。

如果代码中有任何需要澄清的部分,请发表评论,我会尽快回复。谢谢。

【问题讨论】:

查看答案 您的项目布局中有 ImageButton 通常会阻止单击事件。尝试将android:descendantFocusability="blocksDescendants 添加到项目视图的根布局中。希望有帮助! @I_A_Mok 会让我的 imageButton 无法点击吗?抱歉,我不知道descendantFocusability。会查查的。 找到了问题的根源,当我尝试点击卡片视图之外时,我可以点击该项目。但是当我尝试点击卡片视图本身时,它不会工作。 【参考方案1】:

尝试在您的自定义适配器中使用Observable:

// Define 
private final PublishSubject<MerchantLocatorResponse> onItemClick = PublishSubject.create();

// Create the observable method
public Observable<ConversationMessage> getObservable()
    return onItemClick;


// Set the onClickListener into getView()
convertView.setOnClickListener(new View.OnClickListener() 
    @Override
    public void onClick(View v) 
        onItemClick.onNext(merchantLocatorarray.get(position));
    
);

然后,在你的主要活动中听它并处理点击:

@OnClick(R.id.fab)
public void showAccToDialog() 
    // bla bla bla
    listViewBillers.setAdapter(adapter);
    listViewBillers.getObservable().subscribe(geoLocate);
    // bla bla bla



Consumer<MerchantLocatorResponse> geoLocate = new Consumer<MerchantLocatorResponse>() 
    @Override
    public void accept(MerchantLocatorResponse mlr) 
        // Code after click event
    
;

在你的 gradle 中添加这些库:

implementation "io.reactivex.rxjava2:rxjava:2.1.5"
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"

【讨论】:

setOnclickListener 本身不工作。我无法单击我的视图。 :( 尝试将父布局上的android:clicable="true"android:focusable="true" 设置为resourceLayout【参考方案2】:

在您的代码中添加convertView.setOnclickListener()。在您的适配器中尝试以下代码

  @Override
public View getView(final int position, View convertView, ViewGroup parent) 
    if (convertView == null) 
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(resourceLayout, parent, false);
    

    TextView tt1 = (TextView) convertView.findViewById(R.id.field_name_textview);
    TextView tt2 = (TextView) convertView.findViewById(R.id.field_value_textview);
    ImageButton direction = (ImageButton) convertView.findViewById(R.id.direction);
    tt1.setText(merchantLocatorarray.get(position).getTitle());
    tt2.setText(merchantLocatorarray.get(position).getAddress());

   convertView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
               Toast.makeText(context, String.valueOf(position), Toast.LENGTH_SHORT).show();
            
        );

    return convertView;
  

【讨论】:

@***lawrence 请尝试调试

以上是关于Listview自定义适配器无法选择项目的主要内容,如果未能解决你的问题,请参考以下文章

在listview中的addTextChangedListener中使用自定义列表适配器

带有复选框和自定义适配器的 ListView,片段无法正常工作

Android - 通过 getView 函数在自定义 listView 适配器内设置 ImageView 的源无法正常工作

listview 项目未在自定义适配器的 getview 方法中显示分配的值

ListView自定义适配器,包括库中的图像

使用自定义适配器自定义 Android ListView