0

我有一个问题,从 listView 在相同的选定位置获取一个来自 arraylist 并将这两个值传递给 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();

        }
4

1 回答 1

0

您看不到第二个代码块的意图,请尝试使用以下内容:

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

如果这不起作用,则可以尝试对您的初始 i.putExtra 进行硬编码,以仔细检查发送的数据是否为空。

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

希望能帮助到你

于 2012-07-24T11:39:54.013 回答