我创建了一个自定义列表视图,其中包含已填充的7个字段。 一些字段是从本地Db获取并设置在列表中,否则是edittexts。在每个列表行中,我还填充了一个保存按钮。在那个点击我能够将每个列表行保存到本地数据库。但是我需要通过单击按钮将完整列表保存到本地数据库中。有可能??
我的xml,Java类和适配器类正在发送。 XML类:
/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/spnrsangam"
/>
</TableRow>
<TableRow
android:weightSum="3"
android:layout_width="fill_parent"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Share Amount"
android:textSize="@dimen/Trans_heading_textsize"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:id="@+id/txtShreAmnt"
android:layout_weight=".8"
android:layout_marginLeft="15dp"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="@dimen/Trans_heading_textsize"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:id="@+id/edttxtShareAmnt"
android:layout_weight=".6"
android:inputType="number"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Deposit Amount"
android:textSize="@dimen/Trans_heading_textsize"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:id="@+id/txtAmnt"
android:layout_weight=".8"
android:layout_marginLeft="15dp"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="@dimen/Trans_heading_textsize"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:id="@+id/edttxtDepAmnt"
android:layout_weight=".6"
android:inputType="number"
/>
<Button
android:layout_width="0dp"
android:layout_height="30dp"
android:textSize="@dimen/Trans_heading_textsize"
android:layout_gravity="center_vertical"
android:text="Set"
android:background="#C5B2D8"
android:id="@+id/bttn_Show"
android:layout_weight=".2"
android:layout_marginRight="2dp"
/>
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Transaction"
android:textColor="#0000ff"
android:textSize="@dimen/Trans_heading_textsize"
android:textStyle="bold|italic" />
</LinearLayout>
<!-- ............. Table heading ............... -->
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:background="#1D1DF2"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#C5B2D8"
android:gravity="center_horizontal"
android:text="Name"
android:textColor="@android:color/black"
android:textSize="@dimen/Trans_heading_textsize"
android:textStyle="bold"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#C5B2D8"
android:gravity="center_horizontal"
android:text="Share No"
android:textColor="@android:color/black"
android:textSize="@dimen/Trans_heading_textsize"
android:textStyle="bold"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#C5B2D8"
android:gravity="center_horizontal"
android:text="Dep No"
android:textColor="@android:color/black"
android:textSize="@dimen/Trans_heading_textsize"
android:textStyle="bold"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#C5B2D8"
android:gravity="center_horizontal"
android:text="Trans Type"
android:textColor="@android:color/black"
android:textSize="@dimen/Trans_heading_textsize"
android:textStyle="bold"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#C5B2D8"
android:gravity="center_horizontal"
android:text="Share Amt"
android:textColor="@android:color/black"
android:textSize="@dimen/Trans_heading_textsize"
android:textStyle="bold"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#C5B2D8"
android:gravity="center_horizontal"
android:text="Deposit Amt"
android:textColor="@android:color/black"
android:textSize="@dimen/Trans_heading_textsize"
android:textStyle="bold"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#C5B2D8"
android:gravity="center_horizontal"
android:textColor="@android:color/black"
android:textStyle="bold"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list_Trans_sheet"
android:layout_width="match_parent"
android:layout_height="@dimen/Trans_List_Height"
android:divider="#000000"
android:dividerHeight="1dp"
android:choiceMode="multipleChoice"/>
</LinearLayout>
</ScrollView>
<!-- ......................................... -->
</LinearLayout>
</LinearLayout>
主Java类是: package com.example.esccocollection;
import java.util.ArrayList;
import java.util.List;
public class Sangam_Collection extends Activity {
ListView transview;
Custom_SangamCollection_Adapter adapter;
List<Retrieve_SangamCollection> translist;
EditText transAmnt, remark, dpsitamntset, shareAmtSet;
String str_agent_id2,idpref, str_setdepAmt, str_setShareAmt;
final AccountDBAdapter db = new AccountDBAdapter(this);
Spinner spnr_sangam;
String str_spinner, str_sangam_name;
Context context;
Button bttnset, bttnSave;
ArrayList<String> sangamnames = new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newtransaction_sheet);
shareAmtSet=(EditText)findViewById(R.id.edttxtShareAmnt);
dpsitamntset=(EditText)findViewById(R.id.edttxtDepAmnt);
bttnset = (Button)findViewById(R.id.bttn_Show);
transview = (ListView) findViewById(R.id.list_Trans_sheet);
str_setdepAmt="";
str_setShareAmt= "";
bttnset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
str_setdepAmt= dpsitamntset.getText().toString();
str_setShareAmt= shareAmtSet.getText().toString();
str_spinner=spnr_sangam.getSelectedItem().toString();
getsangam2dtls(str_spinner);
}
private void getsangam2dtls(String str_spinner) {
// TODO Auto-generated method stub
try {
db.open();
// String Maxcramnt = db.getTransSheetdetails();
//Cursor cur = db.getTransSheetdetails(str_spinner);
Cursor cur = db.getTransnewSheetdetails(str_spinner);
final String[] arrayAcc = new String[cur.getCount()];
String[] arrayName = new String[cur.getCount()];
String[] arrayBalAmnt = new String[cur.getCount()];
int i = 0;
while (cur.moveToNext()) {
String accArray = cur.getString(cur
.getColumnIndex("Acc_Type"));
String nameArray = cur.getString(cur
.getColumnIndex("Acc_No"));
String tamntArray = cur.getString(cur
.getColumnIndex("Dep_No"));
arrayAcc[i] = accArray.toString();
arrayName[i] = nameArray.toString();
arrayBalAmnt[i] = tamntArray.toString();
i++;
db.close();
}
if(arrayAcc.length!=0){
translist = new ArrayList<Retrieve_SangamCollection>();
for (int m = 0; m <= cur.getCount(); m++) {
//Log.e("Cust name", ""+arrayName[m]);
Retrieve_SangamCollection ret = new
Retrieve_SangamCollection
(arrayAcc[m],arrayName[m],arrayBalAmnt[m],str_setShareAmt,
str_setdepAmt);
translist.add(ret);
listset();
}
}else {
transview.setAdapter(null);
Toast.makeText(getApplicationContext(), "No data to display", 5000).show();
}
} catch (Exception e) {
// TODO: handle exception
Log.e("Trans retlist ", ""+e.getMessage());
}
// TODO Auto-generated method stub
}
private void listset() {
// TODO Auto-generated method stub
try {
transview = (ListView) findViewById(R.id.list_Trans_sheet);
transview.setAdapter(null);
adapter = new Custom_SangamCollection_Adapter
(Sangam_Collection.this, R.layout.trans_pop_list, translist);
transview.setAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (Exception e) {
// TODO: handle exception
Log.e("tr amount is:", "error..");
}
get_list();
}
});
spnr_sangam=(Spinner) findViewById(R.id.spnrsangam);
/////------------- to get sangam name from shared pref -----------------
SharedPreferences pr_sangam_name=getSharedPreferences("pr_sangam_name", MODE_MULTI_PROCESS);
str_sangam_name=pr_sangam_name.getString("sangam_name", "");
Log.e("Sangam name in loanform", ""+str_sangam_name);
sangamnames.add(str_sangam_name);
final ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sangamnames);
spnr_sangam.setAdapter(adapter1);
/*
String[] items = new String[] {"sangam1", "sangam2" };
final ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
spnr_sangam.setAdapter(adapter1);*/
// str_spinner=spnr_sangam.getSelectedItem().toString();
// String sanagam="sangam1";
/*db.open();
String Maxcramnt = db.getTransSheet(sanagam);
Toast.makeText(getApplicationContext(), ""+Maxcramnt, 5000).show();
db.open();*/
// TODO Auto-generated method stub
spnr_sangam.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
try {
// TODO Auto-generated method stub
// str_spinner=spnr_sangam.getSelectedItem().toString();
str_spinner=spnr_sangam.getSelectedItem().toString();
getsangam2dtls(str_spinner);
} catch (Exception e) {
// TODO: handle exception
}
// return false;
}
private void getsangam2dtls(String str_spinner) {
// TODO Auto-generated method stub
try {
db.open();
// String Maxcramnt = db.getTransSheetdetails();
// Cursor cur = db.getTransSheetdetails(str_spinner);
Cursor cur = db.getTransnewSheetdetails(str_spinner);
final String[] arrayAcc = new String[cur.getCount()];
String[] arrayName = new String[cur.getCount()];
String[] arrayBalAmnt = new String[cur.getCount()];
int i = 0;
while (cur.moveToNext()) {
String accArray = cur.getString(cur
.getColumnIndex("Acc_Type"));
String nameArray = cur.getString(cur
.getColumnIndex("Acc_No"));
String tamntArray = cur.getString(cur
.getColumnIndex("Dep_No"));
arrayAcc[i] = accArray.toString();
arrayName[i] = nameArray.toString();
arrayBalAmnt[i] = tamntArray.toString();
i++;
db.close();
}
if(arrayAcc.length!=0){
translist = new ArrayList<Retrieve_SangamCollection>();
for (int m = 0; m <= cur.getCount(); m++) {
// Log.e("Cust name", ""+arrayBalAmnt[m]);
Retrieve_SangamCollection ret = new Retrieve_SangamCollection(arrayAcc[m], arrayName[m], arrayBalAmnt[m], str_setShareAmt, str_setdepAmt);
translist.add(ret);
listset();
}
}else {
transview.setAdapter(null);
Toast.makeText(getApplicationContext(), "No data to display", 5000).show();
}
} catch (Exception e) {
// TODO: handle exception
Log.e("Trans retlist ", ""+e.getMessage());
}
}
private void listset() {
// TODO Auto-generated method stub
try {
transview = (ListView) findViewById(R.id.list_Trans_sheet);
transview.setAdapter(null);
adapter = new
Custom_SangamCollection_Adapter(Sangam_Collection.this,
R.layout.trans_pop_list, translist);
transview.setAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (Exception e) {
// TODO: handle exception
Log.e("tr amount is:", "error..");
}
}
/*private void getsangam1dtls(String str_spinner) {
// TODO Auto-generated method stub
try {
db.open();
// String Maxcramnt = db.getTransSheetdetails();
Cursor cur = db.getTransSheetdetails(str_spinner);
//Cursor cur = db.getTransSheetdetails();
final String[] arrayAcc = new String[cur.getCount()];
String[] arrayName = new String[cur.getCount()];
String[] arrayBalAmnt = new String[cur.getCount()];
int i = 0;
while (cur.moveToNext()) {
String accArray = cur.getString(cur
.getColumnIndex("Acc_No"));
String nameArray = cur.getString(cur
.getColumnIndex("Cust_Name"));
String tamntArray = cur.getString(cur
.getColumnIndex("Bal_Amt"));
arrayAcc[i] = accArray.toString();
arrayName[i] = nameArray.toString();
arrayBalAmnt[i] = tamntArray.toString();
i++;
db.close();
}
translist = new ArrayList<retrieveTrans>();
for (int m = 0; m <= cur.getCount(); m++) {
//Log.e("Cust name", ""+arrayName[m]);
retrieveTrans ret = new retrieveTrans(arrayAcc[m],
arrayName[m], arrayBalAmnt[m]);
translist.add(ret);
listset();
}
} catch (Exception e) {
// TODO: handle exception
Log.e("Trans retlist ", ""+e.getMessage());
}
//Cursor cur
}*/
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
try {
transview = getListView();
transview.setLongClickable(true);
transview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
transview.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateActionMode(ActionMode arg0, Menu arg1) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "on item clicked", 5000).show();
return false;
}
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "on item checked statmnt", 5000).show();
}
});
} catch (Exception e) {
// TODO: handle exception
Log.e("List work ", ""+e.getMessage());
}
}
private ListView getListView() {
// TODO Auto-generated method stub
return null;
}
protected void get_list() {
// TODO Auto-generated method stub
for(int i = 0;i<=transview.getCount();i++);
{
}
}
};
这是适配器类
import java.text.SimpleDateFormat;
@SuppressWarnings("serial")
public class Custom_SangamCollection_Adapter extends BaseAdapter {
public ArrayList<Retrieve_SangamCollection> ret_arrArrayList;
private List<Retrieve_SangamCollection> ret_translist;
private LayoutInflater inflater;
ArrayAdapter<String> adapterspin;
String time, currntdate, status;
String Maxcramnt, balamount,str_useridpref;
double Mamount, Bamount, actualamnt;
String idpref, str_agent_id2, accno;
int trcheck;
int d = 0;
String tramnt = "", remarks = "";
Context context;
int count;
public Custom_SangamCollection_Adapter(Sangam_Collection sangam_Collection,
int transListPop, List<Retrieve_SangamCollection> translist) {
// TODO Auto-generated constructor stub
super();
this.ret_translist = translist;
inflater = LayoutInflater.from(sangam_Collection);
this.ret_arrArrayList = new ArrayList<Retrieve_SangamCollection>();
this.ret_arrArrayList.addAll(translist);
this.context = sangam_Collection;
}
public Custom_SangamCollection_Adapter(Context context2) {
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return ret_translist.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return ret_translist.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
class ViewHolder {
TextView acc_txt, name_txt, balAmnt_txt;
Spinner trans_spinner;
EditText transAmnt_edittxt, remarks_edittxt;
Button save;
}
@Override
public View getView(final int pos, View cv, ViewGroup arg2) {
// TODO Auto-generated method stub
try {
final ViewHolder holder;
View row = cv;
int p = pos;
// Log.e("position of getview:", "" + p);
if (cv == null) {
cv = inflater.inflate(R.layout.trans_pop_list, null);
holder = new ViewHolder();
holder.acc_txt = (TextView) cv
.findViewById(R.id.txtTransAccNo_pop);
holder.name_txt = (TextView) cv
.findViewById(R.id.txtTransName_pop);
holder.balAmnt_txt = (TextView) cv
.findViewById(R.id.txtTransBalAmnt_pop);
holder.trans_spinner = (Spinner) cv
.findViewById(R.id.spinTrans_Type);
holder.transAmnt_edittxt = (EditText) cv
.findViewById(R.id.editTransAmnt_pop);
holder.save = (Button) cv.findViewById(R.id.save);
holder.remarks_edittxt = (EditText) cv
.findViewById(R.id.editRemarks_pop);
cv.setTag(holder);
} else {
holder = (ViewHolder) cv.getTag();
}
holder.acc_txt.setText("" + ret_translist.get(pos).getRetAccNo());
String hari = holder.acc_txt.getText().toString();
holder.name_txt.setText("" + ret_translist.get(pos).getRetName());
holder.balAmnt_txt.setText(""
+ ret_translist.get(pos).getRetBalAmnt());
holder.remarks_edittxt.setText(""+ret_translist.get(pos).getsetdpAmnt());
holder.transAmnt_edittxt.setText(""+ret_translist.get(pos).getsetShrAmt());
// holder.remarks_edittxt.setText("100");
/* ************* Declaring spinner items ************** */
String[] items = new String[] { "credit" };
final ArrayAdapter<String> adapterspin = new ArrayAdapter<String>(
context, android.R.layout.simple_spinner_item, items);
final String haris = holder.name_txt.getText().toString();
holder.trans_spinner.setAdapter(adapterspin);
/* ******************* Save button Click ******************* */
holder.save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int position = pos;
final String accno = holder.name_txt.getText().toString();
final String tramnt = holder.transAmnt_edittxt.getText()
.toString();
String remarks = holder.remarks_edittxt.getText()
.toString();
final String spinner = holder.trans_spinner
.getSelectedItem().toString();
Log.e("accno:", "" + accno);
Log.e("name :", "" + tramnt);
Log.e("position:", "" + position);
Log.e("remark:", "" + remarks);
Log.e("spinner:", "" + spinner);
/************* time and date *************/
// ----------Time----------**
Calendar c = Calendar.getInstance();
time = (c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE)
+ ":" + c.get(Calendar.SECOND));
Log.e("Current Time", " " + time);
// -----------Date---------**
Date now = new Date();
Date alsoNow = Calendar.getInstance().getTime();
currntdate = new SimpleDateFormat("M-d-yyyy").format(now);
Log.e("Date ", " " + currntdate);
/* **************** Check if all fields are Filled **************** */
status = new String("y");
if (tramnt.equals("")) {
Toast.makeText(arg0.getContext(),
"enter Transaction amount", 5000).show();
Log.e("if cndition", " " + tramnt);
} else if (spinner.equals("Type")) {
Toast.makeText(arg0.getContext(),
"Select Transaction type", 5000).show();
Log.e("if cndition", " " + tramnt);
}
else {
if (remarks.equals("")) {
remarks = "null";
}
/******************** declaration for dbcall *********************/
try{
SharedPreferences pr_acc_info=context.
getSharedPreferences("pr_acc_info",
context.MODE_MULTI_PROCESS);
str_useridpref=pr_acc_info.getString("userid","");
// String str_brnamepref=pr_acc_info.getString("branchname","");
Log.e("userid in custom sangamcollction", ""+str_useridpref);
}catch (Exception e) {
// TODO: handle exception
Log.e("", e.toString());
}
AccountDBAdapter db = new AccountDBAdapter(context);
Sangam_Collection ts = new Sangam_Collection();
try {
db.open();
String accid = db.getAccID(accno);
String Maxcramnt = db.getMaxCrAmnt(accno);
db.close();
Log.e("tramnt", " " + tramnt);
Log.e("Mamount", " " + Mamount);
actualamnt = 0;
String userId = "" + 2;
Mamount = Double.parseDouble(Maxcramnt);
Bamount = Double.parseDouble(tramnt);
Log.e("Mamount", " " + Mamount);
if(Bamount<Mamount)
{
Mamount = Double.parseDouble(Maxcramnt);
//Bamount = Double.parseDouble(tramnt);
actualamnt = Mamount - Bamount;
balamount = "" + actualamnt;
db.open();
db.update_MaxCrAmnt(balamount, accid);
db.close();
db.open();
db.insertTransactionTable(accid.toString(),
currntdate.toString(), spinner.toString(),
tramnt.toString(), str_useridpref.toString(),
time.toString(), remarks.toString(),
status.toString());
db.close();
holder.save.setText("SAVED");
holder.save.setBackgroundColor(Color.GRAY);
}
else
{
Toast.makeText(context, "Your Amount Exceeds your Balance", 5000).show();
}
} catch (Exception e) {
Log.e("error", e.getMessage());
}
}
}
});
} catch (Exception c) {
Log.e("adapter error", "" + c.getMessage());
}
return cv;
}
}
需要你的宝贵帮助..提前致谢。