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

时间:2018-09-27 12:21:15

标签: android listview custom-adapter

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

这是我的主要活动:

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_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        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_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="1">

                <TextView
                    android:id="@+id/field_name_textview"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    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_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:orientation="horizontal"
                    >
                    <ImageButton
                        android:id="@+id/direction"
                        android:layout_width="50sp"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_direction"
                        android:scaleType="fitCenter"
                        android:background="@color/translucent_clear_bg"
                        />

                    <ImageButton
                        android:id="@+id/btn_branch_phone"
                        android:layout_width="50sp"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_call_phone"
                        android:scaleType="fitCenter"
                        android:background="@color/translucent_clear_bg"
                        />
                </LinearLayout>
            </RelativeLayout>

            <View
                android:id="@+id/seperator"
                android:layout_width="match_parent"
                android:layout_height="2dp"
                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_width="match_parent"
                android:layout_height="wrap_content"
                android:text="test"/>



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

</LinearLayout>

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

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

2 个答案:

答案 0 :(得分:0)

在您的代码中添加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;
}  }

答案 1 :(得分:0)

尝试在您的自定义适配器中使用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"