Android twoWayGridView onItemClickListenter not working

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 00:32:54

问题


I've implemented the two-way-gridview library by adding libs and two-way-gridview to my Android project.

The XML:

<com.jess.ui.TwoWayGridView
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#000000"
android:id="@+id/listView1"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
app:cacheColorHint="#E8E8E8"
app:columnWidth="80dp"
app:rowHeight="80dp"
app:numColumns="3"
app:numRows="auto_fit"
app:verticalSpacing="16dp"
app:horizontalSpacing="16dp"
app:stretchMode="spacingWidthUniform"
app:scrollDirectionPortrait="horizontal"
app:scrollDirectionLandscape="horizontal"
app:gravity="left" />

The Java code:

//set up adapter
CustomCursorAdapter dataAdapter = new CustomCursorAdapter(context, cursor);
TwoWayGridView gridView = (TwoWayGridView) findViewById(R.id.listView1);

//assign adapter to ListView
gridView.setAdapter(dataAdapter);

This happens:

  1. Grid can scroll horizontally - yay!
  2. All items are TextViews with the same element (the first item) - not yay.
  3. No item in the grid is clickable - the setOnItemClickListener is not compatible - not yay.

Eclipse error:

The method setOnItemClickListener(TwoWayAdapterView.OnItemClickListener) in the type TwoWayAdapterView is not applicable for the arguments (new AdapterView.OnItemClickListener(){})

on this piece of code:

gridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
        //get the cursor, positioned to the corresponding row in the result set
        Cursor cursor = (Cursor) listView.getItemAtPosition(position);

        //get the media file from this row in the database
        String fileName = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.FIELD_RESOURCE));

        String suffix = ".jpg";
        if(fileName.equalsIgnoreCase("null")){
            //Text item so do stuff for that

            }else if(fileName.endsWith(suffix)){
                    //Image item so do stuff for that

                }else {
                    //Video item so do stuff for that

                }
            }
        });

Should I write a custom onItemClickListener for the two-way-gridview?


回答1:


You should probably just change :

gridView.setOnItemClickListener(new OnItemClickListener() {}

by

gridView.setOnItemClickListener(new TwoWayAdapterView.OnItemClickListener() {}


来源:https://stackoverflow.com/questions/18076104/android-twowaygridview-onitemclicklistenter-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!