Trying to display data read from a CSV file in a ListView

+1 vote
asked Sep 12, 2017 by mark-mitchell

I am very new to programming in android studio and would like to make an application I can use at work. The basic idea is to import a product list into the application from a CSV file and display it as a list view so I can bring up a related document for each one.

The program reads from the CSV file with no problems but when I try to use an adapter to view the data in a ListView, the adapter always says "Cannot resolve constructor". I have been searching for days to find an answer but I have given up. Can anybody shed some light?

MainActivity.java

    package com.example.mblapplication;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.nio.charset.Charset;
    import java.util.ArrayList;
    import java.util.List;

    import static com.example.mblapplication.R.id.ProdList1;

    public class MainActivity extends AppCompatActivity {

    public List<ProdSample> productSamples = new ArrayList<>();

    public void readProdData() {
    InputStream is = getResources().openRawResource(R.raw.products);
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(is, Charset.forName("UTF-8"))
    );
    String line = "";
    try {
        // Step over headers
        reader.readLine();
        while ((line = reader.readLine()) != null) {
            Log.d("MBLApplication", "Line is: " + line);

            // Split by ","
            String[] tokens = line.split(",");

            // Read the data
            ProdSample sample = new ProdSample();
            if (tokens[0].length() > 0) {
                sample.setSAP(tokens[0]);
            } else {
                sample.setSAP("");
            }
            sample.setProdID(tokens[1]);
            sample.setWidth(tokens[2]);
            sample.setLooms(tokens[3]);
            productSamples.add(sample);

            Log.d("MBLApplication", "Just created: " + sample);

        }
    } catch (IOException e) {
        Log.wtf("MBLApplication, Error reading file on line " + line, e);
        e.printStackTrace();
    }
}

ListView listView;
//List<String> listItems = new ArrayList<>(5);
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    readProdData();
    ArrayAdapter<String> productAdapter = new ArrayAdapter<String>
            (MainActivity.this, android.R.layout.simple_list_item_1, ProdList1, productSamples); //ERROR HERE
    //listItems.add(0, "Hello");
    //listItems.add(1, "World");

    ListView listView = (ListView) findViewById(R.id.ProdList1);
    listView.setAdapter(productAdapter);
      }
    }

ProdSample.class

    package com.example.mblapplication;

    class ProdSample {
    private String SAP;
    private String ProdID;
    private String Width;
    private String Looms;

    public String getSAP() {
    return SAP;
    }

    public void setSAP(String SAP) {
    this.SAP = SAP;
    }

    public String getProdID() {
    return ProdID;
    }

    public void setProdID(String prodID) {
    this.ProdID = prodID;
    }

    public String getWidth() {
    return Width;
    }

    public void setWidth(String width) {
    this.Width = width;
    }

    public String getLooms() {
    return Looms;
    }

    public void setLooms(String looms) {
    Looms = looms;
    }

    @Override
    public String toString() {
    return "ProdSample{" +
            "SAP='" + SAP + '\'' +
            ", ProdID=" + ProdID +
            ", Width=" + Width +
            ", Looms='" + Looms + '\'' +
            '}';
      }
    }

1 Answer

0 votes
answered Sep 12, 2017 by shriyansh-gautam

As you are using the android default layout for your list item android.R.layout.simple_list_item_1 you don't need the next argument ProdList1. Just remove it

CustomArrayAdapter<ProdSample> productAdapter = new CustomArrayAdapter<ProdSample>
        (MainActivity.this, android.R.layout.simple_list_item_1,  productSamples);

otherwise if you have used your own list item layout then

CustomArrayAdapter<ProdSample> productAdapter = new CustomArrayAdapter<ProdSample>
        (MainActivity.this, R.id.your_textview_id_in_your_list_item_layout, productSamples);

and you list item layout xml file should be then

// your_list_item_layout.xml    
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
    android:id="@+id/your_textview_id_in_your_list_item_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   />

</LinearLayout>

Your custom array adapter

public class CustomArrayAdapter extends ArrayAdapter<ProdSample> {

private static class ViewHolder {
    private TextView itemView;
}

public CustomArrayAdapter(Context context, int textViewResourceId, ArrayList<ProdSample> items) {
    super(context, textViewResourceId, items);
}

public View getView(int position, View convertView, ViewGroup parent) 
{

    if (convertView == null) {
        convertView = LayoutInflater.from(this.getContext())
        .inflate(R.layout.your_list_item_layout, parent, false);

        viewHolder = new ViewHolder();
        viewHolder.itemView = (TextView) convertView.findViewById(R.id.your_textview_id_in_your_list_item_layout);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    ProdSample sample = getItem(position);
    if (item!= null) {

        viewHolder.itemView.setText(sample.toString());
    }

    return convertView;
  }
}
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
...