我正在开发一个应用程序,它允许在数据库中创建联系人,并在列表视图中显示它们。
目前我拥有它:
数据库:
public class Contact {
private int CTC_ID;
private byte[] CTC_IMAGE;
public String CTC_NOM;
public String CTC_NUMERO;
public Contact(){
}
public Contact(int CTC_ID, byte[] CTC_IMAGE, String CTC_NOM, String CTC_NUMERO){
this.CTC_ID = CTC_ID;
this.CTC_IMAGE = CTC_IMAGE;
this.CTC_NUMERO = CTC_NOM;
this.CTC_NUMERO = CTC_NUMERO;
}
public int getCTC_ID(){
return CTC_ID;
}
public void setCTC_ID(int CTC_ID) {
this.CTC_ID = CTC_ID;
}
public byte[] getCTC_IMAGE(){
return CTC_IMAGE;
}
public void setCTC_IMAGE(byte[] CTC_IMAGE) {
this.CTC_IMAGE = CTC_IMAGE;
}
public String getCTC_NOM() {
return CTC_NOM;
}
public void setCTC_NOM(String CTC_NOM) {
this.CTC_NOM = CTC_NOM;
}
public String getCTC_NUMERO() {
return CTC_NUMERO;
}
public void setCTC_NUMERO(String CTC_NUMERO) {
this.CTC_NUMERO = CTC_NUMERO;
}
public String toString(){
return "ID : "+CTC_ID+"\nimage : "+CTC_IMAGE+"\nnom : "+CTC_NOM+"\nnumero : "+CTC_NUMERO;
}
}
databaseHelper:
public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "kids_phone";
// Table Names
private static final String TABLE_CONTACT = "Contact";
private static final String TABLE_MESSAGE = "Message";
private static final String TABLE_ASSOCIER = "Associer";
// Common column names
private static final String KEY_CTC_ID = "CTC_ID";
private static final String KEY_MES_ID = "MES_ID";
private static final String KEY_ASS_ID = "ASS_ID";
// Contact Table - column names
public static final String KEY_CTC_IMAGE = "CTC_IMAGE";
public static final String KEY_CTC_NOM = "CTC_NOM";
public static final String KEY_CTC_NUMERO = "CTC_NUMERO";
// Message Table - column names
private static final String KEY_MES_TITRE = "MES_TITRE";
private static final String KEY_MES_CONTENU = "MES_CONTENU";
// Associer Table - column names
private static final String KEY_ASS_IMG = "ASS_IMG";
private static final String KEY_ASS_MES_ID = "ASS_MES_ID";
private static final String KEY_ASS_CTC_ID = "ASS_CTC_ID";
// Contact table Create Statements
private static final String CREATE_TABLE_CONTACT = "CREATE TABLE "
+ TABLE_CONTACT + " (" + KEY_CTC_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_CTC_IMAGE
+ " BLOB," + KEY_CTC_NOM + " TEXT," + KEY_CTC_NUMERO + " TEXT NOT NULL);";
// Message table create statement
private static final String CREATE_TABLE_MESSAGE = "CREATE TABLE " + TABLE_MESSAGE
+ " (" + KEY_MES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_MES_TITRE + " TEXT,"
+ KEY_MES_CONTENU + " TEXT NOT NULL);";
// Associer table create statement
private static final String CREATE_TABLE_ASSOCIER = "CREATE TABLE "
+ TABLE_ASSOCIER + " (" + KEY_ASS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ASS_IMG + " TEXT,"
+ KEY_ASS_MES_ID + " INTEGER," + KEY_ASS_CTC_ID + " INTEGER);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(CREATE_TABLE_CONTACT);
db.execSQL(CREATE_TABLE_MESSAGE);
db.execSQL(CREATE_TABLE_ASSOCIER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ASSOCIER);
// create new tables
onCreate(db);
}
private static SQLiteDatabase bdd;
static ArrayList<Contact> contactListe = new ArrayList<Contact>();
public void open() {
//on ouvre la BDD en écriture
SQLiteDatabase bdd = this.getWritableDatabase();
}
public void close() {
//on ferme l'accès à la BDD
bdd.close();
}
public long insertContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
//Création d'un ContentValues (fonctionne comme une HashMap)
ContentValues values = new ContentValues();
//on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
values.put(KEY_CTC_IMAGE, contact.getCTC_IMAGE());
values.put(KEY_CTC_NOM, contact.getCTC_NOM());
values.put(KEY_CTC_NUMERO, contact.getCTC_NUMERO());
//on insère l'objet dans la BDD via le ContentValues
long CTC_ID = db.insert(TABLE_CONTACT, null, values);
return CTC_ID;
}
public Cursor fetchAllContact() throws SQLException {
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCursor = db.query(TABLE_CONTACT, new String[]{"CTC_ID _id", KEY_CTC_IMAGE, KEY_CTC_NUMERO}, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public ArrayList<Contact> getListContact(){
String selectQuery = "SELECT * FROM " + TABLE_CONTACT;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
ArrayList<Contact> contactList = new ArrayList<Contact>();
if (cursor.moveToFirst()) {
do {
Contact list = new Contact();
list.setCTC_ID(Integer.parseInt(cursor.getString(0)));
list.setCTC_IMAGE(cursor.getBlob(1));
list.setCTC_NOM(cursor.getString(2));
list.setCTC_NUMERO(cursor.getString(3));
contactList.add(list);
} while (cursor.moveToNext());
}
return contactList;
}
}
ContactAdapter:
public class ContactAdapter extends BaseAdapter{
private Context context;
private ArrayList<Contact> contactList = new ArrayList<Contact>();
// Constructor
public ContactAdapter(Context context, ArrayList<Contact> contactList) {
this.context = context;
this.contactList.clear();
this.contactList.addAll(contactList);
}
@Override
public int getCount() {
return contactList.size();
}
@Override
public Object getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder{
ImageView img;
TextView name;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View row = view;
ViewHolder holder = new ViewHolder();
if(row == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.contact_item, null);
row.setTag(holder);
}
final TextView name = (TextView) row.findViewById(R.id.name);
name.setText(contactList.get(position).getCTC_NUMERO());
final ImageView img = (ImageView) row.findViewById(R.id.img);
byte[] contactImage = contactList.get(position).getCTC_IMAGE();
Bitmap bitmap = BitmapFactory.decodeByteArray(contactImage, 0, contactImage.length);
img.setImageBitmap(bitmap);
return row;
}
}
我的观点:
public class MainActivity extends AppCompatActivity {
private ListView lstViewContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstViewContact = (ListView) findViewById(R.id.lvContact);
displayContact();
}
public void displayContact() {
//On ouvre la base de données pour écrire dedans
DatabaseHelper dbHelper = new DatabaseHelper(this);
final ContactAdapter contactAdapter = new ContactAdapter(this, dbHelper.fetchAllContactDetail());
lstViewContact.setAdapter(contactAdapter);
}
}
目前很好地展示了联系人,
我希望当我向右滑动拨打号码时,我会希望。
我试过这个:
public class MainActivity extends Activity implements SwipeActionAdapter.SwipeActionListener {
public static int OVERLAY_PERMISSION_REQ_CODE = 1234;
protected SwipeActionAdapter mAdapter;
private ListView lstViewContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstViewContact = (ListView) findViewById(R.id.lvContact);
displayContact();
}
public void displayContact() {
//On ouvre la base de données pour écrire dedans
DatabaseHelper dbHelper = new DatabaseHelper(this);
final ContactAdapter contactAdapter = new ContactAdapter(this, dbHelper.getListContact());
mAdapter = new SwipeActionAdapter(contactAdapter);
lstViewContact.setAdapter(mAdapter);
}
@Override
public boolean hasActions(int position, SwipeDirection direction) {
if (direction.isLeft()) return true;
if (direction.isRight()) return true;
return false;
}
@Override
public boolean shouldDismiss(int position, SwipeDirection direction) {
return direction == SwipeDirection.DIRECTION_NORMAL_LEFT;
}
@Override
public void onSwipe(int[] positionList, SwipeDirection[] directionList) {
for (int i = 0; i < positionList.length; i++) {
SwipeDirection direction = directionList[i];
int position = positionList[i];
String dir = "";
switch (direction) {
case DIRECTION_FAR_LEFT:
dir = "Far left";
break;
case DIRECTION_NORMAL_LEFT:
dir = "Left";
break;
case DIRECTION_FAR_RIGHT:
dir = "Far right";
break;
case DIRECTION_NORMAL_RIGHT:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Test Dialog").setMessage("You swiped right").create().show();
dir = "Right";
break;
}
Toast.makeText(
this,
dir + " swipe Action triggered on " + mAdapter.getItem(position),
Toast.LENGTH_SHORT
).show();
mAdapter.notifyDataSetChanged();
}
}
}
但是我在logcat中有这个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: info.picse.phone_kids, PID: 24155
java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.view.View$OnTouchListener.onTouch(android.view.View, android.view.MotionEvent)' on a null object reference
at com.wdullaer.swipeactionadapter.SwipeViewGroup.onInterceptTouchEvent(SwipeViewGroup.java:174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1961)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2394)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
at android.app.Activity.dispatchTouchEvent(Activity.java:2809)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2355)
at android.view.View.dispatchPointerEvent(View.java:8667)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4255)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4121)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3574)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3627)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3593)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3710)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3601)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3767)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3574)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3627)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3593)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3601)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3574)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5939)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5913)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5884)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6029)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:122)
at android.app.ActivityThread.main(ActivityThread.java:5270)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:915)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:710)
感谢你带给我的帮助。
答案 0 :(得分:0)
你需要特殊帮助。幸运的是RecyclerView
已经拥有它。
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
[...]
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
//Remove swiped item from list and notify the RecyclerView
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
答案 1 :(得分:0)
我做到了:
public class OnSwipeTouchListener implements View.OnTouchListener {
ListView list;
private GestureDetector gestureDetector;
private Context context;
public OnSwipeTouchListener(Context ctx, ListView list) {
gestureDetector = new GestureDetector(ctx, new GestureListener());
context = ctx;
this.list = list;
}
public OnSwipeTouchListener() {
super();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
public void onSwipeRight(int pos) {
Log.d("Swipe Right", String.valueOf(pos));
}
public void onSwipeLeft(int pos) {
Log.d("Swipe left", String.valueOf(pos));
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
private int getPostion(MotionEvent e1) {
return list.pointToPosition((int) e1.getX(), (int) e1.getY());
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY)
&& Math.abs(distanceX) > SWIPE_THRESHOLD
&& Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight(getPostion(e1));
else
onSwipeLeft(getPostion(e1));
return true;
}
return false;
}
}
}
主要活动:
public void displayContact() {
//On ouvre la base de données pour écrire dedans
DatabaseHelper dbHelper = new DatabaseHelper(this);
final ContactAdapter contactAdapter = new ContactAdapter(this, dbHelper.getListContact());
lstViewContact.setAdapter(contactAdapter);
lstViewContact.setOnTouchListener(new OnSwipeTouchListener(this, lstViewContact));
}
但我希望当我向右侧移动应用程序调用时,列表视图中的数字为例如我有2个联系人:
1 0231XxXxXx 2 0233XxXxXx
当我滑动到2时,它会调用数字0233XxXxXx,我的列表是动态的。