“farm.admin.Main_Screen $ Contact_Adapter.getView()”致命异常(eclipe + android + sqlite)

时间:2013-10-24 11:15:30

标签: android

我正在尝试在Android模拟器上查看我的数据库,但是我遇到了这个致命的异常。应用程序在数据库中添加变量,直到我按下“查看全部”按钮。之后,我收到消息“很遗憾farmadmin已停止“每次,直到我清除应用程序的数据。我注意到主文件上的错误行

这是错误日志和我的代码:

10-24 06:32:40.521: D/dalvikvm(837): GC_FOR_ALLOC freed 81K, 5% free 2938K/3068K, paused 28ms, total 30ms
10-24 06:32:40.561: D/AndroidRuntime(837): Shutting down VM
10-24 06:32:40.571: W/dalvikvm(837): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-24 06:32:40.581: E/AndroidRuntime(837): FATAL EXCEPTION: main
10-24 06:32:40.581: E/AndroidRuntime(837): java.lang.NullPointerException
10-24 06:32:40.581: E/AndroidRuntime(837):  at farm.admin.Main_Screen$Contact_Adapter.getView(Main_Screen.java:157)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.widget.AbsListView.obtainView(AbsListView.java:2177)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.widget.ListView.onMeasure(ListView.java:1159)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.View.measure(View.java:15848)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.View.measure(View.java:15848)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.View.measure(View.java:15848)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.View.measure(View.java:15848)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
10-24 06:32:40.581: E/AndroidRuntime(837):  at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.View.measure(View.java:15848)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-24 06:32:40.581: E/AndroidRuntime(837):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.View.measure(View.java:15848)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.Choreographer.doFrame(Choreographer.java:532)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.os.Handler.handleCallback(Handler.java:730)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.os.Looper.loop(Looper.java:137)
10-24 06:32:40.581: E/AndroidRuntime(837):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-24 06:32:40.581: E/AndroidRuntime(837):  at java.lang.reflect.Method.invokeNative(Native Method)
10-24 06:32:40.581: E/AndroidRuntime(837):  at java.lang.reflect.Method.invoke(Method.java:525)
10-24 06:32:40.581: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-24 06:32:40.581: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 06:32:40.581: E/AndroidRuntime(837):  at dalvik.system.NativeStart.main(Native Method)

DatabaseHandler.java

package farm.admin;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "contactsManager";

    // Contacts table name
    private static final String TABLE_CONTACTS = "contacts";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_EARRING = "earring";
    private static final String KEY_AGE = "age";
    private static final String KEY_WEIGHT = "weight";
    private static final String KEY_MILKPRODUCTION = "milkproduction";
    private static final String KEY_BREED = "breed";
    private static final String KEY_DATEOFFERTILIZATION = "dateoffertilization";
    private static final String KEY_NOTES = "notes";
    private final ArrayList<Contact> contact_list = new ArrayList<Contact>();

    public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_EARRING + " TEXT," + KEY_AGE + " TEXT," + KEY_WEIGHT + " TEXT," + KEY_MILKPRODUCTION + " TEXT," + KEY_BREED + " TEXT," + KEY_DATEOFFERTILIZATION + " TEXT," + KEY_NOTES + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    // Create tables again
    onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    // Adding new contact
    public void Add_Contact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_EARRING, contact.getEarring()); // Contact earring
    values.put(KEY_AGE, contact.getAge()); // Contact age
    values.put(KEY_WEIGHT, contact.getWeight()); // Contact weight
    values.put(KEY_MILKPRODUCTION, contact.getMilkproduction()); // Contact milkprodcution
    values.put(KEY_BREED, contact.getBreed()); // Contact breed
    values.put(KEY_DATEOFFERTILIZATION, contact.getDateoffertilization()); // Contact date
    values.put(KEY_NOTES, contact.getNotes()); // Contact notes
    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
    db.close(); // Closing database connection
    }

    // Getting single contact
    Contact Get_Contact(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
        KEY_EARRING, KEY_AGE, KEY_WEIGHT,KEY_MILKPRODUCTION, KEY_BREED, KEY_DATEOFFERTILIZATION, KEY_NOTES }, KEY_ID + "=?",
        new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
        cursor.getString(1), cursor.getString(2), cursor.getString(3),cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7));
    // return contact
    cursor.close();
    db.close();

    return contact;
    }

    // Getting All Contacts
    public ArrayList<Contact> Get_Contacts() {
    try {
        contact_list.clear();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.setID(Integer.parseInt(cursor.getString(0)));
            contact.setEarring(cursor.getString(1));
            contact.setAge(cursor.getString(2));
            contact.setWeight(cursor.getString(3));
            contact.setMilkproduction(cursor.getString(4));
            contact.setBreed(cursor.getString(5));
            contact.setDateoffertilization(cursor.getString(6));
            contact.setNotes(cursor.getString(7));
            // Adding contact to list
            contact_list.add(contact);
        } while (cursor.moveToNext());
        }

        // return contact list
        cursor.close();
        db.close();
        return contact_list;
    } catch (Exception e) {
        // TODO: handle exception
        Log.e("all_contact", "" + e);
    }

    return contact_list;
    }

    // Updating single contact
    public int Update_Contact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_EARRING, contact.getEarring());
    values.put(KEY_AGE, contact.getAge());
    values.put(KEY_WEIGHT, contact.getWeight());
    values.put(KEY_MILKPRODUCTION, contact.getMilkproduction());
    values.put(KEY_BREED, contact.getBreed());
    values.put(KEY_DATEOFFERTILIZATION, contact.getDateoffertilization());
    values.put(KEY_NOTES, contact.getNotes());

    // updating row
    return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
        new String[] { String.valueOf(contact.getID()) });
    }

    // Deleting single contact
    public void Delete_Contact(int id) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
        new String[] { String.valueOf(id) });
    db.close();
    }

    // Getting contacts Count
    public int Get_Total_Contacts() {
    String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
    }

}

Add_update_user.java

package farm.admin;

import farm.admin.sqlite.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
//import android.telephony.PhoneNumberFormattingTextWatcher;
//import android.text.Editable;
//import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Add_Update_User extends Activity {
    EditText add_earring, add_age, add_weight, add_milkproduction, add_breed, add_dateoffertilization, add_notes ;
    Button add_save_btn, add_view_all, update_btn, update_view_all;
    LinearLayout add_view, update_view;
    String valid_earring = null, valid_age = null, valid_weight= null,valid_milkproduction= null,valid_breed= null,valid_dateoffertilization= null,valid_notes= null,
        Toast_msg = null, valid_user_id = "";
    int USER_ID;
    DatabaseHandler dbHandler = new DatabaseHandler(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_update_screen);

    // set screen
    Set_Add_Update_Screen();

    // set visibility of view as per calling activity
    String called_from = getIntent().getStringExtra("called");

    if (called_from.equalsIgnoreCase("add")) {
        add_view.setVisibility(View.VISIBLE);
        update_view.setVisibility(View.GONE);
    } else {

        update_view.setVisibility(View.VISIBLE);
        add_view.setVisibility(View.GONE);
        USER_ID = Integer.parseInt(getIntent().getStringExtra("USER_ID"));

        Contact c = dbHandler.Get_Contact(USER_ID);

        add_earring.setText(c.getEarring());
        add_age.setText(c.getAge());
        add_weight.setText(c.getWeight());
        add_milkproduction.setText(c.getMilkproduction());
        add_breed.setText(c.getBreed());
       add_dateoffertilization.setText(c.getDateoffertilization());
       add_notes.setText(c.getNotes());
        dbHandler.close();

    }




    add_save_btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
    ///////// check the value state is null or not
    /////if (valid_earring != null && valid_age != null && valid_weight != null && valid_milkproduction != null && valid_breed != null && valid_dateoffertilization != null && valid_notes != null &&
                //valid_earring.length() != 0 && valid_age.length() != 0 && valid_weight.length() != 0 && valid_milkproduction.length() != 0 && valid_breed.length() != 0 && valid_dateoffertilization.length() != 0 && valid_notes.length() != 0) {

            dbHandler.Add_Contact(new Contact(valid_earring,valid_age, valid_weight,valid_milkproduction,valid_breed, valid_dateoffertilization,valid_notes));
            Toast_msg = "Data inserted successfully";
            Show_Toast(Toast_msg);
            Reset_Text();

    //////////}

        }
    });

    update_btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub

        valid_earring = add_earring.getText().toString();
        valid_age = add_age.getText().toString();
        valid_weight = add_weight.getText().toString();
        valid_dateoffertilization = add_dateoffertilization.getText().toString();
        valid_breed = add_breed.getText().toString();
        valid_dateoffertilization = add_dateoffertilization.getText().toString();
        valid_notes = add_notes.getText().toString();


        // check the value state is null or not
        //////if (valid_earring != null && valid_age != null && valid_weight != null && valid_milkproduction != null && valid_breed != null && valid_dateoffertilization != null && valid_notes != null &&
        /////       valid_earring.length() != 0 && valid_age.length() != 0 && valid_weight.length() != 0 && valid_milkproduction.length() != 0 && valid_breed.length() != 0 && valid_dateoffertilization.length() != 0 && valid_notes.length() != 0) {

            dbHandler.Update_Contact(new Contact(USER_ID, valid_earring,valid_age, valid_weight,valid_milkproduction,valid_breed, valid_dateoffertilization,valid_notes));
            dbHandler.close();
            Toast_msg = "Data Update successfully";
            Show_Toast(Toast_msg);
            Reset_Text();
        //} else {
        ///    Toast_msg = "Sorry Some Fields are missing.\nPlease Fill up all.";
         ///   Show_Toast(Toast_msg);
        /////////}

        }
    });
    update_view_all.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent view_user = new Intent(Add_Update_User.this,
            Main_Screen.class);
        view_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(view_user);
        finish();
        }
    });

    add_view_all.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent view_user = new Intent(Add_Update_User.this,
            Main_Screen.class);
        view_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(view_user);
        finish();
        }
    });

    }

    public void Set_Add_Update_Screen() {

    add_earring = (EditText) findViewById(R.id.add_earring);
    add_age = (EditText) findViewById(R.id.add_age);
    add_weight = (EditText) findViewById(R.id.add_weight);
    add_milkproduction = (EditText) findViewById(R.id.add_milkproduction);
    add_breed = (EditText) findViewById(R.id.add_breed);
    add_dateoffertilization = (EditText) findViewById(R.id.add_dateoffertilization);
    add_notes = (EditText) findViewById(R.id.add_notes);

    add_save_btn = (Button) findViewById(R.id.add_save_btn);
    update_btn = (Button) findViewById(R.id.update_btn);
    add_view_all = (Button) findViewById(R.id.add_view_all);
    update_view_all = (Button) findViewById(R.id.update_view_all);

    add_view = (LinearLayout) findViewById(R.id.add_view);
    update_view = (LinearLayout) findViewById(R.id.update_view);

    add_view.setVisibility(View.GONE);
    update_view.setVisibility(View.GONE);

    }







    public void Show_Toast(String msg) {
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }

    public void Reset_Text() {

    add_earring.getText().clear();
    add_age.getText().clear();
    add_weight.getText().clear();
    add_milkproduction.getText().clear();
    add_breed.getText().clear();
    add_dateoffertilization.getText().clear();
    add_notes.getText().clear();

    }

}

Main_Screen.java

package farm.admin;

import java.util.ArrayList;

import farm.admin.sqlite.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
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.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Main_Screen extends Activity {
    Button add_btn;
    ListView Contact_listview;
    ArrayList<Contact> contact_data = new ArrayList<Contact>();
    Contact_Adapter cAdapter;
    DatabaseHandler db;
    String Toast_msg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
        Contact_listview = (ListView) findViewById(R.id.list);
        Contact_listview.setItemsCanFocus(false);
        add_btn = (Button) findViewById(R.id.add_btn);

        Set_Referash_Data();

    } catch (Exception e) {
        // TODO: handle exception
        Log.e("some error", "" + e);
    }
    add_btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent add_user = new Intent(Main_Screen.this,
            Add_Update_User.class);
        add_user.putExtra("called", "add");
        add_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(add_user);
        finish();
        }
    });

    }

    public void Call_My_Blog(View v) {
    Intent intent = new Intent(Main_Screen.this, My_Blog.class);
    startActivity(intent);

    }

    public void Set_Referash_Data() {
    contact_data.clear();
    db = new DatabaseHandler(this);
    ArrayList<Contact> contact_array_from_db = db.Get_Contacts();

    for (int i = 0; i < contact_array_from_db.size(); i++) {

        int tidno = contact_array_from_db.get(i).getID();
        String earring = contact_array_from_db.get(i).getEarring();
        String age = contact_array_from_db.get(i).getAge();
        String weight = contact_array_from_db.get(i).getWeight();
        String milkproduction = contact_array_from_db.get(i).getMilkproduction();
        String breed = contact_array_from_db.get(i).getBreed();
        String dateoffertilization = contact_array_from_db.get(i).getDateoffertilization();
        String notes = contact_array_from_db.get(i).getNotes();
        Contact cnt = new Contact();
        cnt.setID(tidno);
        cnt.setEarring(earring);
        cnt.setAge(age);
        cnt.setWeight(weight);
        cnt.setMilkproduction(milkproduction);
        cnt.setBreed(breed);
        cnt.setDateoffertilization(dateoffertilization);
        cnt.setNotes(notes);


        contact_data.add(cnt);
    }
    db.close();
    cAdapter = new Contact_Adapter(Main_Screen.this, R.layout.listview_row,
        contact_data);
    Contact_listview.setAdapter(cAdapter);
    cAdapter.notifyDataSetChanged();
    }

    public void Show_Toast(String msg) {
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Set_Referash_Data();

    }

    public class Contact_Adapter extends ArrayAdapter<Contact> {
    Activity activity;
    int layoutResourceId;
    Contact user;
    ArrayList<Contact> data = new ArrayList<Contact>();

    public Contact_Adapter(Activity act, int layoutResourceId,
        ArrayList<Contact> data) {
        super(act, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.activity = act;
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        UserHolder holder = null;

        if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(activity);

        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new UserHolder();
        holder.earring = (TextView) row.findViewById(R.id.add_earring);
        holder.age = (TextView) row.findViewById(R.id.add_age);
        holder.weight = (TextView) row.findViewById(R.id.add_weight);
        holder.milkproduction = (TextView) row.findViewById(R.id.add_milkproduction);
        holder.breed = (TextView) row.findViewById(R.id.add_breed);
        holder.dateoffertilization = (TextView) row.findViewById(R.id.add_dateoffertilization);
        holder.notes = (TextView) row.findViewById(R.id.add_notes);
        holder.edit = (Button) row.findViewById(R.id.btn_update);
        holder.delete = (Button) row.findViewById(R.id.btn_delete);
        row.setTag(holder);
        } else {
        holder = (UserHolder) row.getTag();
        }
        user = data.get(position);
        holder.edit.setTag(user.getID());
        holder.delete.setTag(user.getID());
        holder.earring.setText(user.getEarring());  //<------------- this is where the error points
        holder.age.setText(user.getAge());
        holder.weight.setText(user.getWeight());
        holder.milkproduction.setText(user.getMilkproduction());
        holder.breed.setText(user.getBreed());
        holder.dateoffertilization.setText(user.getDateoffertilization());
        holder.notes.setText(user.getNotes());

        holder.edit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.i("Edit Button Clicked", "**********");

            Intent update_user = new Intent(activity,
                Add_Update_User.class);
            update_user.putExtra("called", "update");
            update_user.putExtra("USER_ID", v.getTag().toString());
            activity.startActivity(update_user);

        }
        });
        holder.delete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(final View v) {
            // TODO Auto-generated method stub

            // show a message while loader is loading

            AlertDialog.Builder adb = new AlertDialog.Builder(activity);
            adb.setTitle("Delete?");
            adb.setMessage("Are you sure you want to delete ");
            final int user_id = Integer.parseInt(v.getTag().toString());
            adb.setNegativeButton("Cancel", null);
            adb.setPositiveButton("Ok",
                new AlertDialog.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog,
                    int which) {
                    // MyDataObject.remove(positionToRemove);
                    DatabaseHandler dBHandler = new DatabaseHandler(
                        activity.getApplicationContext());
                    dBHandler.Delete_Contact(user_id);
                    Main_Screen.this.onResume();

                }
                });
            adb.show();
        }

        });
        return row;

    }

    class UserHolder {
        public TextView notes;
        public TextView dateoffertilization;
        public TextView breed;
        public TextView milkproduction;
        public TextView weight;
        public TextView age;
        public TextView earring;
        TextView name;
        TextView email;
        TextView number;
        Button edit;
        Button delete;
    }

    }

}

0 个答案:

没有答案