我想要进行登录过程,登录后我有listview显示所有用户来自数据库,每行有编辑和删除按钮,并在listview后添加按钮,所以我写了所有的代码,但是当用户登录时,应用程序关闭下 这是代码: 的 DBHandler.java:
element.style.prop = 'prop'
MainActivitiy
package com.mehrandashti.letmelearn.zabansara;
/**
* Created by Welcome on 2017/2/14.
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DBHandler extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_User = "UserInfo";
// Contacts table name
private static final String TABLE_UserS = "Users";
// Users Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_User = "User";
private static final String KEY_PASS = "pass";
public DBHandler(Context context) {
super(context, DATABASE_User, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_UserS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_User + " TEXT"
+ KEY_PASS + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_UserS);
// Creating tables again
onCreate(db);
}
// Adding new User
public void addUser(User User) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID,User.getId());
values.put(KEY_User, User.getUser()); // User Name
values.put(KEY_PASS, User.getPass()); // User Phone Number
// Inserting Row
db.insert(TABLE_UserS, null, values);
db.close(); // Closing database connection
}
// Getting one User
public User getUser(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_UserS, new String[]{KEY_ID,
KEY_User, KEY_PASS}, KEY_ID + "=?",
new String[]{String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
User contact = new User(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
// return User
return contact;
}
// Getting All Users
public List<User> getAllUsers() {
List<User> UserList = new ArrayList<User>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_UserS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
User user = new User();
user.setId(Integer.parseInt(cursor.getString(0)));
user.setUser(cursor.getString(1));
user.setPass(cursor.getString(2));
// Adding contact to list
UserList.add(user);
} while (cursor.moveToNext());
}
// return contact list
return UserList;
}
// Getting Users Count
public int getUsersCount() {
String countQuery = "SELECT * FROM " + TABLE_UserS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
// Updating a User
public int updateUser(User User) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_User, User.getUser());
values.put(KEY_PASS, User.getPass());
// updating row
return db.update(TABLE_UserS, values, KEY_ID + " = ?",
new String[]{String.valueOf(User.getId())});
}
// Deleting a User
public void deleteUser(User User) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_UserS, KEY_ID + " ="+User.getId(),
new String[] { String.valueOf(User.getId()) });
db.close();
}
}
网页
package com.mehrandashti.letmelearn.zabansara;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import java.util.List;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonEnter = (Button) findViewById(R.id.button);
buttonEnter.setOnClickListener(this);
}
@Override
public void onClick(View v) {
DBHandler db = new DBHandler(this);
EditText user = (EditText) findViewById(R.id.editText);
EditText pass = (EditText) findViewById(R.id.editText4);
String s1=user.getText().toString();
String s2=pass.getText().toString();
List<User> Users = db.getAllUsers();
for (User User : Users) {
if (s1.equals(User.getUser()) && s2.equals(User.getPass())) {
Intent launchNewIntent = new Intent(MainActivity.this,Page.class);
startActivityForResult(launchNewIntent, 0);
break;
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("نام کاربری یا رمز ورود اشتباه می باشد ");
builder.setMessage("لطفا در وارد نمودن آنها دقت کنید");
builder.setPositiveButton("ok", null);
AlertDialog dialog = builder.show();
break;
}
}
}
}
MyCustomAdapter
package com.mehrandashti.letmelearn.zabansara;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import java.util.ArrayList;
public class Page extends ActionBarActivity implements View.OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
//generate list
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
//instantiate custom adapter
MyCustomAdapter adapter = new MyCustomAdapter(list, this);
//handle listview and assign adapter
ListView lView = (ListView)findViewById(R.id.listview1);
lView.setAdapter(adapter);
}
DBHandler db=new DBHandler(this);
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Add New User");
final EditText inputuser = new EditText(this);
final EditText inputpass = new EditText(this);
alertDialog.setView(inputuser);
alertDialog.setView(inputpass);
alertDialog.setPositiveButton("Yes Option", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//What ever you want to do with the value
String uss = inputuser.getText().toString();
String pss = inputpass.getText().toString();
db.addUser(new User(uss,pss));
}
});
alertDialog.setNegativeButton("No Option", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// what ever you want to do with No option.
dialog.cancel();
}
});
alertDialog.show();
}
}
**User**
package com.mehrandashti.letmelearn.zabansara;
public class User {
private int id;
private String user;
private String pass;
public User(){}
public User( String user, String pass){
this.user = user;
this.pass = pass;
}
public User(int id, String user, String pass) {
this.id = id;
this.user = user;
this.pass = pass;
}
public User(int id)
{
this.id=id;
}
public void setId(int id) {
this.id = id;
}
public void setUser(String user) {
this.user = user;
}
public void setPass(String pass) {
this.pass = pass;
}
public int getId() {
return id;
}
public String getPass() {
return pass;
}
public String getUser() {
return user;
}
}
page.xml
package com.mehrandashti.letmelearn.zabansara;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.mehrandashti.letmelearn.zabansara.R;
import java.util.ArrayList;
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
DBHandler db=new DBHandler(context);
public MyCustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.user, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.textView2);
listItemText.setText(list.get(position));
TextView list1ItemText = (TextView)view.findViewById(R.id.editText4);
list1ItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.delete);
Button editBtn = (Button)view.findViewById(R.id.edit);
deleteBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
User us=new User();
db.deleteUser(new User(position));
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
editBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
notifyDataSetChanged();
}
});
return view;
}
}
user.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="444dp"
android:id="@+id/listview1" />
<Button
android:text="Add User"
android:layout_width="199dp"
android:layout_height="match_parent"
android:id="@+id/button2"
android:layout_marginLeft="80dp" />
</LinearLayout>