我有一个Adapter
课程,其中包含OnClickListener
和OnLongClickListener
。 OnClickListener
工作正常但OnLongClickListener
不起作用。我没有找到原因。我附加了我的适配器类的代码
class RemainderAdapter extends BaseAdapter implements Filterable{
private Context context;
private ArrayList<AccountDetails> rList;
private AccountDetails aDetails;
DBHelper myDb;
ArrayList<AccountDetails> suggArray= new ArrayList<AccountDetails>();
RemainderAdapter(Context context, ArrayList<AccountDetails> rList) {
this.context = context;
this.rList = rList;
this.suggArray = rList;
DBHelper myDb=new DBHelper(context);
}
@Override
public int getCount() {
return suggArray.size();
}
@Override
public Object getItem(int position) {
return suggArray.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewItem {
private TextView aName,uName,circle,date;
ViewItem(View v) {
aName = (TextView) v.findViewById(R.id.aName);
uName = (TextView) v.findViewById(R.id.uName);
circle = (TextView) v.findViewById(R.id.circle);
date = (TextView) v.findViewById(R.id.date);
}
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewItem holder=null;
if(convertView==null){
LayoutInflater inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.list_item,parent,false);
holder=new ViewItem(convertView);
convertView.setTag(holder);
}else{
holder=(ViewItem) convertView.getTag();
}
AccountDetails pd= suggArray.get(position);
holder.aName.setText(pd.getaName());
holder.uName.setText(pd.getuName());
holder.date.setText(pd.getDate().toString().substring(0, 10));
holder.circle.setText(pd.getaName().toString().substring(0, 1));
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ActivityFive.class);
aDetails = suggArray.get(position);
intent.putExtra("Obj", aDetails);
context.startActivity(intent);
}
});
convertView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
new AlertDialog.Builder(context).
setTitle("Delete").setMessage("do Do you want Delete this item ?").setIcon(R.drawable.delete)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
aDetails = suggArray.get(position);
myDb.deleteRow(aDetails);
Log.e("Values", "Position : " + position + ", Account Value : " + suggArray.get(position).getaName());
context.startActivity(new Intent(context, ActivityThree.class));
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
return true;
}
});
return convertView;
}
public Filter getFilter() {
return mFilter;
}
public Filter mFilter = new Filter() {
@SuppressLint("DefaultLocale")
@Override
protected FilterResults performFiltering(final CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final ArrayList<AccountDetails> nlist = new ArrayList<AccountDetails>();
String filterableString ;
if(constraint!=null && constraint.length()>0) {
for (AccountDetails accountDetails : rList) {
filterableString = accountDetails.getaName().toString();
if (filterableString.toLowerCase().contains(filterString)) {
//Log.e("Values", "Model Value : "+filterableString + " , String Value : " + constraint);
nlist.add(accountDetails);
}
}
results.values = nlist;
results.count = nlist.size();
}else{
results.values = rList;
results.count = rList.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
suggArray = (ArrayList<AccountDetails>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
}
这是日志
05-14 14:01:29.278 2977-2977/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sushanta.reminderpwd, PID: 2977
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.sushanta.reminderpwd.DBHelper.deleteRow(com.sushanta.reminderpwd.AccountDetails)' on a null object reference
at com.sushanta.reminderpwd.RemainderAdapter$2$2.onClick(RemainderAdapter.java:95)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-14 14:01:44.162 1298-1334/? E/InputDispatcher: channel 'd47d530 com.sushanta.reminderpwd/com.sushanta.reminderpwd.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
05-14 14:01:44.163 1298-1334/? E/InputDispatcher: channel '2301c89 com.sushanta.reminderpwd/com.sushanta.reminderpwd.ActivityThree (server)' ~ Channel is unrecoverably broken and will be disposed!
05-14 14:01:44.163 1298-1334/? E/InputDispatcher: channel 'f9bf7de com.sushanta.reminderpwd/com.sushanta.reminderpwd.ActivityThree (server)' ~ Channel is unrecoverably broken and will be disposed!
05-14 14:01:44.183 1298-2881/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa20cd6d0
05-14 14:01:44.263 950-1004/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
05-14 14:26:48.692 1298-1314/? E/BluetoothAdapter: Bluetooth binder is null
05-14 14:26:48.698 1298-1314/? E/KernelCpuSpeedReader: Failed to read cpu-freq
java.io.FileNotFoundException: /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileInputStream.
<init>
(FileInputStream.java:76)
at java.io.FileInputStream.
<init>
(FileInputStream.java:103)
at java.io.FileReader.
<init>
(FileReader.java:66)
at com.android.internal.os.KernelCpuSpeedReader.readDelta(KernelCpuSpeedReader.java:49)
at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8002)
at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1366)
at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
at java.io.FileInputStream.
<init>
(FileInputStream.java:76)
at java.io.FileInputStream.
<init>
(FileInputStream.java:103)
at java.io.FileReader.
<init>
(FileReader.java:66)
at com.android.internal.os.KernelCpuSpeedReader.readDelta(KernelCpuSpeedReader.java:49)
at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8002)
at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1366)
at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
05-14 14:26:48.699 1298-1314/? E/KernelUidCpuTimeReader: Failed to read uid_cputime
java.io.FileNotFoundException: /proc/uid_cputime/show_uid_stat: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileInputStream.
<init>
(FileInputStream.java:76)
at java.io.FileInputStream.
<init>
(FileInputStream.java:103)
at java.io.FileReader.
<init>
(FileReader.java:66)
at com.android.internal.os.KernelUidCpuTimeReader.readDelta(KernelUidCpuTimeReader.java:71)
at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8031)
at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1366)
at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
at java.io.FileInputStream.
<init>
(FileInputStream.java:76)
at java.io.FileInputStream.
<init>
(FileInputStream.java:103)
at java.io.FileReader.
<init>
(FileReader.java:66)
at com.android.internal.os.KernelUidCpuTimeReader.readDelta(KernelUidCpuTimeReader.java:71)
at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8031)
at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1366)
at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
答案 0 :(得分:1)
尝试使用提及ListView的Main_Activity.java。
ListView list = findViewById(R.id.listView);
list.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// put your code here.
return true;
}
我希望这段代码可以帮到你。
答案 1 :(得分:0)
最好在onItemLongClickListener
listView
listeView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
return false;
}
});
答案 2 :(得分:0)
在调用setOnLongClickListener中的deleteRow()之前初始化您的DBHelper对象“myDb”。
答案 3 :(得分:0)
根据您的日志,您将获得一个空指针。检查以确保您的数据库有数据。很可能它不会,如果它是一个错误的位置,它会抛出索引超出范围的异常。因此,请检查数据的初始化并确保其中包含内容。