0

我创建了一个ListView并将其绑定到适配器以显示表中的数据。它工作得很好。我可以选择一行,它会显示我正在选择的产品名称。现在,我ImageView在布局中添加了一个将充当每行上的删除按钮的按钮。我的问题是我不知道如何添加代码来制作它,以便当用户选择ImageView(删除按钮)时,它将删除该行。我已经搜索并找到了很多关于这个主题的文章并尝试了很多,但没有一个真正适用于我的代码。我需要创建一个getView函数吗?我也试过插入getTag(),但我无法让它工作。您能帮我提供一个可以与我的简单代码一起使用的示例代码或为我指明正确的方向吗?这是我的代码:

private void displayListView() {       
    prodinputHelper = new DBAdaptorProductInput(this);   
    prodinputHelper.open();     
    Cursor cursor = prodinputHelper.fetchAllProductInput();     
    // The desired columns to be bound   
    String[] columns = new String[] {     
            DBAdaptorProductInput.KEY_PRODUCTTYPE,     
            DBAdaptorProductInput.KEY_PRODUCTNAME,     
            DBAdaptorProductInput.KEY_MANUFACTURER,     
            DBAdaptorProductInput.KEY_VISC40,     
            DBAdaptorProductInput.KEY_VISC100,     
            DBAdaptorProductInput.KEY_VI,     
            DBAdaptorProductInput.KEY_DEN15C,     
            DBAdaptorProductInput.KEY_VISCTEXT,     
            DBAdaptorProductInput.KEY_BASEOILTYPE,     
            DBAdaptorProductInput.KEY_BASEOIL,     
            DBAdaptorProductInput.KEY_ADDITIVES,     
            DBAdaptorProductInput.KEY_OTHERADDITIVES,     
            DBAdaptorProductInput.KEY_THICKENER,     
            DBAdaptorProductInput.KEY_NLGI,     
            DBAdaptorProductInput.KEY_COMMENT,     
            DBAdaptorProductInput.KEY_PACKAGES,     
            DBAdaptorProductInput.KEY_AREA,     
    };     
    // the XML defined views which the data will be bound to   
    int[] to = new int[] {      
            R.id.code,     
            R.id.name,     
            R.id.manufacturer,     
            R.id.visc40,     
            R.id.visc100,     
            R.id.viscindex,     
            R.id.den15c,     
            R.id.visctext,     
            R.id.baseoiltype,     
            R.id.baseoil,     
            R.id.additives,     
            R.id.otheradditives,     
            R.id.thickener,     
            R.id.nlgi,     
            R.id.comments,     
            R.id.packages,     
            R.id.area,     
    };     
    // create the adapter using the cursor pointing to the desired data    
    //as well as the layout information   
    dataAdapter = new SimpleCursorAdapter(     
            this, R.layout.activity_product_review_info, cursor, columns, to, 0);     

    ListView listView = (ListView) findViewById(R.id.listView1);   
    // Assign adapter to ListView   
    listView.setAdapter(dataAdapter); 

    //SetOnItemClickListener for the ListView
    listView.setOnItemClickListener(new OnItemClickListener() {  
    @Override   
    public void onItemClick(AdapterView<?> listView, View view,       
            int position, long id) {    

        // Get the cursor, positioned to the corresponding row in the result set    
        Cursor cursor = (Cursor) listView.getItemAtPosition(position);      
        // Get the Customer Name from this row in the database.    
        String countryCode = cursor.getString(cursor.getColumnIndexOrThrow("ProductName"));    

        Toast.makeText(getApplicationContext(), countryCode, Toast.LENGTH_SHORT).show();      

        }   

    });
}
4

1 回答 1

1

您需要有一个扩展BaseAdapter或扩展的自定义适配器SimpleCursorAdapter
在适配器的getView()方法中onClickListener为您的ImageView.

于 2013-05-23T21:56:53.537 回答