【问题标题】:Radiobutton check on click in listview onclicklistener单选按钮检查单击列表视图 onclicklistener
【发布时间】:2017-04-30 04:39:28
【问题描述】:

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

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>

【问题讨论】:

  • 复选框在哪里?
  • 抱歉我的错误是单选按钮而不是复选框抱歉。
  • 请不要只复制整个程序。它可能包含许多不相关的细节,读者在尝试重现问题时需要忽略这些细节。如需帮助,请阅读How to create a Minimal, Complete, and Verifiable example

标签: android


【解决方案1】:

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

<?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"
    ........

给这个RelativeLayout id 并应用onClickListner。

在onClick方法中勾选那边的checkBox。

  holder.costcode_relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {

            // code here to set the radioButton checked
            }
        });

【讨论】:

    【解决方案2】:

    您的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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      相关资源
      最近更新 更多