2017-04-26 79 views
0

我使用的是带有textview和Radiobutton的listview。我想实现单选按钮获取检查ListView onItemClickListener,我该怎么做?这是我的主要活动。我试图做到这一点,但当我点击listview复选框检查多个选择,当我滚动列表单选按钮框损失其状态。Radiobutton检查点击列表视图onclicklistener

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

其中是checkBox? – Opiatefuchs

+0

抱歉,我的错误是单选按钮没有复选框抱歉。 – user3470118

+0

请不要只复制你的整个程序。它可能包含许多不相关的细节,读者在尝试重现问题时需要忽略这些细节。有关此方面的帮助,请阅读[如何创建最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 – Vinzenz

回答

0

您可以设置onclickListner在costcode_lv_model

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

的父视图给id来此的RelativeLayout并在Cli上申请ckListner它。

in onClick方法检查那边的复选框。

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

      // code here to set the radioButton checked 
      } 
     }); 
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();