问题是单击它的列表项没有被删除但是它后面或前面的项被删除(对于list2.get(d)或list2.get(d-1))如何获取单击要删除的项目?)
Intent intent=getIntent();
k=intent.getStringExtra("this is a");
list=(ListView)findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
d=i;
AlertDialog.Builder dialog=new AlertDialog.Builder(Main3Activity.this);
dialog.setMessage("this is a");
dialog.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int l){
ParseQuery<ParseObject> b=new ParseQuery<ParseObject>("message");
b.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e)
{
if(e==null) {
for (ParseObject n : objects) {
if (n.getString("message").equals(list2.get(d))&&(n.getString("receiver").equals(ParseUser.getCurrentUser().getUsername().toString())&&n.getString("sender").equals(k)))
{
try {
Toast.makeText(Main3Activity.this,list2.get(d+1), Toast.LENGTH_SHORT).show();
n.delete();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
else
{
Log.i("this is a",e.getMessage());
}
}
});
list2.remove(d);
a.notifyDataSetChanged();
答案 0 :(得分:0)
this
指的是onItemClickListener上下文。您可以通过调用getApplicationContext()
使用应用程序上下文,或者如果需要本地上下文,请使用CLASSNAME.this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent=getIntent();
list=(ListView)findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
AlertDialog.Builder dialog=new AlertDialog.Builder(getApplicationContext());
}
});
OR
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent=getIntent();
list=(ListView)findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
}
});