嘿,伙计们每次在"删除"时都会遇到从列表视图中删除对象的问题。按下按钮。现在我已经用简单的吐司消息替换了那些意味着删除功能的代码,只是为了让我知道按钮工作,但是屏幕上似乎没有任何东西出现,所以我认为它不起作用。当我单击删除按钮时,日志猫上没有出现任何错误。
我已经按照Android的解析教程开始创建这个简单的列表视图,但想要添加更多功能。
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listView = (ListView) findViewById(R.id.listview);
// Pass the results into an ArrayAdapter
adapter = new ArrayAdapter<String>(SelectCourse.this,
R.layout.listview_item, R.id.text);
// Retrieve object "name" from Parse.com database
for (ParseObject course : ob) {
adapter.add((String) course.get("name"));
}
// Binds the Adapter to the ListView
listView.setAdapter(adapter);
// Close the progress dialog
mProgressDialog.dismiss();
// Capture button clicks on ListView items
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
// Send single item click data to SingleItemView Class
Intent i = new Intent(SelectCourse.this,
SingleItemView.class);
// Pass data "name" followed by the position
i.putExtra("name", ob.get(position).getString("name")
.toString());
// Open SingleItemView.java Activity
startActivity(i);
//locate delete button
deleteBtn = (Button) findViewById(R.id.delete);
//create on click listener for the delete button
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView1 = (ListView) parentRow.getParent();
final int position = listView1.getPositionForView(parentRow);
Toast.makeText(getApplicationContext(), "Successfully Deleted", Toast.LENGTH_LONG).show();
}
});
}
});
答案 0 :(得分:0)
首先,如果您不在onclicklistener
的{{1}}中添加按钮itemOnClickListener
,情况会更好。
相反,覆盖适配器的listview
方法,您应该使用以下内容在按钮上设置getView
,如下所示
onclicklistener