我创建了一个应用程序,其中我有两个ListView
ID列表视图和listview1,我试图在长按一下删除该项目..但是长按一下得到数字格式。如何使用longclink和删除我点击的项目。该项目也从数据库中删除..,我在列表中删除名称,当用户长按项目时,它会被删除。
Campaign.java
package com.example.smscampaign;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Campaign_Details extends Activity
{
Cursor c;
SQLiteDatabase db;
String str, str1, str2, str3, str4, str5;
private Demo adapter;
private Demo1 adapter1;
static ListView listview, listview1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_demostration);
TextView txt1 = (TextView) findViewById(R.id.data);
listview = (ListView) findViewById(R.id.listview);
listview1 = (ListView) findViewById(R.id.ClosedCamapign);
PackageInfo pInfo = null;
try {
pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
txt1.setText(pInfo.versionName);
TextView txt = (TextView) findViewById(R.id.textnum1);
TextView txt2 = (TextView) findViewById(R.id.textnum2);
final DatabaseHelp info = new DatabaseHelp(this);
info.open();
ArrayList<String> arr1 = info.getData1();
final ArrayList<String> list1 = new ArrayList<String>();
for (int i = 0; i < arr1.size(); i++) {
list1.add(arr1.get(i));
}
int t1 = arr1.size();
txt.setText(Integer.toString(t1));
adapter1 = new Demo1(this, list1);
// adapter1.setNotifyOnChange(true);
listview.setAdapter(adapter1);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
TextView tv2 = (TextView) view.findViewById(R.id.text2);
String value1 = tv2.getText().toString();
c = info.fetchChildren1(value1);
if (c.moveToFirst())
str3 = (c.getString(c.getColumnIndex("name")));
str4 = (c.getString(c.getColumnIndex("message")));
str5 = (c.getString(c.getColumnIndex("Contacts")));
Toast.makeText(getApplication(), str5, Toast.LENGTH_LONG).show();
c.close();
Intent intent = new Intent(Campaign_Details.this, SmsSend.class);
intent.putExtra("key3", str3);
intent.putExtra("key4", str4);
intent.putExtra("key5", str5);
startActivity(intent);
}
});
ArrayList<String> arr = info.getData();
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < arr.size(); i++) {
list.add(arr.get(i));
}
int t = arr.size();
txt2.setText(Integer.toString(t));
adapter = new Demo(this, list);
// adapter.setNotifyOnChange(true);
listview1.setAdapter(adapter);
listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.text1);
String value = tv.getText().toString();
c = info.fetchChildren(value);
if (c.moveToFirst())
str = (c.getString(c.getColumnIndex("person_name")));
str1 = (c.getString(c.getColumnIndex("scale_person")));
str2 = (c.getString(c.getColumnIndex("Contacts_person")));
c.close();
Intent Sendintent = new Intent(Campaign_Details.this, SmsSend.class);
Sendintent.putExtra("key", str);
Sendintent.putExtra("key1", str1);
Sendintent.putExtra("key2", str2);
startActivity(Sendintent);
}
});
listview1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view,
int arg2, long arg3) {
// TODO Auto-generated method stub
TextView tv = (TextView) view.findViewById(R.id.text1);
String value = tv.getText().toString();
long ltt= Long.parseLong(value);
info.open();
info.deleteEntry(ltt);
info.close();
Toast.makeText(getApplication(), "Deleted", Toast.LENGTH_LONG).show();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.nextPage:
Intent i = new Intent(Campaign_Details.this, SmsSend.class);
SmsSend.conct.delete(0, SmsSend.conct.length());
startActivity(i);
break;
}
return true;
}
}
DataBaseHelp.java
package com.example.smscampaign;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Comment;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelp {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "person_name";
public static final String KEY_SCALE = "scale_person";
public static final String KEY_CONTACTS = "Contacts_person";
public static final String KEY_To_ROWID = "_id";
public static final String KEY_To_NAME = "name";
public static final String KEY_To_SCALE = "message";
public static final String KEY_To_CONTACTS = "Contacts";
private static final String DATABASE_NAME = "Himani";
static final String DATABASE_TABLE = "peopleTable";
private static final String TABLE_ADD_MESSAGE = "Add_Message";
private static final int DATABASE_VERSION = 2;
private DbHelper ourHepler;
private final Context ourContext;
SQLiteDatabase ourDatabase;
public class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
+ " TEXT NOT NULL, " + KEY_SCALE + " TEXT NOT NULL ,"
+ KEY_CONTACTS + ")");
String CREATE_ADD_MESSAGE = "CREATE TABLE " + TABLE_ADD_MESSAGE
+ "(" + KEY_To_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_To_NAME
+ " TEXT , " + KEY_To_SCALE + " TEXT ," + KEY_To_CONTACTS
+ ")";
db.execSQL(CREATE_ADD_MESSAGE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS peopleTable");
db.execSQL("DROP TABLE IF EXISTS Add_Message");
onCreate(db);
}
}
public DatabaseHelp(Context c) {
ourContext = c;
}
public DatabaseHelp open() throws SQLException {
ourHepler = new DbHelper(ourContext);
ourDatabase = ourHepler.getWritableDatabase();
return this;
}
public void close() {
ourHepler.close();
}
public long entryCreate(String name, String scale, String contacts) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_SCALE, scale);
cv.put(KEY_CONTACTS, contacts);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public long entryCreate1(String name, String scale, String contacts) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_To_NAME, name);
cv.put(KEY_To_SCALE, scale);
cv.put(KEY_To_CONTACTS, contacts);
return ourDatabase.insert(TABLE_ADD_MESSAGE, null, cv);
}
public ArrayList<String> getData() {
String[] col = new String[] { KEY_ROWID, KEY_NAME, KEY_SCALE,
KEY_CONTACTS };
Cursor c = ourDatabase.query(DATABASE_TABLE, col, null, null, null,
null, null);
String run = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iScale = c.getColumnIndex(KEY_SCALE);
int iMessage = c.getColumnIndex(KEY_CONTACTS);
ArrayList<String> newList = new ArrayList<String>();
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
newList.add(c.getString(iName));
}
return newList;
}
public ArrayList<String> getData1() {
String[] col = new String[] { KEY_To_ROWID, KEY_To_NAME, KEY_To_SCALE,
KEY_To_CONTACTS };
Cursor c = ourDatabase.query(TABLE_ADD_MESSAGE, col, null, null, null,
null, null);
String run = "";
int iRow = c.getColumnIndex(KEY_To_ROWID);
int iName = c.getColumnIndex(KEY_To_NAME);
int iScale = c.getColumnIndex(KEY_To_SCALE);
int iMessage = c.getColumnIndex(KEY_To_CONTACTS);
ArrayList<String> newList = new ArrayList<String>();
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
newList.add(c.getString(iName));
}
return newList;
}
public Cursor fetchChildren(String KEY_) {
Cursor c = ourDatabase.rawQuery("SELECT * FROM " + DATABASE_TABLE
+ " WHERE person_name = ?", new String[] { KEY_ });
return c;
}
public Cursor fetchChildren1(String KEY_) {
Cursor c = ourDatabase.rawQuery("SELECT * FROM " + TABLE_ADD_MESSAGE
+ " WHERE name = ?", new String[] { KEY_ });
return c;
}
public String getScale(long l) {
// TODO Auto-generated method stub
String[] col = new String[] { KEY_ROWID, KEY_NAME, KEY_SCALE,
KEY_CONTACTS };
Cursor c = ourDatabase.query(DATABASE_TABLE, col, KEY_ROWID + "-" + l,
null, null, null, null);
if (c != null) {
c.moveToFirst();
String scale = c.getString(2);
return scale;
}
return null;
}
public String getScale1(long l) {
// TODO Auto-generated method stub
String[] col = new String[] { KEY_To_ROWID, KEY_To_NAME, KEY_To_SCALE,
KEY_To_CONTACTS };
Cursor c = ourDatabase.query(TABLE_ADD_MESSAGE, col, KEY_To_ROWID + "-"
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String scale = c.getString(2);
return scale;
}
return null;
}
public String getName(long l) {
// TODO Auto-generated method stub
String[] col = new String[] { KEY_ROWID, KEY_NAME, KEY_SCALE,
KEY_CONTACTS };
Cursor c = ourDatabase.query(DATABASE_TABLE, col, KEY_ROWID + "-" + l,
null, null, null, null);
if (c != null) {
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
public String getName1(long l) {
// TODO Auto-generated method stub
String[] col = new String[] { KEY_To_ROWID, KEY_To_NAME, KEY_SCALE,
KEY_To_CONTACTS };
Cursor c = ourDatabase.query(TABLE_ADD_MESSAGE, col, KEY_To_ROWID + "-"
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
public String getContacts(long l) {
// TODO Auto-generated method stub
String[] col = new String[] { KEY_ROWID, KEY_NAME, KEY_SCALE,
KEY_CONTACTS };
Cursor c = ourDatabase.query(DATABASE_TABLE, col, KEY_ROWID + "-" + l,
null, null, null, null);
if (c != null) {
c.moveToFirst();
String contacts = c.getString(3);
return contacts;
}
return null;
}
public String getContacts1(long l) {
// TODO Auto-generated method stub
String[] col = new String[] { KEY_To_ROWID, KEY_To_NAME, KEY_To_SCALE,
KEY_To_CONTACTS };
Cursor c = ourDatabase.query(TABLE_ADD_MESSAGE, col, KEY_To_ROWID + "-"
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String contacts = c.getString(3);
return contacts;
}
return null;
}
public void updateEntry(long lt, String mName, String mScale,
String mContatcs) {
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_NAME, mName);
cvUpdate.put(KEY_SCALE, mScale);
cvUpdate.put(KEY_CONTACTS, mContatcs);
ourDatabase
.update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "-" + lt, null);
}
public void updateEntry1(long lt, String mName, String mScale,
String mContatcs) {
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_To_NAME, mName);
cvUpdate.put(KEY_To_SCALE, mScale);
cvUpdate.put(KEY_To_CONTACTS, mContatcs);
ourDatabase.update(TABLE_ADD_MESSAGE, cvUpdate,
KEY_To_ROWID + "-" + lt, null);
}
public void deleteEntry1(String ltt) throws SQLException {
// TODO Auto-generated method stub
ourDatabase.delete(TABLE_ADD_MESSAGE, KEY_To_ROWID + "=" + ltt, null);
}
public void deleteEntry(String value) throws SQLException { //this is function that is used in lonclick
// TODO Auto-generated method stub
ourDatabase.delete(DATABASE_TABLE, KEY_NAME + "=" + value, null);
}
}
答案 0 :(得分:0)
因为您正在显示数据库本身的数据,所以创建一个类似于:
的类1. class Data {
int id; String name;
}
2. Modify the method `getData()` or `getData1()` whichever it is, in order to return
a set of objects of type Data, that would contain the id, and corresponding name.
3. Create a custom adapter, that displays only the names
4. In the onLongItemClick listener get the value of the id clicked on like:
Data dObj = list1.get(position);
int id = dObj.getId();
5. Pass the id to the db method that performs the delete
6. The delete query should be one that compares the id , and you are done.