2014-04-04 187 views
-1

这段代码的写法是允许用户输入要搜索的企业名称。一旦找到该名称,就会显示出来。如果单击搜索按钮而没有指定要搜索的任何内容,则会显示所有记录。
当在列表视图中选择一个项目时,我想删除该项目。虽然该项目正在被删除,但一旦通过搜索打开重新生成列表,它仍然会重新出现。
这怎么解决?删除列表视图中的项目

 package com.example.farejudgeapp; 

     import java.util.ArrayList; 
     import java.util.List; 
     import android.app.AlertDialog; 
     import android.content.DialogInterface; 
     import android.database.Cursor; 
     import android.database.sqlite.SQLiteDatabase; 
     import android.os.Bundle; 
     import android.view.View; 
     import android.widget.AdapterView; 
     import android.widget.AdapterView.OnItemClickListener; 
     import android.widget.EditText; 
     import android.widget.ListView; 

     public class ListEstablishmentsActivity extends Base_Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list_establishments); 
    }//closes onCreate method 


    //reading data from database 
    public void getEstablishmentMatches(View view){ 


     EstablishmentHelper helper = new EstablishmentHelper(this); 
     final SQLiteDatabase db = helper.getReadableDatabase(); 

     //These are the rows from the Establishment's database that would be retrieved 
     String columns1 [] = {"establishmentName", "establishmentType", "foodServed", "location", "contactNumber"}; 


     final ListView listview = (ListView)findViewById(R.id.searchEstablishmentsListView); 
     final List<String> establishments = new ArrayList<String>(); 
     String selection = "establishmentName LIKE ?"; 


     //Capture data entered in text box 
     EditText establishmentSearchText = (EditText)findViewById(R.id.editTextSearchEstablishment); 
     String establishmentSearchTextString = establishmentSearchText.getText().toString(); 

     //Searching for establishment 
     String selectionArgs [] = {"%" + establishmentSearchTextString + "%"}; 

     //Querying database 
     Cursor c = db.query("establishments", columns1, selection, selectionArgs, null, null, null); 

     //Loops to add data from database to the list view 
     c.moveToFirst(); 
     while(!c.isAfterLast()) 
     { 

      final String establishmentName = c.getString(c.getColumnIndex("establishmentName")); 
      final String establishmentType = c.getString(c.getColumnIndex("establishmentType")); 
      final String foodServed = c.getString(c.getColumnIndex("foodServed")); 
      final String location = c.getString(c.getColumnIndex("location")); 
      final String contactNumber = c.getString(c.getColumnIndex("contactNumber")); 
      final String details = "Establishment Name: " + establishmentName + "\n" + "Establishment Type: " + establishmentType + "\n" + "Food Served: " + foodServed + "\n" + "Location: " + location + "\n" + "Contact Number: " + contactNumber ; 
      //Show various column data//    
      establishments.add(details); 

      c.moveToNext(); 
     }//close while loop 




      //Create an empty adapter that would be used to display the loaded data. 
      final EstablishmentAdapter adapter = new EstablishmentAdapter(this, android.R.layout.simple_list_item_1, establishments); 
      listview.setAdapter(adapter); 

      //Listens for when an item is clicked. 
      listview.setOnItemClickListener(new OnItemClickListener() { 


       //Prompts user to delete an establishment when an item is clicked. 
       @Override 
        public void onItemClick(AdapterView<?> parent, final View view, 
        final int position, final long id) { 
        // Create and display the Alert dialog when next is clicked 



        new AlertDialog.Builder(ListEstablishmentsActivity.this) 
          .setTitle(" Delete Establishment ") 
          .setMessage( 
            "Are you sure you want to delete the selected establishment?") 
          .setNeutralButton("No", 
            new DialogInterface.OnClickListener() { 

             public void onClick(DialogInterface Dialog, 
               int which) { 
              // do nothing - it will just close when clicked 
             }//closes onClick method 
            }) 
          .setPositiveButton("Yes", 
            new DialogInterface.OnClickListener() { 



             //Deletes selected establishment 
             @Override 
             public void onClick(DialogInterface Dialog, int which) { 
              /* 
              * http://androidforbegineers.blogspot.com/2013/08/delete-row-item-in-listview-android.html 
              */ 
              //Deletes selected establishment from database 

              //Captures id of the list view item that was selected to be deleted 
              final long deleteId = id; 
              db.execSQL("DELETE FROM establishments WHERE id=" + deleteId); 
              //db.delete("establishments", "id="+deleteId, null); 

              establishments.remove(deleteId); 
              //establishments.remove(position); 


              android.util.Log.w(this.getClass().getName(), 
                " Establishment Deleted"); 


              //Removes item from list view 
              establishments.remove(position); 
              adapter.notifyDataSetChanged(); 
              adapter.notifyDataSetInvalidated(); 

             }//closes onClick Method 

            }).show(); 

         }//Closes onItemClick method 

      });//Closes setOnItemClickListener 


    }//closes getEstablishmentMatches method 

}//closes ListEstablishmentsActivity class 
+0

有你在'adapter.remove(项目)'试过吗? – nKn

回答

2

您从数组列表中删除的项目,而不是适配器,它仍持有的所有项目..

//Removes item from list view 
establishments.remove(position); 

但是,你需要从适配器还删除 -

adapter.remove(item); 
adapter.notifyDataSetChanged(); 
adapter.notifyDataSetInvalidated(); 
+0

我试过,但它仍然不起作用 – user3498995

0
establishments.remove(deleteId); 

从列表中删除元素编号i,我的意思是0 =第一个,1 =第二个等等。

要通过ID在数据库中删除是好的,但除去从列表尝试

establishments.remove(position); 
+0

在什么enhaces你的答案@ kanak-sony的?这是她的一个子集信息... – nKn

+0

因为她说他正在移除位置,但实际上原来的代码已被评论,我指出唯一的问题是使用位置而不是列表的id。并且...如果列表引用是相同的(这是这种情况),调用adapter.remove并不是必需的 –

+0

有没有一种方法在选择项目时访问while循环中的字符串? – user3498995

相关问题