Listview içerisindeki sayıların toplamını yazdırma
Merhaba aranıza yeni katıldım.Takıldığım konu şu;
1.sorum :Fotodaki gibi Test3 deki girilen değerlerin toplamını nasıl bulabilirim.
2.sorum :Listedeki değerlere basılı tutduğumda o değeri databaseden nasıl silerim.
[code]
package com.student;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity implements OnClickListener {
EditText et_name, et_t1, et_t2, et_t3;
Button btn_save;
ListView listView;
String name, t1, t2, t3;
List<String> list_name = new ArrayList<String>();
List<Integer> list_t1 = new ArrayList<Integer>();
List<Integer> list_t2 = new ArrayList<Integer>();
List<Integer> list_t3 = new ArrayList<Integer>();
static String[] arr_name;
static Integer[] arr_t1;
static Integer[] arr_t2;
static Integer[] arr_t3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et_name = (EditText) findViewById(R.id.et_name);
et_t1 = (EditText) findViewById(R.id.et_t1);
et_t2 = (EditText) findViewById(R.id.et_t2);
et_t3 = (EditText) findViewById(R.id.et_t3);
btn_save = (Button) findViewById(R.id.btn_save);
btn_save.setOnClickListener(this);
//deleteDatabase();
readDBdata();
listView = (ListView) findViewById(R.id.lv_Marks);
listView.setAdapter(new EfficientAdapter(this));
}
@Override
public void onClick(View v) {
if (v == btn_save) {
if (et_name.length() <= 0 || et_t1.length() <= 0
|| et_t2.length() <= 0 || et_t3.length() <= 0) {
Toast.makeText(this, "Please fill each field",
Toast.LENGTH_SHORT).show();
return;
}
name = et_name.getText().toString();
t1 = et_t1.getText().toString();
t2 = et_t2.getText().toString();
t3 = et_t3.getText().toString();
DatabaseHelper dbh = new DatabaseHelper(this);
SQLiteDatabase db = dbh.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(dbh.name, name);
cv.put(dbh.t1, t1);
cv.put(dbh.t2, t2);
cv.put(dbh.t3, t3);
try {
// db.insert(dbh.tbl_student, null, cv); or
db.insertOrThrow(dbh.tbl_student, null, cv);
} catch (Exception ex) {
Toast.makeText(this,
"Sorry data can't be saved. error:" + ex.toString(),
Toast.LENGTH_SHORT).show();
} finally {
db.close();
}
Toast.makeText(this, "Data has been saved.", Toast.LENGTH_SHORT)
.show();
readDBdata();
listView.setAdapter(new EfficientAdapter(this));
}
}
public void readDBdata() {
DatabaseHelper dbh = new DatabaseHelper(this);
SQLiteDatabase db = dbh.getWritableDatabase();
list_name.clear();
list_t1.clear();
list_t2.clear();
list_t3.clear();
Cursor cursor = db.rawQuery("Select " + dbh.name + "," + dbh.t1 + ","
+ dbh.t2 + "," + dbh.t3 + " from tbl_student;", null);
if (cursor.moveToFirst()) {
do {
list_name.add(cursor.getString(0));
list_t1.add(cursor.getInt(1));
list_t2.add(cursor.getInt(2));
list_t3.add(cursor.getInt(3));
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
db.close();
arr_name = new String[list_name.size()];
arr_t1 = new Integer[list_t1.size()];
arr_t2 = new Integer[list_t2.size()];
arr_t3 = new Integer[list_t3.size()];
for (int i = 0; i < list_name.size(); i++) {
arr_name[i] = list_name.get(i);
arr_t1[i] = list_t1.get(i);
arr_t2[i] = list_t2.get(i);
arr_t3[i] = list_t3.get(i);
}
Toast.makeText(this, list_t3.toString(), Toast.LENGTH_SHORT).show();
}
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return arr_name.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.three_col_row, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.TextView01);
holder.text2 = (TextView) convertView
.findViewById(R.id.TextView02);
holder.text3 = (TextView) convertView
.findViewById(R.id.TextView03);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(arr_name[position]);
holder.text2.setText(String.valueOf(arr_t1[position] + ", "
+ arr_t2[position] + ", " + arr_t3[position]));
holder.text3.setText(String.valueOf(4555555));
return convertView;
}
static class ViewHolder {
TextView text1;
TextView text2;
TextView text3;
}
}
public void deleteDatabase() {
DatabaseHelper dbh = new DatabaseHelper(this);
SQLiteDatabase SQLDb = dbh.getWritableDatabase();
dbh.close();
SQLDb.close();
if (this.deleteDatabase(dbh.db_demo)) {
Log.d("Droped", "deleteDatabase(): database deleted.");
} else {
Log.d("Cant Drop", "deleteDatabase(): database NOT deleted.");
}
}
}
[/code]