0

我正在开发一个从 XML 读取数据的应用程序,然后根据数据,它创建一个 ListView 并单击 ListView 中的任何项目,它会打开另一个包含详细信息的活动。我正在尝试在 ListView 中实现搜索功能,下面是我的代码,我不知道如何实现搜索,我尝试了很多方法,但都没有奏效。非常感谢任何帮助。提前致谢。

public class BrowseActors extends Activity {
    private static final String TAG = "QuizListActivity";
    private ImageView bannerImageView; // displays a Image
    // Search EditText
    private EditText inputSearch;
    // XML node keys
    static final String KEY_TAG = "ActorData"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_NAME = "name";
    static final String KEY_ICON = "icon";

    String position;
    String sourceFile;
    // List items
    ListView list;
    //BinderActorData adapter = null;
    List<HashMap<String, String>> actorDataCollection;
    ArrayAdapter<HashMap<String, String>> adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); // Remove title
        setContentView(R.layout.browse_actor);
        bannerImageView = (ImageView) findViewById(R.id.topImageView);
        try {
            // Get Category to filter
            Intent in = getIntent();
            this.position = in.getStringExtra("position");
            sourceFile="BaseData.xml"
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(getAssets().open(sourceFile));

            actorDataCollection = new ArrayList<HashMap<String, String>>();

            // normalize text representation
            doc.getDocumentElement().normalize();

            NodeList quizList = doc.getElementsByTagName("ActorData");

            HashMap<String, String> map = null;

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

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

                Node firstQuestionNode = quizList.item(i);

                if (firstQuestionNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element firstAircraftElement = (Element) firstQuestionNode;
                        // 1.-------
                        NodeList idList = firstAircraftElement
                                .getElementsByTagName(KEY_ID);
                        Element firstIdElement = (Element) idList.item(0);
                        NodeList textIdList = firstIdElement.getChildNodes();
                        // --id
                        map.put(KEY_ID, textIdList.item(0).getNodeValue()
                                .trim());

                        // 2.-------
                        NodeList nameList = firstAircraftElement
                                .getElementsByTagName(KEY_NAME);
                        Element firstNameElement = (Element) nameList.item(0);
                        NodeList textNameList = firstNameElement
                                .getChildNodes();
                        // --name
                        map.put(KEY_NAME, textNameList.item(0).getNodeValue()
                                .trim());

                        // 3.-------
                        NodeList iconList = firstAircraftElement
                                .getElementsByTagName(KEY_ICON);
                        Element firstIconElement = (Element) iconList.item(0);
                        NodeList textIconList = firstIconElement
                                .getChildNodes();
                        // -- Image Icon
                        map.put(KEY_ICON, textIconList.item(0).getNodeValue()
                                .trim());
                        // Add to the Arraylist
                        actorDataCollection.add(map);

                }
            }

            BinderActorData bindingData = new BinderActorData(this,
                    actorDataCollection);
            // Adding items to listview
            list = (ListView) findViewById(R.id.list);
            // Adding items to listview
            inputSearch = (EditText) findViewById(R.id.inputSearch);

            list.setAdapter(bindingData);

            /**
             * Enabling Search Filter
             * */
            inputSearch.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2,
                        int arg3) {
                    // When user changed the Text
                    //BrowseActors.this.bindingData.getFilter().filter(cs);
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                        int arg2, int arg3) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                }
            });

            // Click event for single list row
            list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    Intent i = new Intent();
                    i.setClass(BrowseActors.this, ViewActor.class);

                    // parameters
                    i.putExtra("position", String.valueOf(position + 1));

                    /*
                     * selected item parameters 
                     */

                    i.putExtra("name",
                            actorDataCollection.get(position).get(KEY_NAME));
                    i.putExtra("icon",
                            actorDataCollection.get(position).get(KEY_ICON));

                    // start the sample activity
                    startActivity(i);
                }
            });
        }

        catch (IOException ex) {
            Log.e("Error", ex.getMessage());
        } catch (Exception ex) {
            Log.e("Error", "Loading exception");
        }
    }
}
4

1 回答 1

0

搜索将在原始数据源上实现,它可以是 XML/JSON/数据库或其他任何东西。

您正在使用“位置”参数进行搜索,我认为您可以使用 View view 参数。只需在 ListView 行中保留一个隐藏字段,并将其与某种主键映射。

当 OnClick 事件发生时,您可以从视图中获取行布局并执行 findViewByID() 调用以获取隐藏控件。根据该控件的值,您可以调用后端 Web 服务或运行 x-path 查询或任何您想要的。

于 2013-09-28T19:41:17.507 回答