Radiobutton检查点击列表视图onclicklistener

时间:2017-04-26 07:59:07

标签: android

我正在使用带有textview和Radiobutton的listview。我想在listview onItemClickListener中实现单选按钮获取检查,我该怎么做?这是我的主要活动。我尝试这样做,但是当我点击列表视图复选框检查多个选项时,当我滚动列表单选按钮框时会丢失其状态。

package com.timesheets.momentumtech.foremantimesheets.Activities;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.timesheets.momentumtech.foremantimesheets.Adapters.ListsAdapter;
import com.timesheets.momentumtech.foremantimesheets.Adapters.NamesAdapter;
import com.timesheets.momentumtech.foremantimesheets.Models.EmployeeModel;
import com.timesheets.momentumtech.foremantimesheets.Models.LitModel;
import com.timesheets.momentumtech.foremantimesheets.R;

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

import android.os.Handler;

public class MainActivity extends AppCompatActivity {


    Handler handler;
    Button setupBtn;
    Button reportTimeBtn;
    FloatingActionButton floatingActionButton;
    private PopupWindow mpopup;
    TextView cancle_popup;
    NamesAdapter namesAdapter;
   public static List<LitModel> litModelsList = new ArrayList<>();
    List<EmployeeModel> employeeModelList = new ArrayList<>();
   public static ListView employeeNames, list;
    public String chek = "cost";
    public static TextView scheled;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setupBtn = (Button) findViewById(R.id.setup_main_btn);
        setupBtn.setBackgroundColor(Color.rgb(30, 171, 225));
        floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
        scheled = (TextView) findViewById(R.id.scheld);
        scheled.setText("Cost Code");
        employeeNames = (ListView) findViewById(R.id.names);
        list = (ListView) findViewById(R.id.lists);

        final View popUpView = getLayoutInflater().inflate(R.layout.raw, null);


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

                // inflating popup layout
                mpopup = new PopupWindow(popUpView, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); //Creation of popup
                mpopup.setAnimationStyle(android.R.style.Animation_Translucent);
                mpopup.showAtLocation(popUpView, Gravity.BOTTOM, 0, 0);

                mpopup.setOnDismissListener(new PopupWindow.OnDismissListener() {

                    @Override
                    public void onDismiss() {
                        mpopup.dismiss();
                    }
                });
                cancle_popup = (TextView) popUpView.findViewById(R.id.cancle);
                cancle_popup.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mpopup.dismiss();
                    }
                });

            }
        });

        prepareListData();
        prepareList();
        //prepareTime();


        setupBtn = (Button) findViewById(R.id.setup_main_btn);
        setupBtn.setBackgroundColor(Color.rgb(30, 171, 225));
        //setupBtn.setTextColor(Color.BLACK);
        reportTimeBtn = (Button) findViewById(R.id.report_main_btn);
        reportTimeBtn.setBackgroundColor(Color.rgb(30, 171, 225));
        setupBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setupBtn.setBackgroundColor(Color.rgb(255, 254, 254));
                setupBtn.setTextColor(Color.BLACK);
                Intent i = new Intent(MainActivity.this, ReportTime.class);
                startActivity(i);
            }
        });

        reportTimeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reportTimeBtn.setBackgroundColor(Color.rgb(255, 254, 254));
                reportTimeBtn.setTextColor(Color.BLACK);
                Intent i = new Intent(MainActivity.this, TimeEntry.class);
                startActivity(i);
            }
        });


        **list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               int selected = -1;

              final RadioButton radioButton = (RadioButton) view.findViewById(R.id.costcode_rb);
               if (radioButton.isChecked()){
                   radioButton.setChecked(false);
               }else {
                   radioButton.setChecked(true);
               }
               radioButton.setChecked(true);


                   Toast.makeText(getApplicationContext(),"Helllo",Toast.LENGTH_SHORT).show();


           }
       });**


    }

     public void prepareList() {

        LitModel listModel = new LitModel("120,Poured");
        litModelsList.add(listModel);

        listModel = new LitModel("125,Form");
        litModelsList.add(listModel);

        listModel = new LitModel("130,Reber");
        litModelsList.add(listModel);

        listModel = new LitModel("14,018,HDPE");
        litModelsList.add(listModel);

        listModel = new LitModel("145,Sampl");
        litModelsList.add(listModel);

        listModel = new LitModel("150,Sample");
        litModelsList.add(listModel);

        listModel = new LitModel("160,Sample");
        litModelsList.add(listModel);

        listModel = new LitModel("170,Sample");
        litModelsList.add(listModel);

        listModel = new LitModel("171,Sample");
        litModelsList.add(listModel);

        listModel = new LitModel("172,Sample");
        litModelsList.add(listModel);

        listModel = new LitModel("172,Sample");
        litModelsList.add(listModel);

        listModel = new LitModel("172,Sample");
        litModelsList.add(listModel);

        listModel = new LitModel("172,Sample");
        litModelsList.add(listModel);

        listModel = new LitModel("172,Sample");
        litModelsList.add(listModel);

        ListsAdapter listAdapter = new ListsAdapter(MainActivity.this, litModelsList);
        list.setAdapter(listAdapter);




    }

    private void prepareListData() {


        EmployeeModel employeeModel = new EmployeeModel("All");
        employeeModelList.add(employeeModel);

        employeeModel = new EmployeeModel("Steve");
        employeeModelList.add(employeeModel);

        employeeModel = new EmployeeModel("Dean");
        employeeModelList.add(employeeModel);

        employeeModel = new EmployeeModel("Jacob Betten");
        employeeModelList.add(employeeModel);

        employeeModel = new EmployeeModel("Doug");
        employeeModelList.add(employeeModel);

        employeeModel = new EmployeeModel("Dennis Peters");
        employeeModelList.add(employeeModel);


        employeeModel = new EmployeeModel("Roger,StoneStone");
        employeeModelList.add(employeeModel);


        employeeModel = new EmployeeModel("JB Flet");
        employeeModelList.add(employeeModel);

        employeeModel = new EmployeeModel("Justin Willis");
        employeeModelList.add(employeeModel);

        employeeModel = new EmployeeModel("Bill Jones");
        employeeModelList.add(employeeModel);

        employeeModel = new EmployeeModel("Roger Doug Roberts");
        employeeModelList.add(employeeModel);
        employeeModel = new EmployeeModel("Roger Doug ");
        employeeModelList.add(employeeModel);
        employeeModel = new EmployeeModel("Roberts");
        employeeModelList.add(employeeModel);
        employeeModel = new EmployeeModel("Roger ");
        employeeModelList.add(employeeModel);
        employeeModel = new EmployeeModel(" Doug Roberts");
        employeeModelList.add(employeeModel);

        namesAdapter = new NamesAdapter(employeeModelList, this);
        employeeNames.setAdapter(namesAdapter);


    }


    @Override
    public void onBackPressed() {

    }


}

我的适配器类

package com.timesheets.momentumtech.foremantimesheets.Adapters;

/**
 * Created by MomentumTech on 4/24/2017.
 */

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.timesheets.momentumtech.foremantimesheets.Activities.MainActivity;
import com.timesheets.momentumtech.foremantimesheets.Models.LitModel;
import com.timesheets.momentumtech.foremantimesheets.R;

import java.util.List;

import static com.timesheets.momentumtech.foremantimesheets.Activities.MainActivity.list;
import static com.timesheets.momentumtech.foremantimesheets.Activities.MainActivity.litModelsList;
//import static com.timesheets.momentumtech.foremantimesheets.Activities.MainActivity.prepareList;
import static com.timesheets.momentumtech.foremantimesheets.Activities.MainActivity.scheled;

/**
 * Created by MomentumTech on 3/14/2017.
 */

public class ListsAdapter extends BaseAdapter {
    private List<LitModel> litModelList;
    private Context context;
    private LitModel litModels;
    int selected = -1;
    public String chek = "cost";

    public ListsAdapter(Context context, List<LitModel> litModelList) {

        this.context = context;
        this.litModelList = litModelList;
    }

    @Override
    public int getCount() {
        return litModelList.size();
    }

    @Override
    public Object getItem(int i) {
        return litModelList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {


        View v = view;
        final Holder holder;

        if (view == null) {

            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = layoutInflater.inflate(R.layout.costcode_lv_model, null);
            holder = new Holder(v);
            v.setTag(holder);


        } else {
            holder = (Holder) v.getTag();
        }

        litModels = litModelList.get(i);


        holder.costcode_tv.setText(litModels.getList());


        if (holder.costcode_rb != null) {
            holder.costcode_rb.setTag(i);
            holder.costcode_rb.setChecked(i == selected);

        }

        if (holder.costcode_rb != null) {
            holder.costcode_rb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View view) {


                    selected = (Integer) view.getTag();
                    notifyDataSetChanged();
                }
            });


        }

        return v;
    }









 private class Holder {
    public TextView costcode_tv;
    public RadioButton costcode_rb;

    Holder(View v) {
        costcode_tv = (TextView) v.findViewById(R.id.costcode_tv);
        costcode_rb = (RadioButton) v.findViewById(R.id.costcode_rb);
       // costcode_rb.setFocusable(false);

    }
}
}

和XML文件

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


    <RadioButton
        android:id="@+id/costcode_rb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_marginTop="10dp"
        android:button="@drawable/radio_check" />

    <TextView
        android:id="@+id/costcode_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/costcode_rb"
        android:layout_alignBottom="@+id/costcode_rb"
        android:layout_marginLeft="15dp"
        android:layout_toEndOf="@+id/costcode_rb"
        android:layout_toRightOf="@+id/costcode_rb"
        android:text="TextView"
        android:textColor="@color/textcolor" />

    <View
        android:id="@+id/view3"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/costcode_tv"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="25dp"

        android:background="#a3a3a3" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

您可以在costcode_lv_model的父视图上设置onclickListner

unsigned char buffer[5];
    int samples = 0;
    USART_Init0(MYUBRR);
    uint16_t adc_result0, adc_result1;
    float ADCaverage = 0;

    while(1)
    {

        adc_result0 = adc_read(0);      // read adc value at PA0
        samples++;
        //adc_result1 = adc_read(1);      // read adc value at PA1

        ADCaverage = (ADCaverage + adc_result0)/samples;

        sprintf(buffer, "%d\n", (int)ADCaverage); 
        char * p = buffer;
        while (*p) { USART_Transmit0(*p++); }
        _delay_ms(1000);
    }
    return(0);
}

为此RelativeLayout提供id并将onClickListner应用于它。

在onClick方法中

检查那里的checkBox。

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


<RadioButton
    android:id="@+id/costcode_rb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ........

答案 1 :(得分:0)

您的LitModel需要有一个属性来保存复选框的状态。

如:

public class LitModel{
    private boolean state;

    public boolean isState() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
    }
}

复选框以设置LitModel的状态。

holder.costcode_rb.setChecked(litModels.isState());

如果要更改状态,请更改LitModel的状态,然后刷新。

litModels.setState(true);
notifyDataSetChanged();