我有一个带有SimpleCursorAdapter的ListView显示整数。如何添加所有这些整数并在字符串中显示总数?
see this screenshot to see my idea
看看Toast显示每个项目的值,但TextView(总计)不
public static final String ROW_ID = "row_id";
private static final String TITLE_APROVADAS = "title";
private static final String CREDITOS_APROVADAS = "creditos";
private ListView noteListView;
private SimpleCursorAdapter noteAdapter;
private Cursor cursor;
private TextView total;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_principal);
noteListView=(ListView)findViewById(R.id.listViewTarea);
total=(TextView)findViewById(R.id.texto_contador);
noteAdapter = new SimpleCursorAdapter(this,R.layout.aprovadas_textview,cursor,
new String[] {TITLE_APROVADAS,CREDITOS_APROVADAS},
new int[] {R.id.titulo_aprovadas_texto_listview, R.id.creditos_aprovadas_texto_listview},1){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.findViewById(R.id.titulo_aprovadas_texto_listview);
TextView credito = (TextView) view.findViewById(R.id.creditos_aprovadas_texto_listview);
String txtCredito=credito.getText().toString();
int losCreditos=Integer.parseInt(txtCredito);
total.setText(""+losCreditos);
Toast.makeText(ZMainActivity.this, ""+losCreditos, Toast.LENGTH_LONG).show();
return view;
}
};
noteListView.setAdapter(noteAdapter);
}
@Override
protected void onResume() {
super.onResume();
new GetNotes().execute((Object[]) null);
}
@Override
protected void onStop() {
Cursor cursor = noteAdapter.getCursor();
if(cursor != null ){
cursor.deactivate();
noteAdapter.changeCursor(null);
super.onStop();}
}
public class GetNotes extends AsyncTask<Object, Object, Cursor> {
Aprovadas_DatabaseConnector dbConnector = new Aprovadas_DatabaseConnector(ZMainActivity.this);
@Override
protected Cursor doInBackground(Object... params) {
// Open the database
dbConnector.open();
return (dbConnector.ListAllNotes());
}
@Override
protected void onPostExecute(Cursor result) {
noteAdapter.changeCursor(result);
// Close Database
dbConnector.close();
}}
}
答案 0 :(得分:0)
您应该总结数据提供者的所有值(猜测ArrayList)。迭代数组并将当前值添加到sum,然后显示它。
你需要做这样的事情
JPanel
您可能需要创建方法getCreditos以在每个节点中返回信用值,但是从中确定这是基本想法。