有很多方法可以做到这一点,在某些情况下比其他人更好。
了解您将使用
,张贴在另一个答案类,你将需要:
- 的一些味道
ListView
显示数据
- 某种类型的适配器(
BaseAdapter
会做的很好)来处理显示您的数据
- 一个变量来存储您的数据(我会建议
ArrayList
)
- 定制
Layout
显示每个数据集在你ListView
打开此行弄成工作
首先,最简单的它只是为了让你的组件,然后最后一个环节他们。因此,首先,你的你ListView
的每一行布局:
// Very basic layout for a row
public class rowView extends LinearLayout {
TextView tv1 = null;
TextView tv2 = null;
TextView tv3 = null;
TextView tv4 = null;
public rowView(Context ct) {
super(ct);
// Do some Layout settings as needed
setOrientation(LinearLayout.HORIZONTAL);
// Create and configure TextViews
tv1 = new TextView(ct);
tv2 = new TextView(ct);
tv3 = new TextView(ct);
tv4 = new TextView(ct);
tv1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f));
tv2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f));
tv3.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f));
tv4.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f));
// Add the views to the layout
addView(tv1);
addView(tv2);
addView(tv3);
addView(tv4);
}
}
下一个是你的数据显示,其链接到您的EditText
的等等方式会有所不同。但是,关于如何使用ArrayList<String[]>
,这里有一些想法。
说,当你计算你的第三个数字,你调用一个函数象下面这样:
public void addData(String date, String num1, String num2, String calculatedNum) {
String[] s = new String[4];
s[0] = date;
s[1] = num1;
s[2] = num2;
s[3] = calculatedNum;
arrayListofStrings.add(s);
}
现在您的数据存储。最后一个主要步骤是创建适配器,您必须设置正确的“让您的ListView
正确地与您的数据对话”。鉴于你的新数据添加到年底你ArrayList
(这是不是试图将其添加到开始轻松了许多),所有你需要做的就是翻转以便在适配器像这样:
public class myAdapter extends BaseAdapter {
public int getCount() {
return arrayListofStrings.size();
}
public Object getItem(int position) {
// Returns the strings in REVERSE order
return arrayListofStrings.get(arrayListofStrings.size() - 1 - position);
}
public long getItemId(int position) {
// Not concerned with this
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Here is the version for memory optimization (aka view recycling). This should be habit
rowView ret = null;
if (convertView == null) {
// Create a new view
ret = new rowView(getApplicationContext());
} else {
// Cast and reuse
ret = (rowView) convertView;
}
// Get the data
String[] data = (String[]) getItem(position);
// Update the row with the data
ret.tv1.setText(data[0]);
ret.tv2.setText(data[1]);
ret.tv3.setText(data[2]);
ret.tv4.setText(data[3]);
return ret;
}
}
最后的步骤是在你需要的地方创建你的ListView
,创建并附加你的Adapter
到你的ListView
和其他小事情(比如将你的值转换为字符串等)。
这绝不是一个可行的解决方案,只是你想要做什么的胆量。
注:无论何时更改数据确保你通话BaseAdapter.notifyDataSetChanged
你做了什么至今? – Dharmendra 2012-07-24 04:06:33