setOnItemClickListener在片段活动中不起作用

时间:2015-12-21 12:08:08

标签: android listview

我在点击ListView项目时尝试显示Toast。我在本网站上阅读了一些以前的帖子并尝试解决问题。但它并不适合我。我该如何解决?

以下是Fragment Activity的代码:

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.Toast;

public class SinFragment extends Fragment {

    private static SimpleCursorAdapter dataAdapter;
    private static String[] columns;
    private static int[] to;
    private static ListView listView;
    private static Cursor cursor;
    private static TestAdapter dbHelper;

    public SinFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Open Exiting Database
        dbHelper = new TestAdapter(getContext());
        dbHelper.createDatabase();
        dbHelper.open();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sin, container, false);
        //Get data from Database to ListView
        setListView(view);
        return view;
    }

    public void setListView(View view){

        cursor = dbHelper.getSiDic();

        columns = new String[] {
                "word",
                "definition",
        };

        to = new int[] {
                R.id.word,
                R.id.definition,
        };

        dataAdapter = new SimpleCursorAdapter(
                getContext(), R.layout.item_sin,
                cursor,
                columns,
                to,
                0);

        // Assign adapter to ListView
        listView = (ListView)view.findViewById(R.id.listview);
        listView.setAdapter(dataAdapter);

        final EditText search = (EditText)view.findViewById(R.id.search);
        //Clear text of EditText when click the clear button at right side of EditText
        search.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_LEFT = 0;
                final int DRAWABLE_TOP = 1;
                final int DRAWABLE_RIGHT = 2;
                final int DRAWABLE_BOTTOM = 3;

                if(event.getAction() == MotionEvent.ACTION_UP) {
                    if(event.getRawX() >= (search.getRight() - search.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                        search.setText("");

                        return true;
                    }
                }
                return false;
            }
        });
        //Search user's input form Database
        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                dataAdapter.getFilter().filter(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {
                return dbHelper.getSiDicByName(constraint.toString());
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);
                String word = cursor.getString(cursor.getColumnIndexOrThrow("word"));
                String definition = cursor.getString(cursor.getColumnIndexOrThrow("definition"));
                Log.v("Click Item", word + " is clicked");
                Toast.makeText(getContext(), word + " is clicked", Toast.LENGTH_SHORT).show();
            }
        });

    }

}

更新 片段xml的代码:

<LinearLayout 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"
    android:padding="10dp"
    android:orientation="vertical"
    tools:context="com.mecduino.dicmec.SinFragment">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search"
        android:padding="10dp"
        android:drawableLeft="@android:drawable/ic_menu_search"
        android:drawableRight="@android:drawable/ic_menu_close_clear_cancel"
        android:hint="Search..."
        android:background="@drawable/border"
        android:inputType="text" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="10dp"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listview" />

</LinearLayout>

item_sin.xml的代码

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="0.5">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/word"
            android:padding="5dp"
            android:textSize="12dp"
            android:capitalize="words" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:lineSpacingMultiplier="1.5"
            android:padding="5dp"
            android:id="@+id/definition"
            android:textSize="15dp" />

    </LinearLayout>

</LinearLayout>

border.xml的代码

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ffff" />
    <stroke android:width="1dip" android:color="#ce4416" />
    <corners
        android:bottomRightRadius="5dp"
        android:bottomLeftRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"/>
</shape>

0 个答案:

没有答案
相关问题