0

I've been looking tutorial to tutorial for Android/Java ways to create a ListView with dynamic entries. I'm currently trying to get a String into each iteration of the ListView. I need someone to tell me what's wrong with my class. The code itself is running all the way to the page with the ListViews, but it doesn't stake anything towards filling the ListViews. If someone whose word is good on the Android app programming, and knows this question's answer please contribute to the answers, below.

package com.taxiez.app;

import android.app.Activity;
import android.icu.util.Currency;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.activity.ComponentActivity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import io.grpc.Context;

import static com.taxiez.app.Items.getCurrencyLocaleMap;
import static com.taxiez.app.R.layout.activity_lists;

public class lists extends AppCompatActivity {

    ListView listViewP;
    ListView listViewB;
    ArrayList<String> mpTitle = getInfo( getFilesDir() + "/tax_personal.xml" );
    ArrayList<String> mbTitle = getInfo( getFilesDir() + "/tax_business.xml" );

    public lists() throws ParserConfigurationException, SAXException, ParseException, IOException {
    }
    // so our images and other things are set in array

    // now paste some images in drawable

    public static ComponentActivity newComponentActivity() {
        return new androidx.activity.ComponentActivity( R.layout.content_search );
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_search);

        listViewP = (ListView) findViewById(R.id.view_personal);
        listViewB = (ListView) findViewById(R.id.view_business);
        // now create an adapter class

        MyAdapter adapterP = new MyAdapter(this, mpTitle);
        listViewP.setAdapter(adapterP);
        MyAdapter adapterB = new MyAdapter(this, mbTitle);
        listViewB.setAdapter(adapterB);
        // there is my mistake...
        // now again check this..
        //for (int i = 0; i < mpTitle.size() ; i++)
        {
            adapterP.addAll( mpTitle );
            adapterB.addAll( mbTitle );
        }
        // now set item click on list view
    }

    class MyAdapter extends ArrayAdapter<String> {

        android.content.Context context;
        ArrayList<String> rTitle;

        MyAdapter (android.content.Context c, ArrayList<String> title) {
            super(c, R.layout.content_search, R.id.textView1, title);
            this.context = c;
            this.rTitle = title;

        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            View row = layoutInflater.inflate(R.layout.listed_views, parent, false);
            TextView myTitle = row.findViewById(R.id.textView1);

            myTitle.setText(rTitle.get(position));
            return row;
        }
    }

    public ArrayList<String> getInfo ( String file ) throws ParserConfigurationException, ParseException, IOException, SAXException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        ArrayList<String> biz = null;
        DocumentBuilder db = dbf.newDocumentBuilder();
        Reader reader = new FileReader( file );
        InputSource sax = new InputSource();
        sax.setCharacterStream( reader );

        Document dom = db.parse( sax );

        Element docEle = dom.getDocumentElement();

        NodeList nl = docEle.getChildNodes();

        if (nl != null) {
            int length = nl.getLength();
            for (int i = 0; i < length; i++) {
                if (nl.item( i ).getNodeType() == Node.ELEMENT_NODE) {
                    Element el = (Element) nl.item( i );
                    //Toast.makeText( this, el.getTagName(), Toast.LENGTH_SHORT ).show();
                    if (el.getNodeName().contains( "info" )) {
                        String Date = el.getElementsByTagName( "Date" ).item( 0 ).getTextContent();
                        String Donate = el.getElementsByTagName( "Donate" ).item( 0 ).getTextContent();
                        String Seller = el.getElementsByTagName( "Seller" ).item( 0 ).getTextContent();
                        String StoreNo = el.getElementsByTagName( "Store" ).item( 0 ).getTextContent();
                        String RcptNo = el.getElementsByTagName( "ReceiptNo" ).item( 0 ).getTextContent();
                        String Item = el.getElementsByTagName( "Item" ).item( 0 ).getTextContent();
                        String ItemQuantity = el.getElementsByTagName( "ItemQuantity" ).item( 0 ).getTextContent();
                        String ItemPrice = el.getElementsByTagName( "ItemPrice" ).item( 0 ).getTextContent();
                        String description = el.getElementsByTagName( "Description" ).item( 0 ).getTextContent();
                        String taxAmount = el.getElementsByTagName( "TaxAmount" ).item( 0 ).getTextContent();
                        String totalAmount = el.getElementsByTagName( "TotalAmount" ).item( 0 ).getTextContent();
                        String income = el.getElementsByTagName( "Income" ).item( 0 ).getTextContent();
                        String method = el.getElementsByTagName( "Method" ).item( 0 ).getTextContent();
                        String now = el.getElementsByTagName( "Now" ).item( 0 ).getTextContent();

                        Items items = new Items(method, Seller, Item, description,ItemQuantity,ItemPrice,totalAmount,taxAmount,Date);
                        // CREATE STRINGS
                        String v = items.listString( items );
                        biz.add( 0, v );
                        //v.add( multi );

                    }
                }
            }
        }
        return biz;
    }
}
thexiv
  • 27
  • 9

2 Answers2

0

This is one way you can a achieve dynamic Listview Click here!

0

I think here is the issue:

ArrayList<String> biz = null;

And you are adding [biz.add( 0, v );] on a null reference!

Change the initial value to new ArrayList<>();

ArrayList<String> biz = new ArrayList<>();