Listview show just one item when it give data from xml



I have List view and it take data from xml but it tack just a specific xml tag not all of them.when I click on a list item it go to another page but I have same problem with new page too.how should I fix it?


my list view activity is here:(my list just show PatientId)



public class ViewAllPatientActivity extends ListActivity {
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser=new XMLParser();
static final String URL="http://ift.tt/1zc4Xp4";
static final String Parent_Node="Patient";
static final String Patient_ID="PatientId";
static final String First_Name="PatientFirstName";
static final String Last_Name="PatientLastName";
//static final String Blood_Type="BloodType";
//static final String Phone_Number="phoneNumber";
//static final String Practitioner_ID="practitionerId";
//static final String Email="Email";
//static final String URL="http://ift.tt/1l4gTGN";
//static final String Patient_ID="item";
//static final String First_Name="name";
//static final String Last_Name="cost";

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.all_patient);



String xml=parser.GetXml(URL);
Document doc=parser.DomElement(xml);
if(doc==null){
System.out.print("null");
}
else {

NodeList nl = doc.getElementsByTagName(Parent_Node);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(Patient_ID, parser.GetValue(e, Patient_ID));
map.put(First_Name, parser.GetValue(e, First_Name));
map.put(Last_Name, parser.GetValue(e, Last_Name));
// map.put(Blood_Type,parser.GetValue(e,Blood_Type));
//map.put(Phone_Number,parser.GetValue(e,Phone_Number));
//map.put(Email,parser.GetValue(e,Email));
//map.put(Practitioner_ID,parser.GetValue(e,Practitioner_ID));

menuItems.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.list_item,
new String[]{Patient_ID, First_Name, Last_Name}, new int[]{R.id.pid, R.id.fName, R.id.lName});
setListAdapter(adapter);

ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String Patient_id = ((TextView) view.findViewById(R.id.pid)).getText().toString();
String First_name = ((TextView) view.findViewById(R.id.fName)).getText().toString();
String Last_name = ((TextView) view.findViewById(R.id.lName)).getText().toString();

Intent in = new Intent(getApplicationContext(), SingleListPatient.class);
in.putExtra(Patient_ID, Patient_id);
in.putExtra(First_Name, First_name);
in.putExtra(Last_Name, Last_name);
startActivity(in);
}
});
}


it is my list:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list">
</ListView>
</LinearLayout>


it is another activity:(in this page I have just FirstName)



public class SingleListPatient extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.single_list_patient_view);
TextView txtFN=(TextView)findViewById(R.id.firstN);
TextView txtLN=(TextView)findViewById(R.id.lastN);
TextView txtID=(TextView)findViewById(R.id.patientid);
TextView txtBT=(TextView)findViewById(R.id.btype);
TextView txtPN=(TextView)findViewById(R.id.pnum);
TextView txtEM=(TextView)findViewById(R.id.mail);

Intent i=getIntent();

String FirstName=i.getStringExtra("PatientFirstName");
String LastName=i.getStringExtra("PatientLastName");
String ID=i.getStringExtra("PatientId");
String BloodType=i.getStringExtra("BloodType");
String PhoneNumber=i.getStringExtra("phoneNumber");
String Email=i.getStringExtra("Email");

txtFN.setText(FirstName);
txtLN.setText(LastName);
txtID.setText(ID);
txtBT.setText(BloodType);
txtPN.setText(PhoneNumber);
txtEM.setText(Email);
}


and it is my list item:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://ift.tt/nIICcg"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal">
<TextView xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/pid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" />
<TextView xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/fName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" />
<TextView xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/lName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" />
</LinearLayout>


I have a xml file for each item:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:padding="10dp"
android:id="@+id/firstN"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:padding="10dp"
android:id="@+id/lastN"
android:layout_toRightOf="@+id/firstN"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:text="Patient ID: "
android:id="@+id/paId"
android:layout_below="@+id/firstN"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:id="@+id/patientid"
android:layout_toRightOf="@+id/paId"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:text="Blood Type: "
android:id="@+id/bt"
android:layout_below="@+id/paId"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:id="@+id/btype"
android:layout_toRightOf="@+id/bt"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:text="Phone Number: "
android:id="@+id/pn"
android:layout_below="@+id/bt"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:id="@+id/pnum"
android:layout_toRightOf="@+id/pn"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:text="Email: "
android:id="@+id/em"
android:layout_below="@+id/pn"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:padding="10dp"
android:id="@+id/mail"
android:layout_toRightOf="@+id/em"/>
</RelativeLayout>


and it is my Xml Parser class:



public class XMLParser {
//get xml from http url
public String GetXml(String url){
String xml=null;
try{
//open http connection via url
DefaultHttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(url);
HttpResponse httpResponse=httpClient.execute(httpPost);
HttpEntity httpEntity=httpResponse.getEntity();
//convert to a readable code or get xml content and close connection
xml= EntityUtils.toString(httpEntity);

}
catch (UnsupportedEncodingException e){

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

public Document DomElement(String xml){
Document doc = null;;
//factory API for DOM object trees for xml docs
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try{
//Dom doc instance
DocumentBuilder db=dbf.newDocumentBuilder();
//parse doc to xml
InputSource is=new InputSource();
//place entire files into database columns that can hold large values,
is.setCharacterStream(new StringReader(xml));
doc= db.parse(is);

}catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
//return DOM
return doc;
}

public String GetValue(Element item,String st){
//abstract node--give value of a node and put it on n
NodeList n=item.getElementsByTagName(st);
return this.getElementValue(n.item(0));

}

public final String getElementValue(Node element) {
Node child;
if (element !=null){
if (element.hasChildNodes()){
for (child=element.getFirstChild();child!=null;child=child.getNextSibling()){
if (child.getNodeType()==Node.TEXT_NODE){
return child.getNodeValue();
}
}
}
}
return "";
}
}


thank you.


No comments:

Post a Comment