All I want is a list of records each with three string fields in a list view. More records get generated with use of the app and this activity displays the records as a log. Data gets logged to and read from a CSV file on local storage. If you know of a better way to store the data let me know as well.
Here is my custom adapter. While debugging I get the constructor to execute but the override of getView() is never called which explains why i’m not seeing anything but why? More code below:
public class TransRecordAdapter extends ArrayAdapter<String[]> {
private List<String[]> history;
public TransRecordAdapter(Context context, int textViewResourceId, List<String[]> history2) {
super(context, textViewResourceId);
this.history = history2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
}
String[] rec = history.get(position);
if (rec != null) {
TextView input = (TextView) v.findViewById(R.id.history_input);
TextView trans = (TextView) v.findViewById(R.id.history_trans);
TextView num = (TextView) v.findViewById(R.id.history_num);
if (rec[0] != null) {
input.setText(rec[0]);
}
if (rec[1] != null) {
trans.setText(rec[1]);
}
if (rec[2] != null) {
num.setText(rec[2]);
}
}
return v;
}
}
And the onCreate() for this activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gematria_droid_history);
ListView history_list = (ListView) findViewById(R.id.history_list);
try {
File historyFile = this.getFileStreamPath("gematriadroid-history");
if(!historyFile.exists()) {
historyFile.createNewFile();
}
FileReader mFileReader = new FileReader(historyFile);
CSVReader csvReader = new CSVReader(mFileReader);
List<String[]> history = csvReader.readAll();
csvReader.close();
if(history.size() != 0) {
TransRecordAdapter adapter = new TransRecordAdapter(this, R.layout.listitem, history);
history_list.setAdapter(adapter);
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (RuntimeException e2) {
e2.printStackTrace();
} catch (Exception e3) {
e3.printStackTrace();
}
}
and the layout
<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="match_parent"
android:orientation="vertical"
android:background="@drawable/background"
tools:context=".GematriaDroidHistory" >
<TextView
android:id="@+id/history_filter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="5dp"
android:hint="@string/history_filter"/>
<ListView
android:id="@+id/history_list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#FFFFFF">
</ListView>
</LinearLayout>
and the list item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
android:background="@drawable/papyrus"
android:padding="5dp">
<TextView
android:id="@+id/history_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/history_input"
android:textSize="20sp" />
<TextView
android:id="@+id/history_trans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="@string/history_trans"
android:textSize="20sp" />
<TextView
android:id="@+id/history_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textSize="20sp"
android:text="@string/history_num"/>
</RelativeLayout>
I think the problem is that you don’t tell the base class how many elements your array has.
It might propose two solutions for you.
Solution 1) Modify your constructor as below. This way, I think you don’t have to define the “history”, in getView, you can just call “getItem()” to get item at the position.
Solution 2) Also overwrite getCount() and getItem().