Android 搜索Customlist视图不起作用
我正在尝试在自定义listView上构建搜索功能,但它不起作用。列表不会根据EditText Word的更改而更改 listplaceholder.xmlAndroid 搜索Customlist视图不起作用,android,Android,我正在尝试在自定义listView上构建搜索功能,但它不起作用。列表不会根据EditText Word的更改而更改 listplaceholder.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_pa
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edtSearch"
android:hint="Search"/>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
</LinearLayout>
my_custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/icon" />
<FrameLayout
android:id="@+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button"
/>
<LinearLayout
android:id="@+id/ll1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/a"
android:orientation="vertical" >
<TextView
android:id="@+id/from_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
<TextView
android:id="@+id/from_user_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
Main.java
public class Main extends ListActivity {
/** Called when the activity is first created. */
ImageView img, img1;
private AdapterClass adapter;
private String reviewImageLink;
private static final String TAG = "PRANJAL";
private boolean isImage = false;
String fileName[] = new String[10];
private String imgurl;
ArrayList<HashMap<String, String>> searchResult;
private EditText edtSearch;
private ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
edtSearch = (EditText) findViewById(R.id.edtSearch);
list = (ListView) findViewById(android.R.id.list);
// list = (LinearLayout) findViewById(R.id.list);
final ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions
.getJSONfromURL("http://23.21.228.8/PlutoApp_Beta/consumer_dash/"
+ "checkIn_confirmation_related_record.php?lat=18.535787&lng=73.891889");
try {
JSONArray earthquakes = json.getJSONArray("Display");
for (int i = 0; i < earthquakes.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("Name", e.getString("name"));
map.put("Vicinity", e.getString("vicinity"));
map.put("logo", e.getString("logo"));
imgurl = e.getString("logo");
// Toast.makeText(getApplicationContext(), imgurl,
// Toast.LENGTH_SHORT).show();
imgurl = e.getString("logo");
// Toast.makeText(getApplicationContext(), imgurl,
// Toast.LENGTH_SHORT).show();
DownloadFromUrl(imgurl, i + ".jpg");
reviewImageLink = imgurl;// getString(R.string.ImageURI);
URL reviewImageURL;
fileName[i] = reviewImageLink.substring(reviewImageLink
.lastIndexOf("/") + 1);
// map.put("profile_image_url", fileName[i]);
map.put("logo", i + ".jpg");
list.setTextFilterEnabled(true);
mylist.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
/*final ListView lv = getListView();
lv.setTextFilterEnabled(true);*/
searchResult = new ArrayList<HashMap<String, String>>(mylist);
// adapter = new AdapterClass(Main.this, R.layout.main, mylist);
adapter = new AdapterClass(Main.this, R.layout.my_custom_layout, mylist);
// list.setAdapter(adapter);
setListAdapter(adapter);
edtSearch.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// get the text in the EditText
String searchString = edtSearch.getText().toString();
int textLength = searchString.length();
searchResult.clear();
for (int i = 0; i < mylist.size(); i++) {
String playerName = mylist.get(i).get("Name").toString();
if (textLength <= playerName.length()) {
// compare the String in EditText with Names in the
// ArrayList
if (searchString.equalsIgnoreCase(playerName.substring(
0, textLength)))
Toast.makeText(getApplicationContext(),
"Inside if of : " + playerName, 1000)
.show();
searchResult.add(mylist.get(i));
}
}
adapter.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
/*
* final ListView lv = getListView(); //lv.setTextFilterEnabled(true);
* lv.setOnItemClickListener(new OnItemClickListener() { public void
* onItemClick(AdapterView<?> parent, View view, int position, long id)
* {
*
* @SuppressWarnings("unchecked") HashMap<String, String> o =
* (HashMap<String, String>) lv .getItemAtPosition(position);
*
* } });
*/
}
public void DownloadFromUrl(String DownloadUrl, String fileName) {
try {
File dir = new File("/sdcard/pluto");
if (dir.exists() == false) {
dir.mkdirs();
}
URL url = new URL(DownloadUrl); // you can write here any link
File file = new File(dir, fileName);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager", "download begining");
Log.d("DownloadManager", "download url:" + url);
Log.d("DownloadManager", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
Log.d("DownloadManager",
"download ready in"
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
} catch (IOException e) {
Log.d("DownloadManager", "Error: " + e);
}
}
}
public类主扩展ListActivity{
/**在首次创建活动时调用*/
图像视图img,img1;
专用适配器类适配器;
私有字符串reviewImageLink;
私有静态最终字符串TAG=“PRANJAL”;
私有布尔值isImage=false;
字符串文件名[]=新字符串[10];
私有字符串imgurl;
ArrayList搜索结果;
私人编辑文本搜索;
私有列表视图列表;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
edtSearch=(EditText)findViewById(R.id.edtSearch);
list=(ListView)findViewById(android.R.id.list);
//list=(LinearLayout)findViewById(R.id.list);
最终ArrayList mylist=新ArrayList();
JSONObject json=JSONfunctions
.getJSONfromURL(“http://23.21.228.8/PlutoApp_Beta/consumer_dash/"
+“checkIn_confirmation_related_record.php?lat=18.535787&lng=73.891889”);
试一试{
JSONArray=json.getJSONArray(“显示”);
对于(int i=0;i<1.length();i++){
HashMap=newHashMap();
JSONObject e=地震。getJSONObject(i);
map.put(“Name”,e.getString(“Name”);
地图放置(“附近”,例如getString(“附近”);
map.put(“logo”,e.getString(“logo”);
imgurl=e.getString(“logo”);
//Toast.makeText(getApplicationContext(),imgurl,
//吐司。长度(短)。show();
imgurl=e.getString(“logo”);
//Toast.makeText(getApplicationContext(),imgurl,
//吐司。长度(短)。show();
下载网址(imgurl,i+“.jpg”);
reviewImageLink=imgurl;//getString(R.string.ImageURI);
URL reviewImageURL;
fileName[i]=reviewImageLink.substring(reviewImageLink
.lastIndexOf(“/”+1);
//put(“profile_image_url”,文件名[i]);
地图放置(“logo”,i+“.jpg”);
list.setTextFilterEnabled(true);
添加(地图);
}
}捕获(JSONException e){
Log.e(“Log_标记”,“错误解析数据”+e.toString());
}
/*最终ListView lv=getListView();
lv.setTextFilterEnabled(真)*/
searchResult=newarraylist(mylist);
//适配器=新适配器类(Main.this,R.layout.Main,mylist);
适配器=新适配器类(Main.this,R.layout.my\u custom\u layout,mylist);
//list.setAdapter(适配器);
setListAdapter(适配器);
edtSearch.addTextChangedListener(新的TextWatcher(){
public void onTextChanged(字符序列,int start,int before,
整数计数){
//获取编辑文本中的文本
String searchString=edtSearch.getText().toString();
int textLength=searchString.length();
searchResult.clear();
对于(int i=0;i 如果(textLength您没有在onTextChange中再次设置适配器。请在末尾添加此内部onTextChange:
setListAdapter(searchResult);
移除
adapter.notifyDataSetChanged();
你会尝试使用而不是发明这种奇怪的功能吗?