Advertisement
Guest User

Untitled

a guest
May 31st, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.98 KB | None | 0 0
  1. [1]: http://i.stack.imgur.com/6LwWR.png
  2.  
  3.  
  4.  
  5.  
  6. This is my Adapter class
  7.  
  8. public class NewFeePlanAdapter extends RecyclerView.Adapter<NewFeePlanAdapter.MyViewHolder> {
  9.  
  10. Context context;
  11. ArrayList<FeeTypeModel> feeTypeModelArrayList;
  12.  
  13.  
  14. public NewFeePlanAdapter(Context context, ArrayList<FeeTypeModel> feeTypeModelArrayList){
  15. this.context = context;
  16. this.feeTypeModelArrayList = feeTypeModelArrayList;
  17. }
  18.  
  19.  
  20.  
  21.  
  22. @Override
  23. public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  24.  
  25. View itemView = LayoutInflater.from(parent.getContext())
  26. .inflate(R.layout.new_fee_list_row, parent,false);
  27.  
  28. MyViewHolder myViewHolder = new MyViewHolder(itemView,new MyCustomEditTextListener());
  29.  
  30. return myViewHolder;
  31. }
  32.  
  33. FeeTypeModel feeTypeModel;
  34.  
  35. @Override
  36. public void onBindViewHolder(final MyViewHolder holder, final int position) {
  37.  
  38. feeTypeModel = feeTypeModelArrayList.get(position);
  39. holder.fee_type_name_tv.setText(feeTypeModel.getFeeType().toString());
  40.  
  41.  
  42. if (feeTypeModel.getLinearLayout()!=null) {
  43. holder.new_plan_main_ll.addView(feeTypeModel.getLinearLayout());
  44. }
  45.  
  46. holder.myCustomEditTextListener.updatePosition(position);
  47. holder.fee_amount_et.setText( feeTypeModelArrayList.get(position).getAmount());
  48.  
  49. holder.fee_type_cb.setOnCheckedChangeListener(null);
  50. holder.fee_type_cb.setChecked(feeTypeModel.isSelectPlan());
  51.  
  52.  
  53. holder.fee_type_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  54. @Override
  55. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  56. feeTypeModelArrayList.get(position).setSelectPlan(isChecked);
  57.  
  58. }
  59. });
  60.  
  61.  
  62. holder.add_installments_iv.setOnClickListener(new View.OnClickListener() {
  63. @Override
  64. public void onClick(View v) {
  65. if (holder.fee_amount_et.getText().length() == 0){
  66. Toast.makeText(context,"Please Enter Amount",Toast.LENGTH_LONG).show();
  67. }else {
  68. showDialogForNumberOfInstallments(v,position,Long.parseLong(holder.fee_amount_et.getText().toString()));
  69.  
  70. }
  71. }
  72. });
  73.  
  74.  
  75. }
  76.  
  77. @Override
  78. public int getItemCount() {
  79. return feeTypeModelArrayList.size();
  80. }
  81.  
  82.  
  83.  
  84.  
  85. public class MyViewHolder extends RecyclerView.ViewHolder {
  86.  
  87. public TextView fee_type_name_tv;
  88. EditText fee_amount_et;
  89. ImageView add_installments_iv;
  90. CheckBox fee_type_cb;
  91. public MyCustomEditTextListener myCustomEditTextListener;
  92. LinearLayout new_plan_main_ll;
  93.  
  94. public MyViewHolder(View view, MyCustomEditTextListener myCustomEditTextListener) {
  95. super(view);
  96. new_plan_main_ll = (LinearLayout)view.findViewById(R.id.new_plan_main_ll);
  97. fee_type_cb = (CheckBox) view.findViewById(R.id.fee_type_cb);
  98. fee_type_name_tv = (TextView) view.findViewById(R.id.fee_type_name_tv);
  99. fee_amount_et = (EditText) view.findViewById(R.id.fee_amount_et);
  100. add_installments_iv = (ImageView)view.findViewById(R.id.add_installments_iv);
  101. this.myCustomEditTextListener = myCustomEditTextListener;
  102. this.fee_amount_et.addTextChangedListener(myCustomEditTextListener);
  103.  
  104. }
  105. }
  106.  
  107. private class MyCustomEditTextListener implements TextWatcher {
  108. private int position;
  109.  
  110. public void updatePosition(int position) {
  111. this.position = position;
  112. }
  113.  
  114. @Override
  115. public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
  116. // no op
  117. }
  118.  
  119. @Override
  120. public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
  121. feeTypeModelArrayList.get(position).setAmount( charSequence.toString());
  122. }
  123.  
  124. @Override
  125. public void afterTextChanged(Editable editable) {
  126. // no op
  127. }
  128. }
  129.  
  130.  
  131.  
  132. AutoCompleteTextView new_fee_plan_name_act;
  133. EditText numberOfInstallments_et;
  134. int selectedPosition;
  135. private void showDialogForNumberOfInstallments(View v,int position, final long amount){
  136. selectedPosition = position;
  137. // Create custom dialog object
  138. final Dialog dialog = new Dialog(context);
  139. // Include dialog.xml file
  140. dialog.setContentView(R.layout.new_plan_name_dialog);
  141.  
  142. dialog.setTitle("Installments");
  143.  
  144. new_fee_plan_name_act = (AutoCompleteTextView) dialog.findViewById(R.id.new_fee_plan_name_act);
  145. new_fee_plan_name_act.setVisibility(View.GONE);
  146.  
  147. numberOfInstallments_et = (EditText)dialog.findViewById(R.id.numberOfInstallments_et);
  148. numberOfInstallments_et.setVisibility(View.VISIBLE);
  149.  
  150. new_fee_plan_name_act.setHint("Enter Number Of Installments");
  151.  
  152. dialog.setCancelable(false);
  153. dialog.show();
  154.  
  155. Button feePlan_cancel_bt = (Button) dialog.findViewById(R.id.feePlan_cancel_bt);
  156. // if decline button is clicked, close the custom dialog
  157. feePlan_cancel_bt.setOnClickListener(new View.OnClickListener() {
  158. @Override
  159. public void onClick(View v) {
  160. // Close dialog
  161. dialog.dismiss();
  162. }
  163. });
  164.  
  165. Button FeePlan_create_bt = (Button) dialog.findViewById(R.id.FeePlan_create_bt);
  166. FeePlan_create_bt.setOnClickListener(new View.OnClickListener() {
  167. @Override
  168. public void onClick(View v) {
  169. if (numberOfInstallments_et.getText().toString().length()==0){
  170. Toast.makeText(context,"Please Enter New number 0f Installments",Toast.LENGTH_LONG).show();
  171. }else {
  172.  
  173. FeeTypeModel feeTypeModel11 = feeTypeModelArrayList.get(selectedPosition);
  174. if (feeTypeModel11.getLinearLayout()!=null) {
  175. feeTypeModel11.getLinearLayout().removeView(v);
  176. }
  177.  
  178. LinearLayout layout = new LinearLayout(context);
  179. layout.setLayoutParams(new LinearLayout.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
  180. layout.setOrientation(LinearLayout.VERTICAL);
  181.  
  182.  
  183.  
  184. // Toast.makeText(context,"Entered Installments "+numberOfInstallments_et.getText().toString(),Toast.LENGTH_LONG).show();
  185. int WrapWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
  186. int WrapHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
  187.  
  188. int length = Integer.parseInt(numberOfInstallments_et.getText().toString());
  189. long instAmount = amount/length;
  190. for (int i=0;i<length;i++){
  191. TextView t = new TextView(context);
  192. t.setText("Installment"+i+" "+instAmount);
  193. layout.addView(t,WrapWidth,WrapHeight);
  194. }
  195.  
  196. feeTypeModel11.setLinearLayout(layout);
  197. dialog.dismiss();
  198.  
  199. notifyDataSetChanged();
  200.  
  201.  
  202. }
  203.  
  204. }
  205. });
  206.  
  207. }
  208.  
  209.  
  210. }
  211.  
  212.  
  213. this is my new_fee_list_row.xml
  214.  
  215. <?xml version="1.0" encoding="utf-8"?>
  216. <android.support.v7.widget.CardView
  217. xmlns:android="http://schemas.android.com/apk/res/android"
  218. android:layout_width="match_parent"
  219. android:elevation="6dp"
  220. android:layout_marginTop="@dimen/activity_horizontal_margin"
  221. android:layout_marginRight="@dimen/activity_horizontal_margin"
  222. android:layout_marginBottom="@dimen/activity_horizontal_margin"
  223. android:layout_marginLeft="@dimen/activity_horizontal_margin"
  224. android:layout_height="wrap_content">
  225.  
  226. <LinearLayout
  227. android:id="@+id/new_plan_main_ll"
  228. android:layout_width="match_parent"
  229. android:orientation="vertical"
  230. android:layout_height="wrap_content">
  231.  
  232. <LinearLayout
  233. android:layout_width="match_parent"
  234. android:orientation="horizontal"
  235. android:layout_height="wrap_content">
  236.  
  237. <CheckBox
  238. android:id="@+id/fee_type_cb"
  239. android:layout_width="wrap_content"
  240. android:layout_height="wrap_content" />
  241.  
  242. <TextView
  243. android:id="@+id/fee_type_name_tv"
  244. android:text="fsdfdsf"
  245. android:layout_marginLeft="@dimen/activity_horizontal_margin"
  246. android:layout_width="wrap_content"
  247. android:layout_height="wrap_content" />
  248.  
  249. </LinearLayout>
  250.  
  251. <RelativeLayout
  252. android:layout_width="match_parent"
  253. android:layout_marginTop="@dimen/activity_horizontal_margin"
  254. android:layout_height="wrap_content">
  255.  
  256. <android.support.design.widget.TextInputLayout
  257. android:layout_width="match_parent"
  258. android:layout_toLeftOf="@+id/add_installments_iv"
  259. android:layout_height="wrap_content">
  260.  
  261. <EditText
  262. android:id="@+id/fee_amount_et"
  263. android:layout_width="130dp"
  264. android:layout_height="wrap_content"
  265. android:imeActionId="@+id/login"
  266. android:imeActionLabel="@string/action_sign_in_short"
  267. android:imeOptions="actionUnspecified"
  268. android:maxLines="1"
  269. android:hint="Enter Amount"
  270. android:inputType="number"
  271. android:singleLine="true"
  272. android:layout_alignParentTop="true"
  273. android:layout_alignParentLeft="true"
  274. android:layout_alignParentStart="true" />
  275.  
  276. </android.support.design.widget.TextInputLayout>
  277. <ImageView
  278. android:id="@+id/add_installments_iv"
  279. android:layout_width="wrap_content"
  280. android:layout_marginLeft="@dimen/activity_horizontal_margin"
  281. android:layout_alignParentRight="true"
  282.  
  283. android:layout_centerVertical="true"
  284. android:layout_marginRight="@dimen/activity_horizontal_margin"
  285. android:src="@android:drawable/ic_input_add"
  286. android:layout_height="wrap_content" />
  287. </RelativeLayout>
  288.  
  289.  
  290.  
  291. </LinearLayout>
  292.  
  293.  
  294. </android.support.v7.widget.CardView>
  295.  
  296.  
  297. //This is my main Activity class
  298.  
  299.  
  300. public class NewFeePlanActivity extends AppCompatActivity {
  301.  
  302. @Override
  303. protected void onCreate(Bundle savedInstanceState) {
  304. super.onCreate(savedInstanceState);
  305. setContentView(R.layout.activity_new_fee_plan);
  306. RecyclerView fee_plans_rv = (RecyclerView)findViewById(R.id.fee_plans_rv);
  307. RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
  308. fee_plans_rv.setLayoutManager(mLayoutManager);
  309. NewFeePlanAdapter newFeePlanAdapter = new NewFeePlanAdapter(NewFeePlanActivity.this, feeTypeModelArrayList);
  310. fee_plans_rv.setAdapter(newFeePlanAdapter);
  311. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement