如何获得我的arraylist字符串值?

时间:2012-01-24 11:18:11

标签: android arraylist parcelable

您好我创建了完全基于xml的Android应用程序,因此我使用了sax解析器。现在我有一个疑问,我的疑问是如何将arraylist值一个活动解析为另一个活动。第一个我解释我的项目,我的第一个屏幕显示2spinner和一个网格视图。

两个微调器和网格视图显示我的xml标签字符串值,微调器显示一些字符串值,然后我的gridview在微调器底部显示一些图像。这是我的主屏幕。

如果我自动选择第一个微调器值,我的第二个微调器和网格视图也会改变....

我怀疑的是,如果我点击我的网格视图图像,打开一个标签布局。我在标签布局中添加了三个标签。现在标签只显示空白屏幕,但我想在第一个标签视图中显示一些文字。我在arraylist中存储了一些字符串值,arraylist名称是mspec_List。现在我尝试将一个活动的值解析为另一个活动?如果我点击我的图像,我想在我的第一个标签中显示我的arraylist文本,请大家帮帮我........

这是我的主要活动的全部来源:

public class ParxmlActivity extends Activity {

    private String array_spinner[];
    private Cursor cursor;
    private int columnIndex;

    ParsedExampleDataSet parsedExampleDataSet= null;

    ArrayList<String> hltag_List=new ArrayList<String>();
    ArrayList<Subchild>sltag_List=new ArrayList<Subchild>();

    ArrayList<String> name_List =new ArrayList<String>();
    ArrayList<String> path_List =new ArrayList<String>();
    ArrayList<String> mmimage_List = new ArrayList<String>();
    ArrayList<String> mname_List = new ArrayList<String>();
    ArrayList<String> mmimage = new ArrayList<String>();
    ArrayList<String> mvideo_List = new ArrayList<String>();
    ArrayList<schild> model_List =new ArrayList<schild>();
    ArrayList<String> mspec_List = new ArrayList<String>();

    Spinner spinner_hltag,spinner_sltag, spinner_path;

    ArrayAdapter<String> adapter_hltag;
    ArrayAdapter<String> adapter_sltag;
    ArrayAdapter<String> adapter_path;

    int hltagPos=0;
    int sltagPos=0;


    Object sdcard;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
        spinner_hltag= (Spinner) findViewById(R.id.spinner1);
        spinner_sltag = (Spinner) findViewById(R.id.spinner2);

        sdcardImages.setOnItemClickListener(new OnItemClickListener() {


            public void onItemClick(AdapterView parent, View v, int position, long id) {
                Intent intent = new Intent(ParxmlActivity.this, tabview.class);
                intent.putStringArrayListExtra("numbers", mspec_List);
                //intent.putExtra("name",model_List.get(position).name);


                startActivity(intent);  

            }
        });

        sdcardImages.setAdapter(new ImageAdapter(this));

        String[] proj = {MediaStore.Images.Thumbnails._ID};

        cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                proj, null, null, null);


        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);



        try {
            /* Get a SAXParser from the SAXPArserFactory. */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            /* Get the XMLReader of the SAXParser we created. */
            XMLReader xr = sp.getXMLReader();

            /* Create a new ContentHandler and apply it to the XML-Reader */
            ExampleHandler myExampleHandler = new ExampleHandler();
            xr.setContentHandler(myExampleHandler);

            /* Parse the xml-data from our file. */
            xr.parse(new InputSource(getAssets().open("mafile.xml")));
            /* Parsing has finished. */

            parsedExampleDataSet =myExampleHandler. myParsedExampleDataSet;

            for (int i = 0; i < parsedExampleDataSet.gethltag().size(); i++) {
                Log.v("gethltag SIZE ", ""+parsedExampleDataSet.gethltag().size());
                hltag_List.add(parsedExampleDataSet.getHLTag().get(i));

            }

            sltag_List = parsedExampleDataSet.getSLTag(hltag_List.get(hltagPos));

            for(int i = 0; i < sltag_List.size(); i++) {
                name_List.add(sltag_List.get(i).name);
                path_List.add(sltag_List.get(i).path);

            }
            model_List.clear();     
            model_List = sltag_List.get(sltagPos).model;

            for(int j = 0; j<model_List.size(); j++){
                mmimage_List.addAll(model_List.get(j).imageList);
                mname_List.add(model_List.get(j).name);
                mvideo_List.addAll(model_List.get(j).videoList);
                mmimage.add(model_List.get(j).mimage);
                mspec_List.add(model_List.get(j).spec);

            }


            spinner_hltag.setAdapter(new MyAdapter(ParxmlActivity.this, R.layout.row, hltag_List)); 

            spinner_sltag.setAdapter(new MyAdapter1(ParxmlActivity.this, R.layout.row1,name_List)); 


        } catch (Exception e) { 

        }


    }

    private class ImageAdapter extends BaseAdapter {

        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return path_List.size();
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            //Bitmap bitmap = BitmapFactory.decodeFile(path_List);
            Bitmap bitmap = BitmapFactory.decodeFile(path_List.get(position));
            imageView.setImageBitmap(bitmap);
            return imageView;

        }

    }
    public class MyAdapter extends ArrayAdapter<String>{


        public MyAdapter(Context context, int textViewResourceId,   ArrayList<String> hltagList) {
            super(context, textViewResourceId, hltagList);
        }



        @Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.row, parent, false);

            TextView label=(TextView)row.findViewById(R.id.weekofday);
            label.setText(hltag_List.get(position));

            ImageView icon=(ImageView)row.findViewById(R.id.icon);

            Bitmap bitmap = BitmapFactory.decodeFile(path_List.get(position));;
            icon.setImageBitmap(bitmap);

            return row;
        }
    }

    public class MyAdapter1 extends ArrayAdapter<String>{


        public MyAdapter1(Context context, int textViewResourceId,   ArrayList<String> name_List) {
            super(context, textViewResourceId, name_List);
        }



        @Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.row, parent, false);

            TextView sub=(TextView)row.findViewById(R.id.weekofday1);
            sub.setText(name_List.get(position));

            ImageView icon=(ImageView)row.findViewById(R.id.icon);

            Bitmap bitmap = BitmapFactory.decodeFile(path_List.get(position));;
            icon.setImageBitmap(bitmap);

            return row;
        }
    }

}

请参阅上面的代码并看到以下行:1。sdcardImages.setOnItemClickListener 2. mspec_List数组列表然后这是我的标签布局源代码链接:1。http://paste.org/44196 2. http://paste.org/44195请帮助我

2 个答案:

答案 0 :(得分:0)

除了这些原始数据类型之外,如果要传递某些类的整个arraylist,请使用Parcelable。

http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html

答案 1 :(得分:0)

我不会将ArrayList传递给另一个活动,而只是对该列表的引用。可以将arrayList保存在单独的类中。通过为其实例使用静态方法和静态变量,您可以将数据保存在那里。

public class MyData{
    static private MyData instanceOfMyData;
    ArrayList<String> myStringList = new ArrayList<String>(); 

    static synchronized public MyData getInstance() {
       if (instanceOfMyData == null) instanceOfMyData = new MyData();
       return instanceOfMyData;
    }

    static public ArrayList<?> getMyStringList(MyData instance){    
        return instance.myStringList;
    }
}

然后在您的任何活动中,您可以通过两次调用访问ArrayList:

    MyData myData = MyData.getInstance();
    ArrayList<?> myStrings = MyData.getMyStringList(myData);

您可以使用参考myStrings查看,添加或删除此集合中的项目。