onItemClick和Toast不在listfragment中工作

时间:2014-12-31 07:20:05

标签: android fragment

我试着在我的onitemclick方法中做一个简单的Toast,但没有发生任何事情,当我按下列表中的项目时没有任何错误

我的listfragment:

    public class F1_fr extends ListFragment {
   View rootview;
    TextView textView1;
    ArrayAdapter<String> aa;
    ArrayList<String> arrayList = new ArrayList<String>();
    SQLiteDatabase db;
    ListView listView;

    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootview=inflater.inflate(R.layout.f1_lay,container,false);
        textView1=(TextView)rootview.findViewById(R.id.textView1);
        db = getActivity().openOrCreateDatabase("testDB2", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS test2(mac VARCHAR,mdp VARCHAR,obj VARCHAR);");

        aa = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1,  arrayList);
        setListAdapter(aa);


        ((ListView) rootview.findViewById(android.R.id.list)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(),"lol",Toast.LENGTH_LONG).show();
            }
        });


                    Cursor cursor = db.rawQuery("SELECT * FROM test2", null);
                    //  Toast.makeText(myContext, ""+cursor.getCount(), Toast.LENGTH_LONG).show();
                    if(cursor.moveToFirst())

                    {
                        do {
                            arrayList.add(cursor.getString(2));


                        } while (cursor.moveToNext());
                    }


                    rootview.findViewById(R.id.semi_transparent).

                    setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick (View v){

                            Intent intent = new Intent(getActivity(), ajout.class);
                            startActivityForResult(intent, 2);

                        }

                    }

                    );
                    return rootview;

                }

和我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"
android:layout_height="match_parent">



<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@id/android:list"

    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true" />

<TextView
    android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textSize="30sp"
    android:text="" />


<com.getbase.floatingactionbutton.AddFloatingActionButton
    android:id="@+id/semi_transparent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    fab:fab_plusIconColor="@color/primaryColorDark"
    fab:fab_colorNormal="@color/primaryColor"
    fab:fab_colorPressed="@color/AccentColor"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

所以我只是请求你的帮助,因为我绝对没有任何错误在logcat中没有任何事情发生了什么

3 个答案:

答案 0 :(得分:1)

您已直接ListFragment

实施了@Override onListItemClick(...)
 @Override
public void onListItemClick(ListView l, View v, int position, long id) {

    // do something
    Toast.makeText(getActivity(), "Lol", Toast.LENGTH_SHORT).show();
}

查看this

答案 1 :(得分:0)

我不确定但是尝试在RelativeLayout中添加以下内容。

android:descendantFocusability="blocksDescendants"

希望它能奏效。!!!

答案 2 :(得分:0)

你必须覆盖listFragment的onListItemClick(),你不要调用listview.OnItemClick();