Listfragment和onclick

时间:2014-12-30 19:10:15

标签: android fragment android-listfragment

我的问题是如何设置单击我的列表中的项目,因为在列表片段中它需要具有特定的ID是android:list所以我不知道如何使用它。

我有这个布局:

<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" />

和这个列表片段:

    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);

        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;

    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)

    {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 2)
        {

            //String message=data.getStringExtra("MESSAGE");


            Intent intent=new Intent(getActivity(),qr.class);
            startActivity(intent);



        }





    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }
}

1 个答案:

答案 0 :(得分:0)

这样的事情可以解决问题(至少我认为这是你想要做的事情)

在onCreateView()

((ListView) rootView.findViewById(android.R.id.list)).
    setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
         //Do what you want when you click on an item
    }
}

因为我没有时间解释它,所以会发表评论,但看起来很草率所以认为答案会更好。