Custom ListVIew中的复选框无效

时间:2017-04-18 10:32:52

标签: java android listview

当我使用textview而不是checkbox时,它完美地运行。但是使用复选框它不起作用。这有什么不对?

我想用每个复选框更新Menuitem

那么我怎样才能使这个工作? 这是自定义列表视图,所以我的xml文件中没有listview。我只在xml文件中有textview和checkbox。

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();
    }

}

1 个答案:

答案 0 :(得分:0)

没有点击 - 您需要在列表中使用的其他小部件上将focusable设置为false

如果没有完整的代码,很难确定。