我是Android开发的新手。我正在使用SAX解析器解析XML文件的应用程序我得到了结果并在ListView上显示它们但我无法获得详细信息视图的值。就像我想要用户点击特定行时那样,该特定行显示DetailActivity。
我正在使用这个
onItemClick()
在我的代码中,我从XML获取值并将它们保存在ArrayList
中ArrayList title = new ArrayList();
能够在ListView中显示此标题,但如何在DetailView中显示相同的标题。
以下是我的代码: -
MainActivity.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);
}
});
}
}
ItemList.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;
}
}
}
myAdapter.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);
}
}
任何人都可以请我告诉我如何能够在详细视图上显示标题。 在此先感谢
答案 0 :(得分:0)
http://www.java2s.com/Open-Source/Android/UnTagged/feemaster/com/fee/master/ui/DetailView.java.htm
希望这会有所帮助......
答案 1 :(得分:0)
进行以下更改我希望它能够正常工作..
在getView(...)
中添加以下语句title.txtViewTitle.setText(listTitle.get(position));
title.setTxtViewTitle(listTitle.get(position));
更改OnItemClickListener
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);
}
});
在View holder中添加get方法
class ViewHolder
{
TextView txtViewTitle;
public String getTxtViewTitle(){
return txtViewTitle;
}
public void setTxtViewTitle(String txtViewTitle){
this.txtViewTitle = txtViewTitle;
}
}
最终在DetailsActivity中获得价值,如
getIntent().getExtras().getString("title")