我正在开发一个应用程序,其中从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");
}
}
}
答案 0 :(得分:0)
搜索将在原始数据源上实现,它可以是XML / JSON /数据库或其他任何内容。
您正在使用“position”参数进行搜索,我认为您可以使用View视图参数。 只需在ListView行中保留一个隐藏字段,并使用某种主键映射它。
当OnClick事件发生时,您可以从视图中获取行布局并执行findViewByID()调用以获取隐藏控件。从该控件的值,您可以调用后端Web服务或运行x路径查询或任何您想要的。