如何解析我的arraylist值一个活动到另一个活动?

时间:2012-01-23 11:33:17

标签: android xml saxparser

您好我已经创建了一个完全基于xml的项目。我怀疑现在我将所有xml值存储在我的数组列表中。数组列表名称为mspec_List。如何解析这个数组列表值?我使用sax解析器获得了所有值。我的第一个活动显示一些图像,如果我点击该图像我想在我的下一个活动屏幕中显示段落存储在我的数组列表中的一些段落。如何在我的下一个活动中显示该段落?我创建了新类,我正在使用textview,我也希望在该textview中显示该数组列表值。请帮助我....“

我正在尝试解析该arraylist值,但它无法正常工作,请参阅下面的代码。

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

        }
    });

完整源代码:

  package ml.ml;

    import java.io.IOException; 
    import java.io.InputStream;
    import java.util.ArrayList;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.xml.sax.InputSource;
    import org.xml.sax.XMLReader;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.content.res.AssetManager;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.drawable.Drawable;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.AdapterView.OnItemClickListener;

    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;
            //ArrayList<String> path_List =new ArrayList<String>();

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

    }

`

3 个答案:

答案 0 :(得分:3)

如果你有String ArrayList,你只需使用intent.putStringArrayListExtra(name, value)传递它。您可以查看我的答案here

答案 1 :(得分:1)

tabview.class

Intent i = getIntent();  
arrlist = i.getStringArrayListExtra("name");

答案 2 :(得分:0)

您可以将model_List设为静态。