我想要的只是一个记录列表,每个记录在列表视图中都有三个字符串字段。使用该应用程序会生成更多记录,此活动将记录显示为日志。数据被记录到本地存储上的 CSV 文件并从中读取。如果您知道存储数据的更好方法,请告诉我。
这是我的自定义适配器。在调试时,我让构造函数执行,但从未调用 getView() 的覆盖,这解释了为什么我什么也没看到,但为什么?更多代码如下:
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;
}
}
以及此活动的 onCreate()
@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();
}
}
和布局
<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>
和列表项
<?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>