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