-1

有人可以给我一个删除列表视图中选定项目的示例代码吗?我正在使用简单的适配器来存储数据。我的代码是..

static final String KEY_ITEM = "finance"; // parent node
static final String KEY_ID = "finance";
static final String KEY_NAME = "company";
static final String KEY_COST = "high";
static final String KEY_DESC = "volume";
static final String KEY_SYMBOL="symbol";
static final String KEY = "low";
private String selectedItem; 
private ListAdapter adapter;  
Context context;
ListView lv;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView titleBar = (TextView)getWindow().findViewById(android.R.id.title);
   titleBar.setTextColor(Color.GREEN);
   final ArrayList<HashMap<String, String>> menuItems = new    ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);

    for (int i = 0; i < nl.getLength(); i++) {

      // HashMap<String, String> map = new HashMap<String, String>();

       Node theAttribute;
        Element e = (Element) nl.item(i);


         NodeList nl1=e.getElementsByTagName(KEY_ID);
         System.out.println("keyId"+nl1.getLength());
         for(int j=0;j<nl1.getLength();j++)
         {
             Element e1 = (Element) nl1.item(j);
             HashMap<String, String> map = new HashMap<String, String>();
             NodeList n = e1.getElementsByTagName(KEY_NAME);

              for (int k = 0; k < n.getLength(); k++) {

                  Element e2 = (Element) n.item(k);
            //  System.out.println("node Title value"+e2.getNodeName());
                 NamedNodeMap attributes2 = e2.getAttributes();
                // System.out.println("attrlength"+attributes2.getLength());
                 for (int a = 0; a < attributes2.getLength(); a++) 
                 {
                         theAttribute = attributes2.item(a);

                     String s=theAttribute.getNodeValue();
                   //  lblName.setTypeface(hindiFont);
                        //s = s.replaceAll("[-;#39&amp:,]","");
                         map.put(KEY_NAME,s);

                 }

              }
              NodeList n4 = e1.getElementsByTagName(KEY_SYMBOL);
              // System.out.println("title "+n.getLength());
                for (int k = 0; k < n4.getLength(); k++) {

                    Element e2 = (Element) n4.item(k);
              //    System.out.println("node snippet value"+e2.getNodeName());
                   NamedNodeMap attributes2 = e2.getAttributes();

                   for (int a = 0; a < attributes2.getLength(); a++) 
                   { 
                    // HashMap<String, String> map = new HashMap<String, String>();
                         theAttribute = attributes2.item(a);
                         String s=theAttribute.getNodeValue();


                           map.put(KEY_SYMBOL,s);
                          // menuItems.add(map);
                   }

                } 

              NodeList n1 = e1.getElementsByTagName(KEY_COST);
            // System.out.println("title "+n.getLength());
              for (int k = 0; k < n1.getLength(); k++) {

                  Element e2 = (Element) n1.item(k);
            //    System.out.println("node Url value");
                 NamedNodeMap attributes2 = e2.getAttributes();
              //   System.out.println("attrlength"+attributes2.getLength());
                 for (int a = 0; a < attributes2.getLength(); a++) 
                 {  
                     //HashMap<String, String> map = new HashMap<String, String>();
                         theAttribute = attributes2.item(a);

                         map.put(KEY_COST,theAttribute.getNodeValue());
                 }}

                 NodeList n2 = e1.getElementsByTagName(KEY_DESC);
                // System.out.println("title "+n.getLength());
                  for (int k = 0; k < n2.getLength(); k++) {

                      Element e2 = (Element) n2.item(k);
                //    System.out.println("node snippet value"+e2.getNodeName());
                     NamedNodeMap attributes2 = e2.getAttributes();

                     for (int a = 0; a < attributes2.getLength(); a++) 
                     { 
                        // HashMap<String, String> map = new HashMap<String, String>();
                           theAttribute = attributes2.item(a);
                           String s=theAttribute.getNodeValue();


                             map.put(KEY_DESC,s);
                            // menuItems.add(map);
                     }

                  }   NodeList n3 = e1.getElementsByTagName(KEY);
                // System.out.println("title "+n.getLength());
                  for (int k = 0; k < n3.getLength(); k++) {

                      Element e2 = (Element) n3.item(k);
                //    System.out.println("node snippet value"+e2.getNodeName());
                     NamedNodeMap attributes2 = e2.getAttributes();

                     for (int a = 0; a < attributes2.getLength(); a++) 
                     { 
                        // HashMap<String, String> map = new HashMap<String, String>();
                           theAttribute = attributes2.item(a);
                           String s=theAttribute.getNodeValue();


                             map.put(KEY,s);
                            // menuItems.add(map);
                     }

                  }  

                  menuItems.add(map);
         }

    }

 // Adding menuItems to ListView

         adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item,
                new String[] { KEY_NAME, KEY_DESC, KEY_COST,KEY,KEY_SYMBOL }, new int[] {
                        R.id.name, R.id.desciption, R.id.cost,R.id.low,R.id.symbol }){


            };


        setListAdapter(adapter);


        // selecting single ListView item
         lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {


            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
            /*  String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
                String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
                String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
                */
                // Starting new intent
                /*Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                in.putExtra(KEY_NAME, name);
                in.putExtra(KEY_COST, cost);
                in.putExtra(KEY_DESC, description);
                startActivity(in);*/
                 Toast.makeText(getApplicationContext(), "on clicked",Toast.LENGTH_LONG).show();
            }
        });
         // Create the listener for long item clicks
        OnItemLongClickListener itemLongListener = new OnItemLongClickListener() {


            public boolean onItemLongClick(AdapterView<?> parent, View v, final int position, long rowid) {
                final View view=v;
                // Store selected item in global variable 
                selectedItem = parent.getItemAtPosition(position).toString();
       //  Toast.makeText(getApplicationContext(), "select item"+selectedItem,Toast.LENGTH_LONG).show();

               AlertDialog.Builder builder = new AlertDialog.Builder(AndroidXMLParsingActivity.this);
                builder.setMessage("Do you want to remove " + "?");
                final int positionToRemove = position;
                builder.setCancelable(false);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {



                    public void onClick(DialogInterface dialog, int which) {
                          adapter.remove(selectedItem);
                          adapter.notifyDataSetChanged();

                        Toast.makeText(
                                getApplicationContext(),
                                selectedItem + " has been removed.",
                                Toast.LENGTH_SHORT).show();
                        dialog.cancel();
                    }


                });
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {


                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                // Create and show the dialog
                builder.show();

                // Signal OK to avoid further processing of the long click
                return true;
            }
        };



        getListView().setOnItemLongClickListener(itemLongListener);
}

}

它显示了这些行中的错误

   adapter.remove(selectedItem);
 adapter.notifyDataSetChanged();

它显示 ListAdapter 添加到强制转换适配器的类型未定义方法 remove(String)

请帮我提供示例代码!!!!提前致谢!!!!!!!!

4

1 回答 1

0

从 ListAdapter SimpleAdapter 更改。

private SimpleAdapter adapter;

使 menuItems 全局化。

然后你可以做

menuItems.remove(selectedItem);
adapter.notifyDataSetChanged();
于 2012-11-24T10:41:08.003 回答