-3

When i use textview instead of checkbox, it works perfectly. But with checkbox it doesnt work. What is wrong here?

I want to update Menuitem with every checkboxevent.

So how can i make this working? This is custom made listview, so i dont have listview in my xml file. I only have textview and checkbox in xml file.

package com.example.chaps.pizzaorder;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by chaps on 17/04/2017.
 */

public class veggies extends ListFragment {

    ArrayList<HashMap<String, String>> data=new ArrayList<HashMap<String,String>>();
    SimpleAdapter adapter;
    String st;
    double value;
    double new_value;
    //TextView txt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);


    }



    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        String[] menuItem= getResources().getStringArray(R.array.veggie_topping);



        String[] menuPrice=getResources().getStringArray(R.array.topping_price_veggie);




        HashMap<String, String> map=new HashMap<String, String>();//custom adapter for image and text in list View

        for(int i=0;i<menuItem.length;i++)
        {
            map=new HashMap<String, String>();
            map.put("Pizza",menuItem[i]);
            map.put("Price",menuPrice[i]);
            data.add(map);
        }
        String[] from={"Pizza","Price"};
        int[] to={R.id.textView5,R.id.checkBox};
        adapter=new SimpleAdapter(getActivity(),data,R.layout.veggies,from,to);
        setListAdapter(adapter);



        return super.onCreateView(inflater,container,savedInstanceState);

    }

    public void onStart() {

        super.onStart();
        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/*
    orders fragment = new orders();
    Bundle bundle = new Bundle();
    bundle.putString(String.valueOf(R.string.MD01), data.get(position).get("Pizza"));
    fragment.setArguments(bundle);
    getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.main_content,fragment).commit();

*/
              // st=data.get(position).get("Price");
                st=data.get(position).get("Price").substring(1);
                value=Double.parseDouble(st);
                new_value=new_value+value;

Toast.makeText(getContext(),st,Toast.LENGTH_SHORT).show();




            }
        });



    }
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_pizza_details, menu);
    }

    public void onPrepareOptionsMenu(Menu menu) {

        MenuItem green = menu.findItem(R.id.actnprice);
        if(green.getTitle() != String.valueOf(R.string.actnShow)){
            // If green menu item title not updated then update/change it
            green.setTitle("€"+new_value);
            //Toast.makeText(getContext(),"Green MenuItem Edited",Toast.LENGTH_SHORT).show();
        }
        super.onPrepareOptionsMenu(menu);
        getActivity().supportInvalidateOptionsMenu();
    }

}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • what is not working? TextView and CheckBox are different....Please be more detailed... – Opiatefuchs Apr 18 '17 at 10:37
  • 1
    http://stackoverflow.com/questions/9900913/onitemclicklistener-was-not-work-with-the-checkbox, http://stackoverflow.com/questions/2367936/listview-onitemclicklistener-not-responding – Mike M. Apr 18 '17 at 10:38

1 Answers1

0

It is not taking the click - you need to set focusable to false on the other widgets that you are using in the list.

It is hard to know for sure without the complete code.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
Satyam Anand
  • 377
  • 1
  • 8