Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get values from the ArrayList for DetailView?
    primarykey
    data
    text
    <p>i'm new to android development. i'm working on the app which using SAX parser for parsing XML file i'm getting the result and displaying them on the ListView but i'm unable to get value for detail view. Like i want when user click on particular row then that particular row display the DetailActivity.</p> <p>i'm using this</p> <blockquote> <p>onItemClick()</p> </blockquote> <p>In my code i'm getting the values from XML and save them in the ArrayList </p> <blockquote> <p>ArrayList title = new ArrayList();</p> </blockquote> <p>able to display this title's in the ListView but how to show this same title in the DetailView also.</p> <p>Here is my code below:-</p> <p>MainActivity.java</p> <pre><code>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 ========&gt;"+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&lt;String&gt; listItem= itemList.getTitle(); System.out.println("(ListItem)=======&gt;"+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&lt;?&gt; 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); } }); } } </code></pre> <p>ItemList.java</p> <pre><code>public class ItemList { ArrayList&lt;String&gt; title = new ArrayList&lt;String&gt;(); public ArrayList&lt;String&gt; getTitle() { return title; } public void setTitle(String title) { this.title.add(title); } } </code></pre> <p>XMLParser.java ( In which i had made HTTPRequest only)</p> <pre><code>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; } } </code></pre> <p>XMLHandler.java</p> <pre><code>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; } } } </code></pre> <p>myAdapter.java</p> <pre><code>public class myAdapter extends BaseAdapter { ArrayList&lt;String&gt; listTitle; Activity activity; public myAdapter(Activity activity, ArrayList&lt;String&gt; 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; } } </code></pre> <p>DetailClassJi.java</p> <pre><code>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); } } </code></pre> <p>Can anyone please sugget me how i'll be able to show title also on detail view. Thanks in Advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload