2014-10-17 97 views
1

我需要在我的項目中包含可搜索的配置文件,在android文檔中他們說過要在res/xml目錄中包含此xml文件。但沒有找到xml目錄。我是否需要創建xml目錄或在其他位置添加此xml文件?Android studio res/xml目錄未找到

回答

0
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.TextView; 

public class Main extends Activity { 
    private static String APP_TAG = "tag"; 
    private TextView textView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     textView = (TextView) findViewById(R.id.textField); 
     findViewById(R.id.readBtn).setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       XmlPullParser parser = getResources().getXml(R.xml.cars); 
       StringBuilder stringBuilder = new StringBuilder(); 
       try { 
        while (parser.next() != XmlPullParser.END_DOCUMENT) { 
         String name = parser.getName(); 
         String position = null; 
         String brand = null; 
         if((name != null) && name.equals("car")) { 
          int size = parser.getAttributeCount(); 
          for(int i = 0; i < size; i++) { 
           String attrName = parser.getAttributeName(i); 
           String attrValue = parser.getAttributeValue(i); 
           if((attrName != null) && attrName.equals("position")) { 
            position = attrValue; 
           } else if ((attrName != null) && attrName.equals("brand")) { 
            brand = attrValue; 
           } 
          } 
          if((position != null) && (brand != null)) { 
           stringBuilder.append(position + ", " + brand + "\n"); 
          } 
         } 
         textView.setText(stringBuilder.toString()); 
        } 
       }catch(Exception e) { 
        Log.e(APP_TAG, e.getMessage()); 
       } 
      } 
     }); 
    } 
} 


---------- 
You need to create xml directory under res folder like (RES-XML) put you Car.xml in XML folder 
i have added sample code for retrieving data from xml/car.xml 
+0

但我們仍然不知道,如果我們要創建XML文件夾(以及如何) – 2015-05-10 18:38:36

+1

你需要像(RES-XML)res文件夾下創建manaually xml目錄把你Car.xml在XML文件夾 我已經添加了用於從xml/car.xml中檢索數據的示例代碼 – koutuk 2015-05-12 11:31:11

-1

Documentation says

你首先需要的是一個XML文件稱爲搜索 配置。它配置搜索對話框 或小部件的某些UI方面,並定義搜索行爲如建議和語音等功能。這個文件通常被命名爲searchable.xml,並且 必須保存在res/xml/project目錄中。