English 中文(简体)
如何从ArrayList for DetailView中获取价值?
原标题:How to get values from the ArrayList for DetailView?

i m 新的甲状腺发育。 i m 工作在XML文档中使用SAX par子的 app子上,一米可取得结果,并在名单上显示结果,但一米无法得到详细观察。 同用户点击特定行时一样,该行显示尾矿。

i m

缩略语

在我的法典中,一把价值从XML中获取,并在ArrayList中予以节省。

履历:

能够在清单中显示这一标题,但如何在《详细意见》中显示同一标题。

我的法典如下:

MainActative.java

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    static final String URL = "http://www.xyz.com/api.php?page_id=1";

    ItemList itemList;

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

        XMLParser parser = new XMLParser();
        String XML = parser.getXmlFromUrl(URL);

        System.out.println("This XML is ========>"+XML);

       try
       {
           SAXParserFactory spf = SAXParserFactory.newInstance();
           SAXParser sp = spf.newSAXParser();
           XMLReader xr = sp.getXMLReader();

           /** Create handler to handle XML Tags ( extends DefaultHandler ) */
           MyXMLHandler myXMLHandler = new MyXMLHandler();
           xr.setContentHandler(myXMLHandler);

           ByteArrayInputStream is = new ByteArrayInputStream(XML.getBytes());
           xr.parse(new InputSource(is));
      }
      catch(Exception e)
      {

      }

      itemList = MyXMLHandler.itemList;

       ArrayList<String> listItem= itemList.getTitle();

    System.out.println("(ListItem)=======>"+listItem);


     ListView lview = (ListView) findViewById(R.id.listview1);
     myAdapter adapter = new myAdapter(this, listItem);
     lview.setAdapter(adapter);


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


            // Starting new intent
             Intent in = new Intent(MainActivity.this, DetailClassJI.class);


             myAdapter ma = (myAdapter)parent.getAdapter();



         startActivity(in);
         }
    });
    }
}

项目List.java

public class ItemList 
{
    ArrayList<String> title = new ArrayList<String>();


    public ArrayList<String> getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title.add(title);
    }

}

XMLParser.java (一) 仅提出HTTPRequest

public class XMLParser {

    // constructor
    public XMLParser() {

    }

    /**
     * Getting XML from URL making HTTP request
     * @param url string
     * */
    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }
}

XMLHandler.java

public class MyXMLHandler extends DefaultHandler
{
    public static ItemList itemList;
    public boolean current = false;
    public String currentValue = null;

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub

        current = true;

        if (localName.equals("posts"))
        {
            /** Start */ 
            itemList = new ItemList();

        } 
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        // TODO Auto-generated method stub
        current = false;

        if(localName.equals("title"))
        {
            itemList.setTitle(currentValue);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        // TODO Auto-generated method stub

        if(current)
        {
            currentValue = new String(ch, start, length);
            current=false;
        }
    }
}

我的Adapter.java

public class myAdapter extends BaseAdapter 
{
    ArrayList<String> listTitle;


    Activity activity;

    public myAdapter(Activity activity, ArrayList<String> listTitle) {
        super();
        this.listTitle = listTitle;


        this.activity = activity;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return listTitle.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    private class ViewHolder
    {
        TextView txtViewTitle;

    }

    public View getView(int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub

        ViewHolder title;
        LayoutInflater inflater = activity.getLayoutInflater();

        if(view==null)
        {
            view = inflater.inflate(R.layout.lview_row, null);
            title = new ViewHolder();

            title.txtViewTitle = (TextView) view.findViewById(R.id.txtItem);

            view.setTag(title);
        }
        else
        {
            title = (ViewHolder) view.getTag();
        }

        title.txtViewTitle.setText(listTitle.get(position));


        return view;
    }
}

DetailClassJi.java

public class DetailClassJI extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);

     // getting intent data
        Intent in = getIntent();


     // Displaying all values on the screen
        TextView lblName = (TextView) findViewById(R.id.txtItem);

    }
}

Can anyone please sugget me how i ll be able to show title also on detail view. Thanks in Advance

问题回答

我希望,它将开展工作。

加入:

title.txtViewTitle.setText(listTitle.get(position));
title.setTxtViewTitle(listTitle.get(position));

2. 对ItemClickListener的改动

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


             ViewHolder holder = (ViewHolder) view.getTag();



            // Starting new intent
             Intent in = new Intent(MainActivity.this, DetailClassJI.class);
             in.putExtra("title", holder.getTxtViewTitle());
                startActivity(intent);

             myAdapter ma = (myAdapter)parent.getAdapter();



         startActivity(in);
         }
    });

添加“观点持有人的方法”

 class ViewHolder
        {
            TextView txtViewTitle;

               public String getTxtViewTitle(){
                   return txtViewTitle;
           }
           public void setTxtViewTitle(String txtViewTitle){
                   this.txtViewTitle = txtViewTitle;
           }

        }

最后获得细节价值 活动

getIntent().getExtras().getString("title")




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签