在多个活动中使用arrayList元素

时间:2012-07-24 10:54:33

标签: android

我遇到一个问题,从listView获得一个来自arraylist的同一个选择的possition并将两个值传递给nxt活动我的代码只传递列表视图中所选项的值,但是当放入ListAdaptername.get(position);它在这里代替了元素的值是我的代码:

//将值发送到nxt activiti

 lv = getListView();
                   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, names);

                    setListAdapter(adapter); 

                    lv.setOnItemClickListener(new OnItemClickListener(){
                        public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id)
                        {//getApplicationContext

                            //String bb=parent.getItemAtPosition(position).toString();
                            String name=names.get(position);
                               Intent i = new Intent(LastActivity.this, Details.class);
                               String des=desc.get(position).toString();

                                i.putExtra("Name",name);
                                i.putExtra("desc",des);
                                // starting new activity
                              startActivity(i);
                                //LastActivity.this 
                        }
                    });

}

//这里是我定义列表并调用值:

ListView lv;

    static String UName="OName";
    static String UniUrl="Odesc";
    public String val;
    static List<String> desc;
    static ArrayList<String> names;

    URL url;
    XmlPullParserFactory factory;
    XmlPullParser xpp;
    boolean insideItem;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.main);
        lv=(ListView) findViewById(R.id.list1);
        links=new ArrayList<String>();
        names=new ArrayList<String>();
        try{

          url=new URL("http://api.androidhive.info/pizza/?format=xml");
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            xpp = factory.newPullParser();
            xpp.setInput(getInputStream(url), "UTF_8");
            insideItem = false;


                    // Returns the type of current event: START_TAG, END_TAG, etc..

            int eventType = xpp.getEventType();
            int x=0;
            while (eventType != XmlPullParser.END_DOCUMENT) {

                    if (eventType == XmlPullParser.START_TAG) {


                        if (xpp.getName().equalsIgnoreCase("item")) {

                            insideItem = true;

                        } else if (xpp.getName().equalsIgnoreCase(OName)) {

                            if (insideItem)

                                names.add(xpp.nextText()); //extract the headline

                        } else if (xpp.getName().equalsIgnoreCase(Odesc)) {

                            if (insideItem)

                                desc.add(xpp.nextText()); //extract the link of article

                        }
                    }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){

                        insideItem=false;

                    }

                    eventType = xpp.next(); //move to next element

                }


        }catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (XmlPullParserException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

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

        }

1 个答案:

答案 0 :(得分:0)

你看不到第二块代码的意图,尝试使用类似的东西:

String names = getIntent().getStringExtra("Name");

如果这不起作用,也许可以尝试对你的inital i.putExtra进行硬编码,以便仔细检查数据是否通过非空。

i.putExtra("Name","Test Name");

希望有所帮助