2012-01-24 46 views
0

嗨我創建了完全基於xml的android應用程序,所以我使用了sax解析器。現在我有一個疑問,我的疑問是如何解析arraylist值一個活動到另一個活動。首先我解釋我的項目,我的第一個屏幕顯示2spinner和一個視圖。如何獲取我的arraylist字符串值?

兩個微調框和網格視圖顯示我的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請幫助我。

回答

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查看,添加或從該集合中刪除的項目。