我应该如何在Android中将Arraylist传递给BaseAdapter?

时间:2015-02-15 07:05:32

标签: android android-listview android-arrayadapter

我有以下问题。我无法将Arraylist传递给另一个扩展BaseActivity的Activity。

ListViewAdapter.java

package com.example.coherendz.uobgoldandsilver;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;



public class ListViewAdapter extends BaseAdapter {

    ArrayList<String> myList = new ArrayList();
    ArrayList<String> selectedList = new ArrayList();
    LayoutInflater inflater,layout;
    Context context;
    private ArrayList currency,weight,Buy,Sell;
    ArrayList<String> names= new ArrayList<String>();
    Activity a;


    public ListViewAdapter(ArrayList myList,ArrayList names,ArrayList currency,ArrayList weight,ArrayList Buy,ArrayList Sell, Context context) {
        this.myList = myList;
        this.names = names;
        this.currency = currency;
        this.weight = weight;
        this.Buy = Buy;
        this.Sell = Sell;
        this.context = context;
        inflater = LayoutInflater.from(this.context);
    }


    public ListViewAdapter(Context context,ArrayList<String> selectedList){
      layout = LayoutInflater.from(context);
      this.selectedList = selectedList;
    }



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

    @Override
    public Object getItem(int position) {
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

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

        MyViewHolder mViewHolder=null;


        if(convertView == null) {
            convertView = inflater.inflate(R.layout.custom_row, null);
            mViewHolder = new MyViewHolder(convertView);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }

        String name="",weig="",buy="",sell="";

         String temp_name="";


        if(selectedList.size()== 7 || selectedList.isEmpty())
        {

           /*for(int i =0;i<=names.size();i++)
           {
               for (int j = 1; j < names.size(); j++)
               {
                   //name = names.get(i).toString();
                   name = names.get(position).toString();
                   temp_name = names.get(j).toString();

                   mViewHolder.tvTitle.setText(name);

                   if (temp_name.isEmpty()) {
                       temp_name = name;
                       mViewHolder.tvTitle.setText(temp_name);
                   } else {

                       mViewHolder.tvTitle.setText(name);
                   }
               }

               names.clear();

           }*/

            /*for(String s : names)
            {

                name = names.get(position).toString();


                if(name.isEmpty())
                {
                    mViewHolder.tvTitle.setText(temp_name);

                }
                else
                {
                    mViewHolder.tvTitle.setText(name);
                }

                temp_name = name;
            }*/

            name = names.get(position).toString();
            weig = weight.get(position).toString();
            buy = Buy.get(position).toString();
            sell = Sell.get(position).toString();

            mViewHolder.tvTitle.setText(name);
            mViewHolder.tvQty.setText(weig);
            mViewHolder.tvBuy.setText(buy);
            mViewHolder.tvSell.setText(sell);

        }
       else
        {
            StringBuilder string1 = new StringBuilder();

            for(int i=0; i<selectedList.size();i++){
                string1.append(selectedList.get(i).toString());
            }

            Log.i("Srikanth.M",string1.toString());

         for(int j = 0;j<names.size();j++)
         {

          if(selectedList.contains(names.get(j)))
          {

              name = names.get(position).toString();
              weig = weight.get(position).toString();
              buy = Buy.get(position).toString();
              sell = Sell.get(position).toString();

              mViewHolder.tvTitle.setText(name);
              mViewHolder.tvQty.setText(weig);
              mViewHolder.tvBuy.setText(buy);
              mViewHolder.tvSell.setText(sell);

          }
          }

         }

        return convertView;
    }


    private class MyViewHolder {
        TextView tvTitle,tvQty,tvBuy,tvSell;

        MyViewHolder(View v){

          tvTitle = (TextView) v.findViewById(R.id.tvName);
          tvQty = (TextView) v.findViewById(R.id.tvQty);
          tvBuy = (TextView) v.findViewById(R.id.tvBuy);
          tvSell = (TextView) v.findViewById(R.id.tvSell);

        }
    }
 }

Settings.java

package com.example.coherendz.uobgoldandsilver;


import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import java.util.ArrayList;


public class Settings extends ActionBarActivity {


    private String arbo,cas;
    ArrayList<String> selectedList = new ArrayList<String>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actvity_settings);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_settings, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.action_click) {

            ListViewAdapter adapter = new ListViewAdapter(this,selectedList);
            finish();

        }

        return super.onOptionsItemSelected(item);
    }


    public void onCheckboxClicked(View view) {
        // Is the view now checked?

        boolean checked = ((CheckBox) view).isChecked();


        // Check which checkbox was clicked
        switch (view.getId()) {
            case R.id.cbArgor:
                if (checked) {

                    CheckBox arbor = (CheckBox) findViewById(R.id.cbArgor);
                    arbo = arbor.getText().toString();
                    Log.i("Srikanth", arbo);
                    selectedList.add(arbo);

                }
                // Put some meat on the sandwich
                else {

                  selectedList.remove(0);
                  Log.i("Srikanth","Removed Argo");

                }
                // Remove the meat
                break;

            case R.id.cbCast:
                if (checked) {

                    CheckBox cast = (CheckBox) findViewById(R.id.cbCast);
                    cas = cast.getText().toString();
                    Log.i("Srikanth", cas);
                    selectedList.add(cas);
                }
                // Cheese me
                else {

                    selectedList.remove(cas);
                    Log.i("Srikanth","Removed Cast");
                }
                // I'm lactose intolerant
                break;

            case R.id.cbGoldCertificate:
                if (checked) {



                }
                // Cheese me
                else {


                }
                // I'm lactose intolerant
                break;

            case R.id.cbGoldSavings:
                if (checked) {



                }
                // Cheese me
                else {


                }
                // I'm lactose intolerant
                break;

            case R.id.cbGoldBullion:
                if (checked) {



                }
                // Cheese me
                else {


                }
                // I'm lactose intolerant
                break;

            case R.id.cbPampGoldBars:
                if (checked) {



                }
                // Cheese me
                else {


                }
                // I'm lactose intolerant
                break;


            case R.id.cbPampGoldBarsWithHook:
                if (checked) {



                }
                // Cheese me
                else {


                }
                // I'm lactose intolerant
                break;

            case R.id.cbSilverPassbookAccount:
                if (checked) {



                }
                // Cheese me
                else {


                }
                // I'm lactose intolerant
                break;
        }


        StringBuilder string = new StringBuilder();

        for(int i=0; i<selectedList.size();i++){
            string.append(selectedList.get(i).toString());
        }

        Log.i("Srikanth.S",string.toString());


    }


}

我无法检索selectedList的值。

任何建议?

1 个答案:

答案 0 :(得分:0)

因为您要将值添加到selectedList而不是适配器数据。

第1步:在适配器中添加这些方法:

public void addItem(String item){
     this.selectedList.add(item);
     notifyDataSetChanged();
}

public void removeItem(int position){
     this.selectedList.remove(position);
     notifyDataSetChanged();
}

第2步Settings活力:

1)将ListViewAdapter adapter作为类变量。

2)替换所有这些电话:

selectedList.add(...);

adapter.addItem(...); {p>和selectedList.remove(...); adapter.removeItem(...);