kuldeep dwivedi
- Forum posts: 1
Jul 10, 2013, 9:29:33 AM via Website
Jul 10, 2013 9:29:33 AM via Website
hi friends
i tried to design a simple a sqlite related apps .but when i try to inflate my data in listview using view holder.it's getting error.i cannt understand how to debug it. please help me
my code is---
1- code for SQLiteOpenHelper
i tried to design a simple a sqlite related apps .but when i try to inflate my data in listview using view holder.it's getting error.i cannt understand how to debug it. please help me
my code is---
1- code for SQLiteOpenHelper
1public class DataHelper extends SQLiteOpenHelper {
2 public static String DATABASE="kuldb";
3 public static String DB_TABLE="friends";
4 public static String COL_id="id";
5 public static String COL_NAME="name";
6 public static String COL_ADD="address";
7 public static String COL_PH="call";
8
9 ArrayList<FriendModel>cartlist=new ArrayList<FriendModel>();
10 Context c;
11 public DataHelper(Context c){
12 super(c,DATABASE,null,1);
13 }
14 public void onCreate(SQLiteDatabase db){
15 db.execSQL("create table friends(id integer primary key autoincrement,name text,address text,call text)");
16 }
17 public void onUpgrade(SQLiteDatabase db,int oldv,int newv){
18 db.execSQL("drop table if exist "+DB_TABLE);
19 this.onCreate(db);
20 }
21
22 public void putfriends(FriendModel fritype){
23 SQLiteDatabase sd=this.getWritableDatabase();
24 ContentValues cv=new ContentValues();
25 cv.put("name", fritype.friname);
26 cv.put("address", fritype.friadd);
27 cv.put("call", fritype.friph);
28 sd.insert("friends", null, cv);
29 sd.close();
30 }
31
32 public void updatefri(FriendModel frilist){
33 SQLiteDatabase sd=this.getWritableDatabase();
34 ContentValues cv=new ContentValues();
35 cv.put("name", frilist.friname);
36 cv.put("address", frilist.friadd);
37 cv.put("call", frilist.friph);
38 sd.update("friends", cv, "name="+ frilist.friname, null);
39 sd.close();
40 }
41 public void emptyfriends(){
42 try{
43 SQLiteDatabase sd=this.getWritableDatabase();
44 sd.execSQL("delete from friends");
45 sd.close();
46 }catch(Exception e){
47 e.printStackTrace();
48 }
49 }
50 public void removefriend(String fname,String fadd,String fph){
51 try{
52 SQLiteDatabase sd=this.getWritableDatabase();
53 String[]arg={fname};
54 sd.delete("friends", "name=?", arg);
55 sd.close();
56 }catch(Exception e){
57 e.printStackTrace();
58 }
59 }
60
61 public ArrayList<FriendModel>getallfriends(){
62 cartlist.clear();
63 SQLiteDatabase sd=this.getWritableDatabase();
64 Cursor c=sd.rawQuery("select * from friends", null);
65 if(c.getCount()!=0){
66 if(c.moveToFirst()){
67 do{
68 FriendModel type=new FriendModel();
69 type.friname=c.getString(c.getColumnIndex("name"));
70 type.friadd=c.getString(c.getColumnIndex("address"));
71 type.friph=c.getString(c.getColumnIndex("call"));
72 cartlist.add(type);
73 }while(c.moveToNext());
74 }
75 }
76 c.close();
77 sd.close();
78 return cartlist;
79
80 }
81}
2- code for MainActivity2 public static String DATABASE="kuldb";
3 public static String DB_TABLE="friends";
4 public static String COL_id="id";
5 public static String COL_NAME="name";
6 public static String COL_ADD="address";
7 public static String COL_PH="call";
8
9 ArrayList<FriendModel>cartlist=new ArrayList<FriendModel>();
10 Context c;
11 public DataHelper(Context c){
12 super(c,DATABASE,null,1);
13 }
14 public void onCreate(SQLiteDatabase db){
15 db.execSQL("create table friends(id integer primary key autoincrement,name text,address text,call text)");
16 }
17 public void onUpgrade(SQLiteDatabase db,int oldv,int newv){
18 db.execSQL("drop table if exist "+DB_TABLE);
19 this.onCreate(db);
20 }
21
22 public void putfriends(FriendModel fritype){
23 SQLiteDatabase sd=this.getWritableDatabase();
24 ContentValues cv=new ContentValues();
25 cv.put("name", fritype.friname);
26 cv.put("address", fritype.friadd);
27 cv.put("call", fritype.friph);
28 sd.insert("friends", null, cv);
29 sd.close();
30 }
31
32 public void updatefri(FriendModel frilist){
33 SQLiteDatabase sd=this.getWritableDatabase();
34 ContentValues cv=new ContentValues();
35 cv.put("name", frilist.friname);
36 cv.put("address", frilist.friadd);
37 cv.put("call", frilist.friph);
38 sd.update("friends", cv, "name="+ frilist.friname, null);
39 sd.close();
40 }
41 public void emptyfriends(){
42 try{
43 SQLiteDatabase sd=this.getWritableDatabase();
44 sd.execSQL("delete from friends");
45 sd.close();
46 }catch(Exception e){
47 e.printStackTrace();
48 }
49 }
50 public void removefriend(String fname,String fadd,String fph){
51 try{
52 SQLiteDatabase sd=this.getWritableDatabase();
53 String[]arg={fname};
54 sd.delete("friends", "name=?", arg);
55 sd.close();
56 }catch(Exception e){
57 e.printStackTrace();
58 }
59 }
60
61 public ArrayList<FriendModel>getallfriends(){
62 cartlist.clear();
63 SQLiteDatabase sd=this.getWritableDatabase();
64 Cursor c=sd.rawQuery("select * from friends", null);
65 if(c.getCount()!=0){
66 if(c.moveToFirst()){
67 do{
68 FriendModel type=new FriendModel();
69 type.friname=c.getString(c.getColumnIndex("name"));
70 type.friadd=c.getString(c.getColumnIndex("address"));
71 type.friph=c.getString(c.getColumnIndex("call"));
72 cartlist.add(type);
73 }while(c.moveToNext());
74 }
75 }
76 c.close();
77 sd.close();
78 return cartlist;
79
80 }
81}
1public class MainActivity extends Activity{
2 EditText fname,fadd,fcall;
3 Button fsave,fview;
4 FriendModel fm;
5 DataHelper help;
6 public void onCreate(Bundle b){
7 super.onCreate(b);
8 setContentView(R.layout.main);
9 fname=(EditText)findViewById(R.id.addname);
10 fadd=(EditText)findViewById(R.id.addcity);
11 fcall=(EditText)findViewById(R.id.addph);
12 fsave=(Button)findViewById(R.id.savedata);
13 fview=(Button)findViewById(R.id.viewdata);
14
15 fsave.setOnClickListener(new OnClickListener() {
16
17 @Override
18 public void onClick(View v) {
19 // TODO Auto-generated method stub
20 if(fname.getText().toString().equals("")
21 ||fadd.getText().toString().equals("")||fcall.getText().toString().equals("")){
22 Toast.makeText(MainActivity.this, "please Fill All data fields", Toast.LENGTH_LONG).show();
23 }
24 else{
25 help=new DataHelper(getApplicationContext());
26 help.getWritableDatabase();
27 fm=new FriendModel();
28 fm.friname=fname.getText().toString();
29 fm.friadd=fadd.getText().toString();
30 fm.friph=fcall.getText().toString();
31 help.putfriends(fm);
32 Toast.makeText(MainActivity.this, "Records Added sucessfully", Toast.LENGTH_LONG).show();
33 fname.setText("");
34 fadd.setText("");
35 fcall.setText("");
36 }
37
38
39 }
40 });
41
42 fview.setOnClickListener(new OnClickListener() {
43
44 @Override
45 public void onClick(View v) {
46 // TODO Auto-generated method stub
47 Intent i=new Intent(getApplicationContext(),ViewFriends.class);
48 startActivity(i);
49 }
50 });
51 }
52
53}
code for FriendModel class2 EditText fname,fadd,fcall;
3 Button fsave,fview;
4 FriendModel fm;
5 DataHelper help;
6 public void onCreate(Bundle b){
7 super.onCreate(b);
8 setContentView(R.layout.main);
9 fname=(EditText)findViewById(R.id.addname);
10 fadd=(EditText)findViewById(R.id.addcity);
11 fcall=(EditText)findViewById(R.id.addph);
12 fsave=(Button)findViewById(R.id.savedata);
13 fview=(Button)findViewById(R.id.viewdata);
14
15 fsave.setOnClickListener(new OnClickListener() {
16
17 @Override
18 public void onClick(View v) {
19 // TODO Auto-generated method stub
20 if(fname.getText().toString().equals("")
21 ||fadd.getText().toString().equals("")||fcall.getText().toString().equals("")){
22 Toast.makeText(MainActivity.this, "please Fill All data fields", Toast.LENGTH_LONG).show();
23 }
24 else{
25 help=new DataHelper(getApplicationContext());
26 help.getWritableDatabase();
27 fm=new FriendModel();
28 fm.friname=fname.getText().toString();
29 fm.friadd=fadd.getText().toString();
30 fm.friph=fcall.getText().toString();
31 help.putfriends(fm);
32 Toast.makeText(MainActivity.this, "Records Added sucessfully", Toast.LENGTH_LONG).show();
33 fname.setText("");
34 fadd.setText("");
35 fcall.setText("");
36 }
37
38
39 }
40 });
41
42 fview.setOnClickListener(new OnClickListener() {
43
44 @Override
45 public void onClick(View v) {
46 // TODO Auto-generated method stub
47 Intent i=new Intent(getApplicationContext(),ViewFriends.class);
48 startActivity(i);
49 }
50 });
51 }
52
53}
1public class FriendModel {
2 public String friname="";
3 public String friadd="";
4 public String friph="";
5
6 public String getfriname(){
7 return friname;
8 }
9 public void setfriname(String friname){
10 this.friname=friname;
11 }
12 public String getfriadd(){
13 return friadd;
14 }
15 public void setfriadd(String friadd){
16 this.friadd=friadd;
17 }
18 public String getfriph(){
19 return friph;
20 }
21 public void setfriph(String friph){
22 this.friph=friph;
23 }
24
25}
code for final ViewFriends classs--2 public String friname="";
3 public String friadd="";
4 public String friph="";
5
6 public String getfriname(){
7 return friname;
8 }
9 public void setfriname(String friname){
10 this.friname=friname;
11 }
12 public String getfriadd(){
13 return friadd;
14 }
15 public void setfriadd(String friadd){
16 this.friadd=friadd;
17 }
18 public String getfriph(){
19 return friph;
20 }
21 public void setfriph(String friph){
22 this.friph=friph;
23 }
24
25}
1public class ViewFriends extends Activity{
2 TextView totalfans;
3 ListView listview;
4 DataHelper help;
5 ArrayList<FriendModel>friendlist=new ArrayList<FriendModel>();
6
7 public void onCreate(Bundle b){
8 super.onCreate(b);
9 setContentView(R.layout.view_record);
10 totalfans=(TextView)findViewById(R.id.totalrecords);
11 listview=(ListView)findViewById(R.id.listview);
12 }
13 public void onResume(){
14 super.onResume();
15 friendlist.clear();
16 help=new DataHelper(getApplicationContext());
17 help.getWritableDatabase();
18 ArrayList<FriendModel>fanlist=help.getallfriends();
19 for(int i=0;i<fanlist.size();i++){
20 String tfname=fanlist.get(i).getfriname();
21 String tfadd=fanlist.get(i).getfriadd();
22 String tfph=fanlist.get(i).getfriph();
23
24 FriendModel fan=new FriendModel();
25 fan.setfriname(tfname);
26 fan.setfriadd(tfadd);
27 fan.setfriph(tfph);
28 friendlist.add(fan);
29 }
30 totalfans.setText("Total Records Found: "+friendlist.size());
31 listview.setAdapter(new ListAdapter(this));
32 help.close();
33
34 }
35 private class ListAdapter extends BaseAdapter{
36 LayoutInflater inflater;
37 ViewHolder viewholder;
38
39 public ListAdapter(Context c){
40 inflater=LayoutInflater.from(c);
41 }
42
43 @Override
44 public int getCount() {
45 // TODO Auto-generated method stub
46 return friendlist.size();
47 }
48
49 @Override
50 public Object getItem(int position) {
51 // TODO Auto-generated method stub
52 return position;
53 }
54
55 @Override
56 public long getItemId(int position) {
57 // TODO Auto-generated method stub
58 return position;
59 }
60
61 @Override
62 public View getView(int position, View convertView, ViewGroup parent) {
63 // TODO Auto-generated method stub
64 if(convertView==null){
65 convertView=inflater.inflate(R.layout.item_record, null);
66 ViewHolder viewholder=new ViewHolder();
67 viewholder.tfriname=(TextView)convertView.findViewById(R.id.namedb);
68 viewholder.tfriadd=(TextView)convertView.findViewById(R.id.addressdb);
69 viewholder.tfriph=(TextView)convertView.findViewById(R.id.calldb);
70 convertView.setTag(viewholder);
71 }
72 else{
73 viewholder=(ViewHolder)convertView.getTag();
74 }
75 viewholder.tfriname.setText(friendlist.get(position).getfriname());
76 viewholder.tfriadd.setText(friendlist.get(position).getfriadd().trim());
77 viewholder.tfriph.setText(friendlist.get(position).getfriph().trim());
78 final int temp=position;
79
80 (convertView.findViewById(R.id.update)).setOnClickListener(new OnClickListener() {
81
82 @Override
83 public void onClick(View v) {
84 // TODO Auto-generated method stub
85 String updatename=friendlist.get(temp).getfriname();
86 String updateadd=friendlist.get(temp).getfriadd();
87 String updateph=friendlist.get(temp).getfriph();
88 Intent in=new Intent(ViewFriends.this,Update.class);
89 in.putExtra("fname", updatename);
90 in.putExtra("fadd", updateadd);
91 in.putExtra("fph", updateph);
92 startActivity(in);
93
94 }
95 });
96 (convertView.findViewById(R.id.delete)).setOnClickListener(new OnClickListener() {
97
98 @Override
99 public void onClick(View v) {
100 // TODO Auto-generated method stub
101 AlertDialog.Builder adb=new AlertDialog.Builder(ViewFriends.this);
102 adb.setCancelable(true);
103 adb.setMessage("Are you Sure about Delete Record???");
104 adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
105
106 @Override
107 public void onClick(DialogInterface dialog, int which) {
108 // TODO Auto-generated method stub
109 help.removefriend(friendlist.get(temp).getfriname().trim()
110 , friendlist.get(temp).getfriadd().trim(),friendlist.get(temp).getfriph().trim());
111 ViewFriends.this.onResume();
112 Toast.makeText(ViewFriends.this, "Record Deleted Sucessfully", Toast.LENGTH_LONG).show();
113
114 }
115 });
116 adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
117
118 @Override
119 public void onClick(DialogInterface dialog, int which) {
120 // TODO Auto-generated method stub
121
122 }
123 });
124 adb.show();
125
126 }
127 });
128 return convertView;
129 }
130
131 }
132 private class ViewHolder{
133 TextView tfriname;
134 TextView tfriadd;
135 TextView tfriph;
136 }
137
138}
2 TextView totalfans;
3 ListView listview;
4 DataHelper help;
5 ArrayList<FriendModel>friendlist=new ArrayList<FriendModel>();
6
7 public void onCreate(Bundle b){
8 super.onCreate(b);
9 setContentView(R.layout.view_record);
10 totalfans=(TextView)findViewById(R.id.totalrecords);
11 listview=(ListView)findViewById(R.id.listview);
12 }
13 public void onResume(){
14 super.onResume();
15 friendlist.clear();
16 help=new DataHelper(getApplicationContext());
17 help.getWritableDatabase();
18 ArrayList<FriendModel>fanlist=help.getallfriends();
19 for(int i=0;i<fanlist.size();i++){
20 String tfname=fanlist.get(i).getfriname();
21 String tfadd=fanlist.get(i).getfriadd();
22 String tfph=fanlist.get(i).getfriph();
23
24 FriendModel fan=new FriendModel();
25 fan.setfriname(tfname);
26 fan.setfriadd(tfadd);
27 fan.setfriph(tfph);
28 friendlist.add(fan);
29 }
30 totalfans.setText("Total Records Found: "+friendlist.size());
31 listview.setAdapter(new ListAdapter(this));
32 help.close();
33
34 }
35 private class ListAdapter extends BaseAdapter{
36 LayoutInflater inflater;
37 ViewHolder viewholder;
38
39 public ListAdapter(Context c){
40 inflater=LayoutInflater.from(c);
41 }
42
43 @Override
44 public int getCount() {
45 // TODO Auto-generated method stub
46 return friendlist.size();
47 }
48
49 @Override
50 public Object getItem(int position) {
51 // TODO Auto-generated method stub
52 return position;
53 }
54
55 @Override
56 public long getItemId(int position) {
57 // TODO Auto-generated method stub
58 return position;
59 }
60
61 @Override
62 public View getView(int position, View convertView, ViewGroup parent) {
63 // TODO Auto-generated method stub
64 if(convertView==null){
65 convertView=inflater.inflate(R.layout.item_record, null);
66 ViewHolder viewholder=new ViewHolder();
67 viewholder.tfriname=(TextView)convertView.findViewById(R.id.namedb);
68 viewholder.tfriadd=(TextView)convertView.findViewById(R.id.addressdb);
69 viewholder.tfriph=(TextView)convertView.findViewById(R.id.calldb);
70 convertView.setTag(viewholder);
71 }
72 else{
73 viewholder=(ViewHolder)convertView.getTag();
74 }
75 viewholder.tfriname.setText(friendlist.get(position).getfriname());
76 viewholder.tfriadd.setText(friendlist.get(position).getfriadd().trim());
77 viewholder.tfriph.setText(friendlist.get(position).getfriph().trim());
78 final int temp=position;
79
80 (convertView.findViewById(R.id.update)).setOnClickListener(new OnClickListener() {
81
82 @Override
83 public void onClick(View v) {
84 // TODO Auto-generated method stub
85 String updatename=friendlist.get(temp).getfriname();
86 String updateadd=friendlist.get(temp).getfriadd();
87 String updateph=friendlist.get(temp).getfriph();
88 Intent in=new Intent(ViewFriends.this,Update.class);
89 in.putExtra("fname", updatename);
90 in.putExtra("fadd", updateadd);
91 in.putExtra("fph", updateph);
92 startActivity(in);
93
94 }
95 });
96 (convertView.findViewById(R.id.delete)).setOnClickListener(new OnClickListener() {
97
98 @Override
99 public void onClick(View v) {
100 // TODO Auto-generated method stub
101 AlertDialog.Builder adb=new AlertDialog.Builder(ViewFriends.this);
102 adb.setCancelable(true);
103 adb.setMessage("Are you Sure about Delete Record???");
104 adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
105
106 @Override
107 public void onClick(DialogInterface dialog, int which) {
108 // TODO Auto-generated method stub
109 help.removefriend(friendlist.get(temp).getfriname().trim()
110 , friendlist.get(temp).getfriadd().trim(),friendlist.get(temp).getfriph().trim());
111 ViewFriends.this.onResume();
112 Toast.makeText(ViewFriends.this, "Record Deleted Sucessfully", Toast.LENGTH_LONG).show();
113
114 }
115 });
116 adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
117
118 @Override
119 public void onClick(DialogInterface dialog, int which) {
120 // TODO Auto-generated method stub
121
122 }
123 });
124 adb.show();
125
126 }
127 });
128 return convertView;
129 }
130
131 }
132 private class ViewHolder{
133 TextView tfriname;
134 TextView tfriadd;
135 TextView tfriph;
136 }
137
138}
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.