Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

New Forums: Nexus Player | Nexus 9
test: Reply
 
LinkBack Thread Tools
Old July 10th, 2013, 02:17 AM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Jun 2013
Posts: 2
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation getting error while using viewHolder in sqlite based application

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
Code:
public class DataHelper extends SQLiteOpenHelper {
    public static String DATABASE="kuldb";
    public static String DB_TABLE="friends";
    public static String COL_id="id";
    public static String COL_NAME="name";
    public static String COL_ADD="address";
    public static String COL_PH="call";
    
    ArrayList<FriendModel>cartlist=new ArrayList<FriendModel>();
    Context c;
    public DataHelper(Context c){
        super(c,DATABASE,null,1);
    }
    public void onCreate(SQLiteDatabase db){
        db.execSQL("create table friends(id integer primary key autoincrement,name text,address text,call text)");
    }
    public void onUpgrade(SQLiteDatabase db,int oldv,int newv){
        db.execSQL("drop table if exist "+DB_TABLE);
        this.onCreate(db);
    }
    
    public void putfriends(FriendModel fritype){
        SQLiteDatabase sd=this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put("name", fritype.friname);
        cv.put("address", fritype.friadd);
        cv.put("call", fritype.friph);
        sd.insert("friends", null, cv);
        sd.close();
    }
    
     public void updatefri(FriendModel frilist){
         SQLiteDatabase sd=this.getWritableDatabase();
         ContentValues cv=new ContentValues();
         cv.put("name", frilist.friname);
         cv.put("address", frilist.friadd);
         cv.put("call", frilist.friph);
         sd.update("friends", cv, "name="+ frilist.friname, null);
         sd.close();
     }
     public void emptyfriends(){
         try{
         SQLiteDatabase sd=this.getWritableDatabase();
         sd.execSQL("delete from friends");
         sd.close();
         }catch(Exception e){
             e.printStackTrace();
         }
     }
     public void removefriend(String fname,String fadd,String fph){
         try{
         SQLiteDatabase sd=this.getWritableDatabase();
         String[]arg={fname};
         sd.delete("friends", "name=?", arg);
         sd.close();
         }catch(Exception e){
             e.printStackTrace();
         }
     }
     
      public ArrayList<FriendModel>getallfriends(){
          cartlist.clear();
          SQLiteDatabase sd=this.getWritableDatabase();
          Cursor c=sd.rawQuery("select * from friends", null);
          if(c.getCount()!=0){
              if(c.moveToFirst()){
                  do{
                      FriendModel type=new FriendModel();
                      type.friname=c.getString(c.getColumnIndex("name"));
                      type.friadd=c.getString(c.getColumnIndex("address"));
                      type.friph=c.getString(c.getColumnIndex("call"));
                      cartlist.add(type);
                  }while(c.moveToNext());
              }
          }
          c.close();
          sd.close();
          return cartlist;
          
      }
}
2- code for MainActivity
Code:
public class MainActivity extends Activity{
    EditText fname,fadd,fcall;
    Button fsave,fview;
    FriendModel fm;
    DataHelper help;
    public void onCreate(Bundle b){
        super.onCreate(b);
        setContentView(R.layout.main);
        fname=(EditText)findViewById(R.id.addname);
        fadd=(EditText)findViewById(R.id.addcity);
        fcall=(EditText)findViewById(R.id.addph);
        fsave=(Button)findViewById(R.id.savedata);
        fview=(Button)findViewById(R.id.viewdata);
        
        fsave.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(fname.getText().toString().equals("")
                        ||fadd.getText().toString().equals("")||fcall.getText().toString().equals("")){
                    Toast.makeText(MainActivity.this, "please Fill All data fields", Toast.LENGTH_LONG).show();
                }
                else{
                    help=new DataHelper(getApplicationContext());
                    help.getWritableDatabase();
                    fm=new FriendModel();
                    fm.friname=fname.getText().toString();
                    fm.friadd=fadd.getText().toString();
                    fm.friph=fcall.getText().toString();
                    help.putfriends(fm);
                    Toast.makeText(MainActivity.this, "Records Added sucessfully", Toast.LENGTH_LONG).show();
                    fname.setText("");
                    fadd.setText("");
                    fcall.setText("");
                }
                    
                
            }
        });
        
        fview.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i=new Intent(getApplicationContext(),ViewFriends.class);
                          startActivity(i);
            }
        });
    }

}
code for FriendModel class
Code:
public class FriendModel {
    public String friname="";
    public String friadd="";
    public String friph="";
    
    public String getfriname(){
        return friname;
    }
    public void setfriname(String friname){
        this.friname=friname;
    }
    public String getfriadd(){
        return friadd;
    }
    public void setfriadd(String friadd){
        this.friadd=friadd;
    }
    public String getfriph(){
        return friph;
    }
    public void setfriph(String friph){
        this.friph=friph;
    }

}
code for final ViewFriends classs--
Code:
public class ViewFriends extends Activity{
    TextView totalfans;
    ListView listview;
    DataHelper help;
    ArrayList<FriendModel>friendlist=new ArrayList<FriendModel>();
    
       public void onCreate(Bundle b){
           super.onCreate(b);
           setContentView(R.layout.view_record);
           totalfans=(TextView)findViewById(R.id.totalrecords);
           listview=(ListView)findViewById(R.id.listview);
       }
       public void onResume(){
           super.onResume();
           friendlist.clear();
           help=new DataHelper(getApplicationContext());
           help.getWritableDatabase();
           ArrayList<FriendModel>fanlist=help.getallfriends();
           for(int i=0;i<fanlist.size();i++){
               String tfname=fanlist.get(i).getfriname();
               String tfadd=fanlist.get(i).getfriadd();
               String tfph=fanlist.get(i).getfriph();
               
               FriendModel fan=new FriendModel();
               fan.setfriname(tfname);
               fan.setfriadd(tfadd);
               fan.setfriph(tfph);
               friendlist.add(fan);
           }
           totalfans.setText("Total Records Found: "+friendlist.size());
           listview.setAdapter(new ListAdapter(this));
           help.close();
           
       }
       private class ListAdapter extends BaseAdapter{
           LayoutInflater inflater;
           ViewHolder viewholder;
           
           public ListAdapter(Context c){
               inflater=LayoutInflater.from(c);
           }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return friendlist.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            if(convertView==null){
                convertView=inflater.inflate(R.layout.item_record, null);
                ViewHolder viewholder=new ViewHolder();
                viewholder.tfriname=(TextView)convertView.findViewById(R.id.namedb);
                viewholder.tfriadd=(TextView)convertView.findViewById(R.id.addressdb);
                viewholder.tfriph=(TextView)convertView.findViewById(R.id.calldb);
                convertView.setTag(viewholder);
            }
            else{
                viewholder=(ViewHolder)convertView.getTag();
            }
              viewholder.tfriname.setText(friendlist.get(position).getfriname());
              viewholder.tfriadd.setText(friendlist.get(position).getfriadd().trim());
              viewholder.tfriph.setText(friendlist.get(position).getfriph().trim());
              final int temp=position;
              
              (convertView.findViewById(R.id.update)).setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String updatename=friendlist.get(temp).getfriname();
                    String updateadd=friendlist.get(temp).getfriadd();
                    String updateph=friendlist.get(temp).getfriph();
                    Intent in=new Intent(ViewFriends.this,Update.class);
                    in.putExtra("fname", updatename);
                    in.putExtra("fadd", updateadd);
                    in.putExtra("fph", updateph);
                    startActivity(in);
                    
                }
            });
              (convertView.findViewById(R.id.delete)).setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    AlertDialog.Builder adb=new AlertDialog.Builder(ViewFriends.this);
                    adb.setCancelable(true);
                    adb.setMessage("Are you Sure about Delete Record???");
                    adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            help.removefriend(friendlist.get(temp).getfriname().trim()
                                    , friendlist.get(temp).getfriadd().trim(),friendlist.get(temp).getfriph().trim());
                            ViewFriends.this.onResume();
                            Toast.makeText(ViewFriends.this, "Record Deleted Sucessfully", Toast.LENGTH_LONG).show();
                            
                        }
                    });
                    adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            
                        }
                    });
                    adb.show();
                    
                }
            });
            return convertView;
        }
           
       }
       private class ViewHolder{
           TextView tfriname;
           TextView tfriadd;
           TextView tfriph;
       }

}

Advertisements
kuldeep10m is offline  
Reply With Quote
sponsored links
Old July 10th, 2013, 05:54 AM   #2 (permalink)
~Play Nice~
 
Unforgiven's Avatar
 
Join Date: Jun 2010
Location: Douglas, MA
Gender: Male
Posts: 25,065
 
Device(s): Moto X Developer Edition, Nexus 7 (2012 & 2013), Note II, S3
Carrier: Not Provided

Thanks: 15,737
Thanked 17,016 Times in 9,363 Posts
Default

I moved this to the Application Development forum to get some better eyes on it.
__________________

Join the fun and make some friends, register for free here.
If someone helped, hit Thanks, if you see rude or abusive posts, spam, or threads that need staff attention, hit Report.
Site Rules / Android Forums FAQ
*** Do you want to talk guns? ***

Unforgiven is online now  
Reply With Quote
Reply
Tags
sqlite database, viewholder


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 11:44 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.