2013-03-29 90 views
0

我有一個自定義列表視圖,它是從數據庫中檢索的。現在,我無法理解的是如何從列表中刪除項目:在Google上搜索如果沒有標準解決方案,我看到了不同的問題,所以我對此有所懷疑。我怎樣才能從一個CustomListView中使用異步任務刪​​除一行?從自定義列表視圖中刪除項目

這裏是Leggi_Pizzaiolo活動(在這裏我顯示列表視圖):

public class Leggi_Pizzaiolo extends Activity 
{ 
    // Progress Dialog 
    private ProgressDialog pDialog; 
    public List list = new LinkedList(); 
    // Creating JSON Parser object 
    JSONParser jParser = new JSONParser(); 

    ArrayList<HashMap<String, String>> productsList; 

    // url to get all products list 
    private static String url_all_products = "http://10.0.2.2/tesina/Leggi_Pizzaiolo.php"; 

    // JSON Node names 
    private static final String TAG_SUCCESS = "Esito"; 
    private static final String TAG_PRODUCTS = "comande"; 
    private static final String TAG_PID = "ID"; 
    private static final String TAG_NAME = "Nome"; 
    private static final String TAG_TABLE = "Tavolo"; 
    public ListView lv; 
    // products JSONArray 
    JSONArray products = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.ordini_cuoco); 

     // Hashmap for ListView 
     productsList = new ArrayList<HashMap<String, String>>(); 

     // Loading products in Background Thread 

     // Get listview 
     lv = (ListView)findViewById(R.id.lista); 

     new LoadAllProducts().execute(); 

    } 

    /** 
    * Background Async Task to Load all product by making HTTP Request 
    * */ 
    class LoadAllProducts extends AsyncTask<String, String, String> 
    { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(Leggi_Pizzaiolo.this); 
      pDialog.setMessage("Loading products. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     /** 
     * getting All products from url 
     * */ 
     protected String doInBackground(String... args) { 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      // getting JSON string from URL 
      JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params); 

      // Check your log cat for JSON reponse 
      Log.d("All Products: ", json.toString()); 

      try { 
       // Checking for SUCCESS TAG 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        // products found 
        // Getting Array of Products 
        products = json.getJSONArray(TAG_PRODUCTS); 

        // looping through All Products 
        for (int i = 0; i < products.length(); i++) { 
         JSONObject c = products.getJSONObject(i); 

         // Storing each json item in variable 
         int id = c.getInt(TAG_PID); 
         String name = c.getString(TAG_NAME); 
         int Tavolo= c.getInt(TAG_TABLE); 

         list.add(new Comanda(name, id, Tavolo)); 

        } 
       } else { 
        // no products found 
        // Launch Add New product Activity 
        Intent i = new Intent(getApplicationContext(), 
          Listino.class); 
        // Closing all previous activities 
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) 
     { 
      // dismiss the dialog after getting all products 
      pDialog.dismiss(); 
      // updating listview 
      final ComandaCursorAdapter adapter = new ComandaCursorAdapter(Leggi_Pizzaiolo.this, R.layout.comanda_cuoco, list); 
      lv.setAdapter(adapter); 


     } 



    } 


} 

這是CursorAdapter的:

public class ComandaCursorAdapter extends ArrayAdapter<Comanda> 
{ 

public ComandaCursorAdapter(Context context, int comandaCuoco, List list) { 
    super(context, comandaCuoco, list); 
    // TODO Auto-generated constructor stub 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = inflater.inflate(R.layout.comanda_cuoco, null); 

    TextView Nome = (TextView)convertView.findViewById(R.id.Comanda); 
    TextView Tavolo = (TextView)convertView.findViewById(R.id.Tavolo); 
    TextView Codice = (TextView)convertView.findViewById(R.id.Codice); 

    Comanda c = getItem(position); 

    Nome.setText(c.getNome()); 
    Tavolo.setText("Tavolo: " + Integer.toString(c.getTavolo())); 
    Codice.setText("Codice: " + Integer.toString(c.getCodice())); 


    return convertView; 

} 

這是對象Comanda:

public class Comanda { 

    private String Nome; 
    private int Codice; 
    private int Tavolo; 

    public Comanda(String Nome, int Codice, int Tavolo) 
    { 
     this.Nome = Nome; 
     this.Codice = Codice; 
     this.Tavolo = Tavolo; 

    } 

    public String getNome() 
    { 
     return Nome; 
    } 

    public void setNome(String Nome) 
    { 
     this.Nome = Nome; 
    } 

    public int getCodice() 
    { 
     return Codice; 
    } 

    public void setCodice(int Codice) 
    { 
     this.Codice = Codice; 
    } 

    public int getTavolo() 
    { 
     return Tavolo; 
    } 

    public void setTavolo(int Tavolo) 
    { 
     this.Tavolo = Tavolo; 
    } 


} 

現在,我必須在Leggi_Pizzaiolo活動中聲明setOnItemClickListener?我是否需要在課堂上實施刪除方法?請讓我知道如何...

回答

1

,我無法理解的是如何從列表

沒有刪除一個項目,通常創建例如OnItemClickListener()到能夠處理點擊的ListView事件。然後在onItemClick()你有參數int position返回項目在適配器中的位置。現在,你需要從列表中刪除的項目,然後進行

list.remove(position) 

,然後你需要調用

adapter.notifyDataSetChanged(); 

通知該數據源已經改變適配器。

注意:爲了更加舒適,您可以點擊ListItem後顯示一些AlertDialog的按鈕,用於刪除或不刪除。

+0

哦,你再次! :))是的我也認爲OnItemCliclListener,但我不應該在我的適配器或只是與list.remove(位置)聲明任何刪除功能? – Eulante

+0

@Eulante不管你如何說,list.remove(position)然後調用notifyDataSetChanged(); – Sajmon

+0

我愛你謝謝!真的很有用! :) – Eulante

0

試試這個

lv.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
     AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this); 
     adb.setTitle("Delete?"); 
     adb.setMessage("Are you sure you want to delete " + position); 
     final int positionToRemove = position; 
     adb.setNegativeButton("Cancel", null); 
     adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       MyDataObject.remove(positionToRemove); 
       adapter.notifyDataSetChanged(); 
      }}); 
     adb.show(); 
     } 
    }); 

其中LV是您的列表視圖,亞行是一個對話框,mydataobject是您正在使用填充您的ListView和適配器適配器集合。現在

相關問題