如何将arraylist从活动传递到活动的自定义适配器

时间:2017-05-26 10:05:41

标签: java android arraylist

我解析XML并将XML数据插入到Integer数据的数组列表中。

现在,我想从活动传递到自定义适配器这个数组列表,但我不知道我怎么做。

我试图做到这一点,但没有成功。

我告诉你我的代码。

活动:

public class MyListFragment extends Fragment  /*implements
        CompoundButton.OnCheckedChangeListener, View.OnClickListener*/ {


    ListView lv;
    ArrayList<Planet> planetList;
    static PlanetAdapter plAdapter;
    private TextView txtName;
    private TextView txtEmail;
    private Button btnLogout;
    ListView listView;
    String user = "";
    private Spinner spinner;
    String selState;
    EditText cristo;
    private String zao;
    CheckBox ck;
    //private SQLiteHandler db;
    private SessionManager session;
    BirraAdapter biAdapter;
    PlanetAdapter.PlanetHolder holder;
    private static Context context = null;
    private static FragmentActivity mInstance;
    ArrayList<Integer> hm=new ArrayList<>();
    ArrayList<Integer>mItems;
    HashMap<Integer,String> ar=new HashMap<>();


    Integer[] imageId = {
            R.mipmap.androtuto,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ok,
            /*R.drawable.image6,
            R.drawable.image7
            Environment.getExternalStorageDirectory()" +
                    "                                .getAbsolutePath() + /ordinazioni/16:Maggio:2017-13:36:38 Pm.xml*/

    };

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



        try {
            int a=0;
            String status="";
            boolean statuss = false;
            File file = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/ordinazioni/16:Maggio:2017-21:48:49 PM.xml");
            FileInputStream is = new FileInputStream(file);

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(is);

            Element element = doc.getDocumentElement();
            element.normalize();

            NodeList nList = doc.getElementsByTagName("checkboxes_pizza");

            for (int i = 0; i < nList.getLength(); i++) {
                Node node = nList.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element2 = (Element) node;
                    //tv1.setText(tv1.getText()+"\nName : " + getValue("name", element2)+"\n");
                    String id = getValue("id", element2);
                    a = Integer.parseInt(id);
                    status = getValue("status", element2);

                    statuss= Boolean.parseBoolean(status);

                    System.out.println("XML:" + a);

                    hm.add(a);




                    }

                }




        } catch (Exception e) {
            e.printStackTrace();
        }



}
}

CUSTOM ADAPTER:

public abstract class PlanetAdapter extends ArrayAdapter<Planet> implements CompoundButton.OnCheckedChangeListener, View.OnClickListener

{
    private List<Planet> planetList;
    private Context context ;
    ArrayList<Birra> objects;
    public boolean Checked;
    private Integer[] imageId;
    String ciao;
    static PlanetAdapter plAdapter;
    String selState ;
    boolean[] itemChecked;

    public PlanetAdapter(List<Planet> planetList, Context context,Integer[] imageId) {
        super(context, R.layout.single_listview_item,planetList);
        //itemChecked = new boolean[planetList.size()];

        this.planetList = planetList;
        this.context = context;
        this.imageId = imageId;
        this.itemChecked=new boolean[planetList.size()];

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }

    @Override
    public void onClick(View v) {

    }



    public class PlanetHolder {
        public TextView planetName;
        public TextView distView;
        public TextView valuta;
        public CheckBox chkBox;
      //  public EditText edit;
        public TextView caratteristica;
        public TextView id;
        public ImageView imageView;
        public Spinner spinner;
        public NumberPicker np;


    }


    @Override
    public int getCount() {
        return planetList.size();
    }

    @Override
    public Planet getItem(int position) {
        return planetList.get(position);
    }


    static ArrayList<Integer> itemss;




    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {

        View row = convertView;
        PlanetHolder holder = null;



        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(R.layout.single_listview_item, parent, false);
            holder = new PlanetHolder();
            holder.planetName = (TextView) row.findViewById(R.id.name);
            holder.distView = (TextView) row.findViewById(R.id.dist);
            holder.valuta = (TextView) row.findViewById(R.id.valuta);
            holder.chkBox = (CheckBox) row.findViewById(R.id.chk_box);
         //   holder.edit = (EditText) row.findViewById(R.id.editText);
            holder.spinner = (Spinner) row.findViewById(R.id.simpleSpinner);
            holder.np=(NumberPicker)row.findViewById(R.id.np);
           // holder.edit2 = (EditText) row.findViewById(R.id.editText2);
          //  holder.iterator = (Button) row.findViewById(R.id.iterator);
           // Button iterator=(Button) row.findViewById(R.id.iterator);
           // iterator.setOnClickListener(this.PlanetHolder);
            holder.caratteristica=(TextView) row.findViewById(R.id.caratteristica);
            holder.imageView = (ImageView) row.findViewById(R.id.ivImage);
         //   holder.edit.setVisibility(View.GONE);
         //   holder.edit.setEnabled(false);
            holder.spinner.setVisibility(View.GONE);
          //  holder.edit.setVisibility(View.GONE);


            // holder.id = (TextView) row.findViewById(R.id.id);


            row.setTag(holder);

        } else {
            holder = (PlanetHolder) row.getTag();


        }



        final Planet p = planetList.get(position);
        System.out.println("stampato P" + p);
        holder.imageView.setImageResource(imageId[position]);
        holder.planetName.setText(p.getName());
        holder.distView.setText("" + p.getDistance());
        holder.valuta.setText("" + p.getValuta());
        holder.chkBox.setChecked(p.isSelected());
        holder.chkBox.setTag(p);
        holder.caratteristica.setText(p.getCaratteristica());

        // holder.edit.setEnabled(false);

        SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
        boolean isChecked = states.getBoolean("holder.chkBox" + holder.planetName.getText().toString(), false);
        System.out.println(isChecked);


        //holder.chkBox.setChecked(true);


        if (isChecked) {



            holder.spinner.setVisibility(View.VISIBLE);
            holder.np.setVisibility(View.VISIBLE);







        } else {
            holder.chkBox.setChecked(false);
          //  holder.edit.setVisibility(View.GONE);
          //  holder.edit.setEnabled(false);
            holder.spinner.setVisibility(View.GONE);
            holder.np.setVisibility(View.GONE);

         }


        holder.chkBox.setOnCheckedChangeListener(PlanetAdapter.this);



        final PlanetHolder finalHolder = holder;
        Planet z=getItem(position);
        System.out.println(z);




       /* holder.chkBox.setChecked(true);
        holder.spinner.setVisibility(View.VISIBLE);
        holder.np.setVisibility(View.VISIBLE);*/


       // holder.chkBox.setChecked(true);
      //holder.chkBox.findViewById(R.id.chk_box).setVisibility(View.INVISIBLE);

        /*for(int i=0;i<planetList.size();i++){
            if(4==position)
           finalHolder.chkBox.setChecked(true);

        }*/





        holder.chkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (finalHolder.chkBox.isChecked()) {

                itemChecked[position]=true;

                    System.out.println("POSITION:"+position);
                   // finalHolder.edit.setVisibility(View.VISIBLE);
                   // finalHolder.edit.setEnabled(true);
                    finalHolder.spinner.setVisibility(View.VISIBLE);
                    finalHolder.np.setVisibility(View.VISIBLE);
                    finalHolder.np.setMaxValue(10);
                    finalHolder.np.setMinValue(0);
                    finalHolder.np.setWrapSelectorWheel(true);
                   // finalHolder.np.getMinValue();
                   // System.out.println("valore picker alla selezione della checkbox"+finalHolder.np.getMinValue());

                    // finalHolder.edit2.setVisibility(View.VISIBLE);
                    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                            getContext(),
                            R.array.country_arrays,
                            android.R.layout.simple_spinner_item);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    //finalHolder.spinner.setAdapter(adapter);
                    finalHolder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            finalHolder.spinner.setSelection(position);
                          //  selState = finalHolder.spinner.getSelectedItem().toString();
                         //   p.setQuantità(finalHolder.edit.getText().toString().trim());
                            p.setTipo(finalHolder.spinner.getSelectedItem().toString());

                          //  finalHolder.edit2.setText(selState);
                          //  System.out.println(selState);
                            System.out.println("p.getTipo()"+p.getTipo());

                           /* Toast.makeText(
                                    getContext(),
                                    "Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();*/



                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }


                    });
                    finalHolder.np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                        @Override
                        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                            p.setQuantità2(newVal);
                            System.out.println(p.getQuantità2());
                            SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = states.edit();
                            editor.putInt("finalHolder.picker" + finalHolder.planetName.getText().toString(), finalHolder.np.getId());
                            editor.commit();
                        }
                    });



                    SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = states.edit();
                    editor.putBoolean("holder.chkBox" + finalHolder.planetName.getText().toString(), true);
                    editor.commit();



                } else {
                    finalHolder.spinner.setVisibility(View.GONE);
                    finalHolder.np.setVisibility(View.GONE);
                    finalHolder.np.setValue(0);


                }

            }
        });




        return row;
    }
   /* Planet getPlanet(int position) {
        return ((Planet) getItem(position));
    }*/




    /*CompoundButton.OnCheckedChangeListener myCheckChangList = new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            getPlanet((Integer) buttonView.getTag()).isSelected() = isChecked;
        }
    };*/


   /* ArrayList<Planet> getBox() {
        ArrayList<Planet> box = new ArrayList<Planet>();
        for (Planet p : planetList) {
            if (p.isSelected())
                box.add(p);
        }
        return box;
    }*/



    }

1 个答案:

答案 0 :(得分:0)

您需要在片段

中创建适配器
plAdapter = new PlanetAdapter(planetList, ...);

然后将其设置为listView:

lv.setAdapter(plAdapter);