0

可能重复:
如何通过在edittext中写入来获取xml解析中的特定值

这段代码不编译这段代码有什么问题?不是说应用程序意外停止我做的一切都正确吗?请检查它我想添加一个EditText输入并从XML文件中检查它的值并显示在屏幕上我按照 本教程

      static final String URL = "http://api.androidhive.info/pizza/?format=xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
       EditText et = (EditText)findViewById(R.id.editText1);

               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);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
           if(parser.getValue(e, KEY_ID).equals(et.getText().toString())){

            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
            map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
            map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

            // adding HashList to ArrayList
            menuItems.add(map);
            }

      }
        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item,
                new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                        R.id.name, R.id.desciption, R.id.cost });

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();
                // listening to single listitem click
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            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);

            }
        });
    }
}

main.xml 文件-->>

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical">
<!-- Main ListView 
     Always give id value as list(@android:id/list)
-->

  <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="596dp" />



    <EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

4

2 回答 2

0

试试这个代码,

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
               EditText et = (EditText)findViewById(R.id.editText1);

                       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);
                // looping through all item nodes <item>
                for (int i = 0; i < nl.getLength(); i++) {
                    // creating new HashMap
                    Element e = (Element) nl.item(i);
                    // adding each child node to HashMap key => value
                   if(parser.getValue(e, KEY_ID).equals(et.getText().toString())){
                       String name = parser.getValue(e, KEY_NAME);
                       String cost = "Rs." + parser.getValue(e, KEY_COST);
                       String description = parser.getValue(e, KEY_DESC);
                       Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                       in.putExtra(KEY_NAME, name);
                       in.putExtra(KEY_COST, cost);
                       in.putExtra(KEY_DESC, description);
                       startActivity(in);
                    }

              }
    }

SingleMenuItemActivity类中,使用以下方法获取值,

    String name = this.getIntent().getStringExtra("KEY_NAME");
String cost= this.getIntent().getStringExtra("KEY_NAME");
String description= this.getIntent().getStringExtra("KEY_DESC");

编辑-

main.xml 文件-->>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical">


    <EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

</LinearLayout>

Activity课堂上扩展您的活动

于 2012-11-08T08:23:32.620 回答
0

您的 layout.xml 格式不正确:

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >

您需要像这样正确结束<EditText>标签/>

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
于 2012-11-08T08:25:52.833 回答