android xml node parse array adapter weird - java

I have an odd problem, (see shorter version at the bottom first please)
When my activity starts for the first time, the listview shows the dataset fine. The adapter is a custom adapter which shows 2 rows of text and an image. I call an asynctask upon a click event to the listview, the dataset updates in accordance with whatever was clicked on in the listview - more specifically the arrays which are associated with the adapter become rewritten with the parsings of some xml, and then notifyachapterdatasetchanged method is called to update the listview in the onPostExecute function. However I always get NullPointerException when I am iterating through some xml (which is very well formed and validates). ALso its worth mentioing that the algorithm that parses the desired values is good, because as mentioned above, if i write to just 1 element of the array then I dont get the error I just get the last node value so its looping in the correct places. i.e If I simple try to copy the current node value I am parsing during the loop into, say, producyTypes[0] then the last value from within the loop actually makes it to the listview as it constantly overwrites this elemet of the array
Here is my code.`
public class main extends Activity implements OnItemClickListener
{
String selectedType="";
ListView lview3;
ImageView iv;
ListViewCustomAdapter adapter;
String test = "";
List<String> ls;
String productTypes[] = {"Monitor","Components","Systems","Laptops","Flash / Usb Memory",
"Networking","Cables","Peripherals","Sound and Vision", "Software"};
String productsIncluded[] = {"Sizes (inches) 17, 19, 20",
"Motherboards, PSU, Cases, Fans/Heatsinks/Coolers, Barebones Systems, Blue-Ray/DVD. Card Readers, Controller Cards, Drive Enclosures, Sound Cards",
"Bundles, Switches and Hubs, Print Servers, Accessories/Modules",
"Cables for: Drives, Networking, HDMI/Monitor, Audio/Video, USB/Firewire, Power, Miscellaneous",
"Mice, Connectors, Bluetooth Devices",
"Mp3/Mp4 Solar Panel",
"Anti-Virus, Internet Security, Operating Systems, Office,,
"",
""};
private static int images[] = {R.drawable.monitor, R.drawable.components, R.drawable.systems,
R.drawable.laptops, R.drawable.flashusb, R.drawable.networking, R.drawable.cables, R.drawable.
peripherals, R.drawable.soundandvision, R.drawable.software};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView)findViewById(R.id.ImageView01);
iv.setImageResource(R.drawable.logo);
iv.setVisibility(View.VISIBLE);
lview3 = (ListView) findViewById(R.id.listView3);
adapter = new ListViewCustomAdapter(main.this, productTypes, productsIncluded, images);
lview3.setAdapter(adapter);
lview3.setOnItemClickListener(main.this);
}
#Override
public void onItemClick(AdapterView arg0, View arg1, int position, long id) {
selectedType = "";
if(position == 0)
{
selectedType= "Monitors";
}
if(position == 1)
{
selectedType= "Hard Drives";
}
Toast.makeText(this, Integer.toString(position), Toast.LENGTH_SHORT).show();
new async().execute();
/*
Intent intent = new Intent(getApplicationContext(),activitywo.class);
Bundle bundle =new Bundle();
bundle.putString("selectedType",selectedType);
intent.putExtras(bundle);
startActivity(intent);
//Toast.makeText(this, "Title => "+productTypes[position]+" \n Description => "+productsIncluded[position], Toast.LENGTH_SHORT).show();
*/
}
private class async extends AsyncTask<String, Void, Void> {
// UI Thread
protected void onPreExecute() {
}
// automatically done on worker thread (separate from UI thread)
protected Void doInBackground(final String... args) {
try{
Resources res = getResources();
InputStream in;
in = res.openRawResource(R.raw.core);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(in);
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("entry");
count=0;
for(int i =0;i<items.getLength();i++){
Node item = items.item(i);
NodeList properties = item.getChildNodes();
//productsDefinedByTypeArray = new String[properties.getLength()];
for(int j = 0;j<properties.getLength();j++){
Node property = properties.item(j);
String name = property.getNodeName();
if(name.equalsIgnoreCase("g:product_type")){//everytime we hit g:product_type grab the value
String strText = property.getFirstChild().getNodeValue();
if(strText.contains(selectedType)){
//
for(int k = 0;k<properties.getLength();k++){
Node propertysecond = properties.item(k);
String namesecond = propertysecond.getNodeName();
if(namesecond.equalsIgnoreCase("title")){//everytime we hit title grab the value
String strTextsecond = propertysecond.getFirstChild().getNodeValue();
productTypes[0] = strTextsecond;
yo = strTextsecond;
count++;
}
}
}
}
}
}
The code crashes at the point where I am trying to copy the value of a "title" node that I parse out of my xml file into the list-String-. I am using a list-string- to show that even if you try and copy the value into the array itself (the array that is associated with the adapter), even if you comment out the notifydatasetchanged () line the program still crashes. The xml is well formed and very consistent. I know this because (aecept the fact its really small and I have read it all) ! Why can I not copy every node value into my array/list whilst the loop is in progress?
Any help is massively appreciated. Thank you.
Shorter version:
I cannot write to the List
if(strText.contains(selectedType)){
//
for(int k = 0;k<properties.getLength();k++){
property = properties.item(k);
name = property.getNodeName();
if(name.equalsIgnoreCase("title")){//everytime we hit title grab the value
String strTextsecond = property.getFirstChild().getNodeValue().toString();
ls.add(strTextsecond.toString());
//test = strTextsecond;
}
}
}

Maybe you forget initialize ls? I don't find in your code something like:
ls = new List<String>();

Related

Custom Autocomplete Adapter Android

I wish to have a auto-complete text-box which comes up with the users contact names. My code is as follows.
private void getContactNames()
{
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
_contactAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line);
while (cursor.moveToNext())
{
int nameIdx = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
String tmp = cursor.getString(nameIdx);
_contactAdapter.add(tmp);
}
}
Setting the adapter:
AutoCompleteTextView contactName = (AutoCompleteTextView) findViewById(R.id.contactName);
contactName.setAdapter(_contactAdapter);
When I do this, the adapter has all the contact names in there (238 contacts). However, when I start typing into the text box, the auto complete does not show.
Funny, as when I test it out doing this:
String[] ab = new String[] {"aaaaa", "bbbbb"};
_contactAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,ab);
it will show "aaaaa" and "bbbbb" when typing in to the text box.
Any ideas?
Thanks,
Tom
*EDIT
Just thought I would follow up. It does seem to be the sheer amount of contacts that is preventing it from appearing. Any way to get around this?
while (cursor.moveToNext())
{
int nameIdx = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
String tmp = cursor.getString(nameIdx);
//_contactAdapter.add(tmp);
// get all names in a new arraylist and then assign it to
arrayList.add(tmp);
}
and then assign it to your adapter as
_contactAdapter = new ArrayAdapter<String> this,android.R.layout.simple_dropdown_item_1line, arrayList);

Android: IndexOutOfBounds Error when deleting row in ListView

I have an application that has 2 screens. The first screen has a ListView of movies with a row consisting of 3 Elements: Title, Date and Gross declared in strings.xml. The user has the option of adding a movie by clicking the menu button, which sends him to another screen. The second screen has 3 Edit texts that correspond to Title Date and Gross, which is alphabetically sorted straight away when it returns to screen 1.
Similarly, the user can also Edit/Delete entries by long clicking a row thatbrings up a context menu. The Edit function works like this:
a.) User long clicks Titanic and chooses Edit
b.) Row gets deleted, and user is brought to screen 2
c.) Edit texts are populated with the initial data from the deleted Row
d.) When user edits data, new movie is added at the bottom of the ListView.
The problem arises when the user deletes this new movie at the bottom of the ListView. Logcat gives a
java.lang.IndexOutOfBoundsException: Invalid index 50, size is 50
Here is my code (Take note I am using Perst to persist data, but I don;t think that won't really matter with my problem):
case R.id.contextedit:
Lab9_082588FetchDetails row = (Lab9_082588FetchDetails) getListView()
.getItemAtPosition(info.position);
Intent editData = new Intent(MovieList.this, Lab9_082588Edit.class);
String startTitle = row.getTitle();
String startGross = row.getGross();
String startDate = row.getDate();
editData.putExtra(Lab9_082588Edit.TITLE_STRING, startTitle);
editData.putExtra(Lab9_082588Edit.GROSS_STRING, startGross);
editData.putExtra(Lab9_082588Edit.DATE_STRING, startDate);
startActivityForResult(editData, MovieList.EDIT_MOVIE);
int posEdit = info.position;
String editTitle = results.get(info.position).getTitle();
results.remove(posEdit);
adapter.notifyDataSetChanged();
//Perst
Index<Lab9_082588FetchDetails> rootEdit = (Index<Lab9_082588FetchDetails>) db
.getRoot();
rootEdit.remove(editTitle, results.get((int) info.id));
db.setRoot(rootEdit);
return true;
Edit Class:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection using item.getItemId()
switch (item.getItemId()) {
case R.id.edit:
next();
break;
}
return true;
}
private void next() {
// TODO Auto-generated method stub
EditText movieTitle = (EditText) findViewById(R.id.etTitle);
EditText movieGross = (EditText) findViewById(R.id.etGross);
EditText movieDate = (EditText) findViewById(R.id.etDate);
String title = movieTitle.getText().toString();
String gross = movieGross.getText().toString();
String date = movieDate.getText().toString();
if ((title.length() > 0) && (gross.length() > 0)
&& (date.length() == 4)) {
Intent hobby = getIntent();
hobby.putExtra(Lab9_082588Edit.TITLE_STRING, title);
hobby.putExtra(Lab9_082588Edit.GROSS_STRING, gross);
hobby.putExtra(Lab9_082588Edit.DATE_STRING, date);
setResult(RESULT_OK, hobby);
finish();
}
}
Delete function:
int posDelete = info.position;
String deleteTitle = results.get(
info.position).getTitle();
results.remove(posDelete);
adapter.notifyDataSetChanged();
Index<Lab9_082588FetchDetails> rootDelete = (Index<Lab9_082588FetchDetails>) db
.getRoot();
rootDelete.remove(deleteTitle,
results.get(info.position));
db.setRoot(rootDelete); //Perst
return;
OnActivityResult (Edit):
case EDIT_MOVIE:
Lab9_082588FetchDetails edittedMovie = new Lab9_082588FetchDetails();
NumberFormat formatterEdit = new DecimalFormat("###,###,###");
edittedMovie.setTitle(data
.getStringExtra(Lab9_082588Add.TITLE_STRING));
edittedMovie.setGross("$"
+ formatterEdit.format(Double.parseDouble(data
.getStringExtra(Lab9_082588Add.GROSS_STRING))));
edittedMovie.setDate(data
.getStringExtra(Lab9_082588Add.DATE_STRING));
results.add(edittedMovie);
adapter.notifyDataSetChanged();
Populating the Listview:
for (int i = 0; i < items.size(); i++) {
Lab9_082588FetchDetails sr = new Lab9_082588FetchDetails();
sr.setTitle(items.get(i).getTitle());
sr.setGross(items.get(i).getGross());
sr.setDate(items.get(i).getDate());
results.add(sr);
Collections.sort(results, ignoreCaseStart);
}
How do I remedy this?
This problem occurs because in your delete function, you first remove the element from the results collection("results.remove(posDelete);"), and then, a few lines later, you call "results.get(info.position)" to fetch a parameter for the rootDelete.remove call, but which is already removed.
If the element is the last element of your collection, let's say the 50th element, the value for "info.position" is 50. You remove one element, so the number of elements is now 49. In the rootDelete.remove line you call results.get(50), which produces the error.

Showing text in Android ListAdapter on this specific case

I am trying to build a simple TV-Guide for Android. To make that, I am using RSS from one site. I parse XML and now, I'd like to show it. The idea is a List that displays Station, Whats currently on the show and date, and when I press on some item in the list it will give me a new Activity that shows the full schedule of some TV station. I've managed to separate parts of Whole TV program in just separate Strings (like: 06:00 News; 07:15 Movie). And I even managed to separate it so it fills String[][] (like: |06|,|00|,|News| ; |07|,|15|,|Movie|). And my Idea was to check which one is closest to real time and to display it.
Well, currently my App displays TV Station, WHOLE program (schedule), and Date. And I want it to display just whats currently showed. Can anybody help me with this one?
Here is class in which I want to do it: (I also have Parser class and Main class with just buttons and URLs of XMLs)
public class Pomocna extends ListActivity {
// All static variables
static String url =null;
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_DATE = "pubDate";
static final String KEY_DESC = "encoded";
Calendar calendar = new GregorianCalendar();
int DSQ=calendar.get(Calendar.HOUR_OF_DAY);
int DMQ=calendar.get(Calendar.MINUTE);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent in = getIntent();
// Get XML values from previous intent
url = in.getStringExtra("A");
ArrayList<HashMap<String,String>> menuItems = new ArrayList<HashMap<String,String>>();
//ArrayList<String[][]> nekaj = new ArrayList<String[][]>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(url); //get XML
Document doc = parser.getDomElement(xml); // get DOM elem.
//doc.getDocumentElement().normalize();
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
//loop
for (int i=0; i< nl.getLength(); i++){
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
//add to map
map.put(KEY_TITLE, Vracanje1(parser.getValue(e, KEY_TITLE)));
map.put(KEY_DATE, Vracanje2(parser.getValue(e, KEY_DATE)));
map.put(KEY_DESC, parser.getValue3(e,KEY_DESC));
//NEKO FRCERANJE -=- POCETAK
String pvp = parser.getValue3(e,KEY_DESC);
char[] rast = pvp.toCharArray();
int brojac = 0;
for(int q=0; q<pvp.length(); q++){
if(rast[q]=='*') brojac++;
}
String[][] niz= new String[brojac][3];
int bf1=0;
int bf3=0;
int oo=0;
for(int q=0; q<pvp.length(); q++){
if(rast[q]=='*'){
bf3=bf1;
bf1=q;
String lol = pvp.substring(bf3, bf1);
// SEPARATE STRING: LOL
// IT MUST HAVE 3 PARTS: HOUR, MINUTE, AND DESCRIPTION
if(oo==0){
String ps=lol.substring(0,2);
String ds=lol.substring(3,5);
String ts=lol.substring(6,lol.length());
niz[oo][0]=ps;
niz[oo][1]=ds;
niz[oo][2]=ts;
}
if(oo>0){
String ps=lol.substring(1,3);
String ds=lol.substring(4,6);
String ts=lol.substring(7,lol.length());
niz[oo][0]=ps;
niz[oo][1]=ds;
niz[oo][2]=ts;
}
oo++;
}
}
//NEKO FRCERANJE -=- KRAJ
menuItems.add(map);
//nekaj.add(niz);
}
//DISPLAY WHAT's CURRENTLY ON PROGRAM
//USE TIME TO COMPARE AND SHOW
ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.list_item,
new String[]{KEY_TITLE, KEY_DESC, KEY_DATE}, new int[]{
R.id.title, R.id.description, R.id.date
});
setListAdapter(adapter);
//singleView
ListView lv = getListView();
}
public String Vracanje1(String bq){
int p1=bq.length();
int p2=p1-10;
StringBuilder sbp= new StringBuilder(bq);
sbp.delete(p2, p1);
String ses=sbp.toString();
return ses;
}
public String Vracanje2(String bq){
int p1=bq.length();
int p2=p1-14;
StringBuilder sbp=new StringBuilder(bq);
sbp.delete(p2, p1);
String ses = sbp.toString();
return ses;
}
}
Per my understanding the way You're doing won't be acceptable to any TV-program application, because usually program has many channels with many programs and parsing and analysing the data in UI thread will (sooner or later) cause ANRs. I would suggest You to consider use of separate service and content provider to let UI thread do only UI-related stuff and leave data processing to corresponding components. It would be faster and easier to find/process data via sqlite in provider.
Regarding a question about finding closest data - You could sort arrays and when find closest one (e.g. just iterating from the beginning).
I agree with #sandstar, but I'm not sure if what you're asking is how to filter the data or how to show the data? In this case it is the matter of updating the ListAdapter. This means, when you want to show only the most recent program, you need to provide the adapter a list of maps containing only the map with the data for the latest show. This means you should filter the data before applying the adapter to the ListView. If you want to have more control over the adapter you might consider extending the ArrayAdapter which also allows you to add/clear the data of the existing adapter.

Android crashes when try to overwrite TextViews

I've created a timetable app, which allows the user to enter data and then view it.However if the user enters an entry, on a day and time where there already is one, my emulator crashes(forces a close).
Basically I'm pulling back data to a linear layout- which contains 10 TextViews, each representing the times 9-15.
Here's the code:
public void addMondayGrid() {
// TODO Auto-generated method stub
for (int index = 0; index < info.mon.size(); index++) {
// int entryId = info.monIds.get(index);
int time = info.monTimes.get(index);
int id = info.monIds.get(index);
int duration = info.monDuration.get(index);
String dayOfWeek = "mon";
timeId = getResources().getIdentifier("mon" + time, "id",
getPackageName());
if (duration == 1) {
SpannableString text = new SpannableString(info.mon.get(index));
setEntryColour(text);
final TextView textV = (TextView) findViewById(timeId);
textV.setTextSize(10);
textV.setText(text, BufferType.SPANNABLE);
textV.setId(id);
deleteGridEntry(textV);
} else {
longerDuration(time, index, id, info.mon, dayOfWeek);
}
}
}
The thing is is works fine as long as there isn't two entries for the same day and time, eg. monday at 9 oclock.
Anyone have any ideas?I'm quite new to this and any help would be much appreciated!
I have to reference the id this way as there are too many ids to reference any other way,is there not a simple way to overwrite the old textView with new data pulled back from the database? I want the id to be the same one as that is the textView I want to deal with, but it just keeps crashing, is it something to do with instances?
change:
final TextView textV = (TextView) findViewById(timeId);
to:
final TextView textV = (TextView) findViewById(R.id.timeId);

Android Updating ListView

I have looked at Android - how to update ListView item that is currently displayed and http://commonsware.com/Android/excerpt.pdf and the Android documentation but I still don't understand.
My problem:
Using a handler, I am trying to update a Stock data multi-column listview which I have populated from a webservice which retrieves data from a MySQL database.
To update the listview, I am calling a servlet which returns an XML that I loop through using DOM.
I cannot find a working way to apply the new data (from the XML) into the Listview, though only the third column (Trade Column) has to be updated. Also when I try to cast a View from a ListView row, I get a NullPointerException and can't figure out why.
The code I have done so far is below.
java Code:
private void updateUI() throws Exception
{
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String curTime = hours + ":" + minutes + ":"+ seconds;
refreshHandler.sleep(60000);
ListView listview = (ListView) findViewById(R.id.listview);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("http://10.0.0.29:8080/CI3500/FTSEXML");
//Filter and store ALL 'update' XML elements into node array
NodeList nodeList = doc.getElementsByTagName("update");
View v = null;
TextView t = null;
Adapter adapter = listview.getAdapter();
for (int i = 0; i < adapter.getCount(); i++)
{
v = listview.getChildAt(i);
t = (TextView)v.findViewById(R.id.item2);
String companyCode = t.getText().toString(); //Column 1
for(int j = 0; j < nodeList.getLength(); j++)
{
if(companyCode == nodeList.item(j).getFirstChild().getNodeValue())
{
//TODO Update Listview Code
}
}
}
txtStatus.setText(String.valueOf("Last Update: " + curTime));
}
The listview mapping is as follows:
// create the grid item mapping
String[] columns = new String[] {"col_1", "col_2", "col_3" };
int[] rows = new int[] { R.id.CodeColumn, R.id.NameColumn, R.id.TradeColumn };
You should implement you own ListView Adapter that will provide data to the list view. Calling notifyDataSetChanged() from adapter will force list view to fetch data from the adapter. Updating list view views directly looks strange.
You can call invalidate to let the listview redraw.

Categories

Resources