在片段视图中获取onclick for list按钮

时间:2016-04-08 06:02:54

标签: android xml listview

我有一个带有几个按钮的列表视图,其中button.text属性使用sqlite数据库中的多个子名称填充。

当点击孩子的按钮时,我需要检索特定分配给每个孩子的任务(可能使用按钮的文本属性(用子名称填充))。

我有3个我正在使用的java文件(ChildDatabase,ChildListAdapter,ChildList)和2个xml文件(fragment_child_list_layout,fragment_child_list)。

在哪里可以为列表视图中的按钮设置onClick事件。如果只有一个按钮被识别,我如何区分每个按钮?

这是我的ChildDatabase文件

package com.example.victor.kidsrewards;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Victor on 4/3/2016.
 */
public class ChildDataBase {
    private SQLiteDatabase db;
    private ChildSQLiteHelper childSQLiteHelper;
    String _parentName = Globals._username;

    public ChildDataBase(Context context) {
        childSQLiteHelper = new ChildSQLiteHelper(context);
        db = childSQLiteHelper.getWritableDatabase();
    }

    public void Close() {
        db.close();
    }

    public void createChild(String childNameText, String nickNameText, String genderText, int ageText) {

        String query = "select * from child";
        Cursor cursor = db.rawQuery(query, null);
        int count = cursor.getCount();

        ContentValues contentValues = new ContentValues();
        contentValues.put("id", count);
        contentValues.put("childname", childNameText);
        contentValues.put("nickname", nickNameText);
        contentValues.put("age", ageText);
        contentValues.put("gender", genderText);
        contentValues.put("parent", _parentName);

        //insert
        db.insert("child", null, contentValues);

    }

    public List<ChildUser> getChildren() {
        List<ChildUser> childList = new ArrayList<ChildUser>();

        //select columns
        String[] tableColumns = new String[]{"id", "childname", "nickname"};

        Cursor cursor = db.query("child", tableColumns, "parent =? ", new String[]{_parentName}, null, null, null);
        cursor.moveToFirst();

        while (!cursor.isAfterLast()) {
            ChildUser child = new ChildUser();
            //take values from database
            child.setId(cursor.getInt(0));
            child.setChildName(cursor.getString(1));
            child.setNickName(cursor.getString(2));


            //add to db
            childList.add(child);

            cursor.moveToNext();
        }
        return childList;
    }

    public String[] getChildNames() {
        String[] tableColumns = new String[]{"childname"};

        Cursor cursor = db.query("child", tableColumns, "parent =?", new String[]{_parentName}, null, null, null);
        String[] array = new String[cursor.getCount()];//sets the length of the array
        int i = 0;
        while (cursor.moveToNext()) {
            String uname = cursor.getString(cursor.getColumnIndex("childname"));
            array[i] = uname;
            i++;
        }

        return array;
    }
}

这是我的ChildListAdapter 使用不同的文本属性再次显示相同的按钮

package com.example.victor.kidsrewards;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * Created by Victor on 4/3/2016.
 */
public class ChildListAdapter extends ArrayAdapter<ChildUser> {

    private final Context context;

    private final List<ChildUser> childList;

    public ChildListAdapter(Context context, List<ChildUser> childList){
        super(context, R.layout.fragment_child_list_layout, childList);//fragment_child_list
        this.context = context;
        this.childList= childList;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.fragment_child_list_layout,parent,false);


        Button childName = (Button)rowView.findViewById(R.id.childButton);
        childName.setText(childList.get(position).getNickName());




        return rowView;
    }

ChildList类

package com.example.victor.kidsrewards;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class ChildList extends ListFragment {


    private ChildDataBase cdb;

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





   /* @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }*/

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_child_list, container, false);


    }

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


        cdb = new ChildDataBase(getContext());


        setListAdapter(new ChildListAdapter(getContext(), cdb.getChildren()));


    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

}





    }

fragment_child_list_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >



    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/childButton"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="15dp" />


</RelativeLayout>

fragment_child_list.xml

<FrameLayout 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"
    tools:context="com.example.victor.kidsrewards.ChildList">

    <!-- TODO: Update blank fragment layout -->
    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:text="TODO TEXT GOES_HERE">
    </ListView>

</FrameLayout>

2 个答案:

答案 0 :(得分:0)

您可以在适配器的OnClickListener方法中为Button设置getView()。 position参数可用于标识单击了哪个按钮。

参见代码

@Override
public View getView(final int position, View convertView, ViewGroup parent){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.fragment_child_list_layout,parent,false);

    Button childName = (Button)rowView.findViewById(R.id.childButton);
    childName.setText(childList.get(position).getNickName());

    childName.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            String selectedChild = childList.get(position).getNickName();
            // do something with selected item

        }
    });


    return rowView;
}

答案 1 :(得分:0)

@覆盖     public View getView(int position,View convertView,ViewGroup parent){         LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.fragment_child_list_layout,parent,false);


    Button childName = (Button)rowView.findViewById(R.id.childButton);
    childName.setText(childList.get(position).getNickName());
    childName.setOncLickListener(new OnclickListener() {
       public void onClick() {
            // do something for click each row
       }
    return rowView;
}