I\'m following this tutorial.
It works great but I would like it to return an array with all the strings instead of a single string with the last element.
An
public class MainActivity extends AppCompatActivity {
ListView lvPcsPost;
ArrayList name;
ArrayList price;
ArrayList Description;
LayoutInflater layoutInflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvPcsPost = (ListView) findViewById(R.id.lvPcsPost);
name = new ArrayList<>();
price = new ArrayList<>();
Description = new ArrayList<>();
new PostAsync().execute();
}
class PostAsync extends AsyncTask {
ProgressDialog pd;
XMLHelper helper;
@Override
protected void onPreExecute() {
pd = ProgressDialog.show(MainActivity.this, "", "Loading...", true, false);
}
@Override
protected Void doInBackground(Void... arg0) {
helper = new XMLHelper();
helper.get();
return null;
}
@Override
protected void onPostExecute(Void result) {
PostBaseAdapter postBaseAdapter = new PostBaseAdapter();
lvPcsPost.setAdapter(postBaseAdapter);
pd.dismiss();
}
}
public class XMLHelper extends DefaultHandler {
private String URL_MAIN = "http://uat.winitsoftware.com/ThemeManager/Data/Products/Products.xml";
String TAG = "XMLHelper";
Boolean currTag = false;
String currTagVal = "";
public void get() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser mSaxParser = factory.newSAXParser();
XMLReader mXmlReader = mSaxParser.getXMLReader();
mXmlReader.setContentHandler(this);
InputStream mInputStream = new URL(URL_MAIN).openStream();
mXmlReader.parse(new InputSource(mInputStream));
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currTag) {
currTagVal = currTagVal + new String(ch, start, length);
currTag = false;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currTag = false;
if (localName.equalsIgnoreCase("Name"))
name.add(currTagVal);
else if (localName.equalsIgnoreCase("Description"))
Description.add(currTagVal);
else if (localName.equalsIgnoreCase("Price"))
price.add(currTagVal);
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
Log.i(TAG, "TAG: " + localName);
currTag = true;
currTagVal = "";
if (localName.equals("Products"));
}
}
public class PostBaseAdapter extends BaseAdapter {
public PostBaseAdapter() {
}
@Override
public int getCount() {
return name.size();
}
@Override
public Object getItem(int position) {
return name.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
layoutInflater = LayoutInflater.from(getApplicationContext());
convertView = layoutInflater.inflate(R.layout.list_item_post, parent, false);
TextView txtPrice = (TextView) convertView.findViewById(R.id.txtPrice);
TextView txtDescription = (TextView) convertView.findViewById(R.id.txtDescription);
TextView txtName = (TextView) convertView.findViewById(R.id.txtName);
ImageView image = (ImageView) convertView.findViewById(R.id.Image);
ImageView bigImage = (ImageView) convertView.findViewById(R.id.BigImage);
txtPrice.setText("Price : "+price.get(position));
txtDescription.setText("Description : "+Description.get(position));
txtName.setText("Name : "+name.get(position));
return convertView;
}
}
}