用android联系应用程序

时间:2016-12-01 09:48:36

标签: android sqlite android-sqlite

Contacts()类: 公共课联系人{

for(i in levels(n)){
  x.subset <- subset(data$x, data$n == i)
  x.big <- NULL
  x.big.prev <- 1

  for(j in y){
    x.big[j] <- x.subset[j]^2

    if((mean(x.big.prev)-mean(x.big)) >=30){
          break
        } #end of if() statement
    x.big.prev <- x.big #####
      } #end of j-loop
      plot(y, x.big.prev)
} #end of i loop

}

Example4()类: 公共类Example4扩展了AppCompatActivity {

int _id;
String _name;
String _mobile;
String _home;
String _office;
String _email;
String _companyName;
String _jobRole;
String _address;


public Contacts( String name, String mobile, String home, String office, String address, String email,String companyName, String jobRole ) {

     this._name = name;
    this._mobile = mobile;
    this._home = home;
    this._office = office;
    this._address = address;
    this._email = email;
    this._companyName = companyName;
    this._jobRole = jobRole;

}
public Contacts(int id, String name, String mobile, String home, String office,String address,String email,String companyName, String jobRole ) {

    this._id = id;
     this._name = name;
    this._mobile = mobile;
    this._home = home;
    this._office = office;
    this._address = address;
    this._email = email;
    this._companyName = companyName;
    this._jobRole = jobRole;

}

public Contacts() {

}


public String getCompanyName() {
    return _companyName;
}

public void setCompanyName(String companyName) {
    this._companyName = companyName;
}

public String getEmail() {
    return _email;
}

public void setEmail(String email) {
    this._email = email;
}

public String getHome() {
    return _home;
}

public void setHome(String home) {
    this._home = home;
}

public int getId() {
    return _id;
}

public void setId(int id) {
    this._id = id;
}

public String getJobRole() {
    return _jobRole;
}

public void setJobRole(String jobRole) {
    this._jobRole = jobRole;
}

public String getMobile() {
    return _mobile;
}

public void setMobile(String mobile) {
    this._mobile = mobile;
}

public String getName() {
    return _name;
}

public void setName(String name) {
    this._name = name;
}

public String getOffice() {
    return _office;
}

public void setOffice(String office) {
    this._office = office;
}

public String getAddress() {

    return _address;
}

public void setAddress(String address) {
    this._address = address;
}

}

DisplayContacts()类:

公共类DisplayContacts扩展了Activity {

DbHandler dbHandler;
ListView listView;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.example4);



    dbHandler = new DbHandler(this);
    List<Contacts> listContacts = dbHandler.getAllContacts();
    ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,listContacts);

    listView = (ListView) findViewById(R.id.listView1);
    listView.setAdapter(arrayAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            Bundle bundle = new Bundle();
            int id_search = i ;
            bundle.putInt("id",id_search);

            Intent intent = new Intent(getApplicationContext(),DisplayContacts.class);
            intent.putExtras(bundle);
            startActivity(intent);

        }
    });






    Log.d("Insert: ", "Inserting ..");
    dbHandler.addContact(new Contacts("Ravi", "9100000000","044-123456","","","r@gmail.com","qbrik","developer"));
    dbHandler.addContact(new Contacts("Srinivas", "9199999999","044-123456","","","r@gmail.com","qbrik","developer"));
    dbHandler.addContact(new Contacts("Tommy", "9522222222","044-123456","","","r@gmail.com","qbrik","developer"));
    dbHandler.addContact(new Contacts("Karthik", "9533333333","044-123456","","","r@gmail.com","qbrik","developer"));

    Log.d("Reading: ", "Reading all contacts..");

    for (Contacts cn : listContacts) {
        String log = "Id: "+cn.getId()+" ,Name: " + cn.getName() + " ,Mobile: " + cn.getMobile() + " ,Home: " + cn.getHome() + " ,Office: " + cn.getOffice() + " ,Email: " + cn.getEmail() + " ,Company Name: " + cn.getCompanyName() + " ,Job Role: " + cn.getJobRole();
        Log.d("Name: ", log);
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
     super.onOptionsItemSelected(item);
    switch(item.getItemId()) {
        case R.id.item1:Bundle dataBundle = new Bundle();
            dataBundle.putInt("id", 0);

            Intent intent = new Intent(getApplicationContext(),DisplayContacts.class);
            intent.putExtras(dataBundle);

            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

}

}

DbHandler()类:

公共类DbHandler扩展了SQLiteOpenHelper {

TextView name;
TextView mobile;
TextView home;
TextView office;
TextView companyName;
TextView jobRole;
TextView email;
TextView address;
private DbHandler dbHandler;
private int id_update =0;
private  Contacts contacts;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.example4_display);

    name = (TextView) findViewById(R.id.editName);
    mobile = (TextView) findViewById(R.id.editTextMobile);
    home = (TextView) findViewById(R.id.editTextHome);
    office = (TextView) findViewById(R.id.editTextOffice);
    companyName = (TextView) findViewById(R.id.editTextComapanyName);
    jobRole = (TextView) findViewById(R.id.editTextJobRole);
    email = (TextView) findViewById(R.id.editEmail);
    address = (TextView) findViewById(R.id.editAddress);

    dbHandler = new DbHandler(this);
    contacts = new Contacts();
    Bundle bundle = getIntent().getExtras();
    if(bundle != null) {

        int value = bundle.getInt("id");
        if(value > 0) {

            Cursor cs = (Cursor) dbHandler.getContact(value);
            id_update = value;
            cs.moveToFirst();


            String nam = cs.getString(cs.getColumnIndex(DbHandler.KEY_NAME));
            String mob = cs.getString(cs.getColumnIndex(DbHandler.KEY_MOBILE));
            String hom = cs.getString(cs.getColumnIndex(DbHandler.KEY_HOME));
            String off = cs.getString(cs.getColumnIndex(DbHandler.KEY_OFFICE));
            String com = cs.getString(cs.getColumnIndex(DbHandler.KEY_COMPANYNAME));
            String job = cs.getString(cs.getColumnIndex(DbHandler.KEY_JOBROLE));
            String emai = cs.getString(cs.getColumnIndex(DbHandler.KEY_EMAIL));
            String addr = cs.getString(cs.getColumnIndex(DbHandler.KEY_ADDRESS));

            if(!cs.isClosed()) {
                cs.close();

                Button b = (Button) findViewById(R.id.button1);
                b.setVisibility(View.INVISIBLE);


                name.setText(nam);
                name.setFocusable(false);
                name.setClickable(false);

                mobile.setText(mob);
                mobile.setFocusable(false);
                mobile.setClickable(false);

                home.setText(hom);
                home.setFocusable(false);
                home.setClickable(false);

                office.setText(off);
                office.setFocusable(false);
                office.setClickable(false);

                companyName.setText(com);
                companyName.setFocusable(false);
                companyName.setClickable(false);

                jobRole.setText(job);
                jobRole.setFocusable(false);
                jobRole.setClickable(false);

                email.setText(emai);
                email.setFocusable(false);
                email.setClickable(false);

                address.setText(addr);
                address.setFocusable(false);
                address.setClickable(false);
            }


        }

    }






}

public void run(View view) {

    Bundle bundle = getIntent().getExtras();
    if(bundle != null) {

        int value =bundle.getInt("id");
        if(value > 0) {
            if(dbHandler.updateContact(new Contacts(id_update,name.getText().toString(),mobile.getText().toString(),home.getText().toString(),office.getText().toString(),companyName.getText().toString(),jobRole.getText().toString(),email.getText().toString(),address.getText().toString())))  {

                Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getApplicationContext(),Example4.class);
                startActivity(intent);
            } else{
                Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();
            }
        } else{
            if(dbHandler.addContact(new Contacts(name.getText().toString(),mobile.getText().toString(),home.getText().toString(),office.getText().toString(),companyName.getText().toString(),jobRole.getText().toString(),email.getText().toString(),address.getText().toString()))) {
                Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
            } else{
                Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
            }
            Intent intent = new Intent(getApplicationContext(),Example4.class);
            startActivity(intent);
        }
    }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    Bundle bundle = getIntent().getExtras();
    if(bundle !=null) {
        int value = bundle.getInt("id");
        if(value > 0) {
           getMenuInflater().inflate(R.menu.display_contact,menu);
        }else {
            getMenuInflater().inflate(R.menu.menu_main, menu);
        }
    }

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);

    switch(item.getItemId()) {
        case R.id.Edit_Contact:

            Button b = (Button) findViewById(R.id.button1);
            b.setVisibility(View.VISIBLE);

            name.setEnabled(true);
            name.setFocusableInTouchMode(true);
            name.setClickable(true);

            mobile.setEnabled(true);
            mobile.setFocusableInTouchMode(true);
            mobile.setClickable(true);

            home.setEnabled(true);
            home.setFocusableInTouchMode(true);
            home.setClickable(true);

            office.setEnabled(true);
            office.setFocusableInTouchMode(true);
            office.setClickable(true);

            companyName.setEnabled(true);
            companyName.setFocusableInTouchMode(true);
            companyName.setClickable(true);

            jobRole.setEnabled(true);
            jobRole.setFocusableInTouchMode(true);
            jobRole.setClickable(true);

            email.setEnabled(true);
            email.setFocusableInTouchMode(true);
            email.setClickable(true);

            address.setEnabled(true);
            address.setFocusableInTouchMode(true);
            address.setClickable(true);

            return true;
        case  R.id.Delete_Contact:

            return  true;
        default:
            return super.onOptionsItemSelected(item);
    }


}

}

但是此代码无效,请您更新代码

1 个答案:

答案 0 :(得分:-1)

可能你想要......

if(dbHandler.updateContact(new Contacts(id_update,name.getText().toString(),mobile.getText().toString(),home.getText().toString(),office.getText().toString(),companyName.getText().toString(),jobRole.getText().toString(),email.getText().toString(),address.getText().toString()))) {}

您还需要更改addContact / updateContact以返回布尔值,如下所示:

        public Boolean addContact(Contacts contacts) {

        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_NAME,contacts.getName());
        contentValues.put(KEY_MOBILE,contacts.getMobile());
        contentValues.put(KEY_HOME,contacts.getHome());
        contentValues.put(KEY_OFFICE,contacts.getOffice());
        contentValues.put(KEY_ADDRESS,contacts.getAddress());
        contentValues.put(KEY_EMAIL,contacts.getEmail());
        contentValues.put(KEY_COMPANYNAME,contacts.getCompanyName());
        contentValues.put(KEY_JOBROLE,contacts.getJobRole());


        sqLiteDatabase.insert(TABLE_CONTACTS,null,contentValues);
        sqLiteDatabase.close();

        return true;
    }

尝试此功能并告诉我stackstrace错误

    public List<Contacts>getAllContacts(){

    List<Contacts> contactsList = new ArrayList<Contacts>();
    String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    Cursor cursor = sqLiteDatabase.rawQuery(selectQuery,null);

    if (cursor.moveToFirst()){

        do {

            Contacts contacts = new Contacts();
            contacts.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndexOrThrow(KEY_ID))));
            contacts.setName(cursor.getString(cursor.getColumnIndexOrThrow(KEY_NAME)));
            contacts.setMobile(cursor.getString(cursor.getColumnIndexOrThrow(KEY_MOBILE)));
            contacts.setHome(cursor.getString(cursor.getColumnIndexOrThrow(KEY_HOME)));
            contacts.setOffice(cursor.getString(cursor.getColumnIndexOrThrow(KEY_OFFICE)));
            contacts.setAddress(cursor.getString(cursor.getColumnIndexOrThrow(KEY_ADDRESS)));
            contacts.setEmail(cursor.getString(cursor.getColumnIndexOrThrow(KEY_EMAIL)));
            contacts.setCompanyName(cursor.getString(cursor.getColumnIndexOrThrow(KEY_COMPANYNAME)));
            contacts.setJobRole(cursor.getString(cursor.getColumnIndexOrThrow(KEY_JOBROLE)));

            contactsList.add(contacts);


        }while (cursor.moveToNext());

    }
    return contactsList;

}