我的应用中的SearchView显示错误的ListView项目时遇到问题。
如果我输入名称: ListView按预期显示
如果我输入Name1:,我会收到条目1-2
如果我输入名称2-11:,我会收到Name1的条目
据我所知,代码很好,但应用程序没有按预期工作。有人可以指出我正确的方向来解决这个问题吗?
ListView与以下数据中的自定义数组适配器一起显示:
[{"society_id":1,"name":"Name1","email":"Test@email1","description":"TestDes1"},
{"society_id":2,"name":"Name2","email":"Test@email2","description":"TestDes2"},
{"society_id":3,"name":"Name3","email":"Test@email3","description":"TestDes3"},...]
SocietySearch.java:
public class SocietySearch extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) { //TODO remember notifyDataSetChanged
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_society_search);
List<Society> societies = new ArrayList<>();
ServerRequests serverRequest1 = new ServerRequests(SocietySearch.this);
serverRequest1.GetSocietyDataAsyncTask(societies, new GetSocietyCallback() {
@Override
public void done(final List<Society> societies) {
ListView lv = (ListView) findViewById(R.id.ListView);
List<ListViewItem> items = new ArrayList<>();
for (final Society s : societies) {
items.add(new ListViewItem() {{
ThumbnailResource = R.drawable.test;
Title = s.socName;
Description = s.socDes;
}});
}
final CustomListViewAdapter adapter = new CustomListViewAdapter(SocietySearch.this, items, societies);
lv.setAdapter(adapter);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) findViewById(R.id.search);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint("Search..");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
}
});
}
public class ListViewItem {
public int ThumbnailResource;
public String Title;
public String Description;
}
CustomListViewAdapter.java:
public class CustomListViewAdapter extends ArrayAdapter {
LayoutInflater inflater;
List<SocietySearch.ListViewItem> items;
public CustomListViewAdapter(Activity context, List<SocietySearch.ListViewItem> items, List<Society> societies) {
super(context, R.layout.item_row, societies);
this.items = items;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//Auto-generated method stub
ListViewItem item = items.get(position);
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.item_row, null);
ImageView test = (ImageView) vi.findViewById(R.id.imgThumbnail);
TextView txtTitle = (TextView) vi.findViewById(R.id.txtTitle);
TextView txtSubTitle = (TextView) vi.findViewById(R.id.txtSubTitle);
test.setImageResource(item.ThumbnailResource);
txtTitle.setText(item.Title);
txtSubTitle.setText(item.Description);
return vi;
}
}
Society.java:
public class Society {
String socName, socEmail, socDes;
int society_id;
public Society(int society_id, String socName, String socEmail, String socDes) {
this.society_id = society_id;
this.socName = socName;
this.socEmail = socEmail;
this.socDes = socDes;
}
public String toString() {
return socName;
}
}
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="k.unionapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeScreenActivity"
android:theme="@style/AppTheme.NoActionBar"></activity>
<activity android:name=".SocietySearch"></activity>
</application>
</manifest>
答案 0 :(得分:2)
我相信你的问题可能是因为你抓住了内存中的原始列表,并将你的视图绑定了
ListViewItem item = items.get(position);
相反,您可能想尝试从适配器本身获取该项,因为Filter
ListViewItem item = this.getItem(position);
答案 1 :(得分:2)
您需要在Private Sub cmdApplyFilter_Click()
Me!subReport.Report.Filter = "id=1"
Me!subReport.Report.FilterOn = True
类中提供toString
方法的实现,该方法将返回项目名称。
SocietySearch.ListViewItem
public class ListViewItem {
public int ThumbnailResource;
public String Title;
public String Description;
@Override
public String toString(){
return Title;
}
}
中过滤器的默认实现使用此方法将任意项类型转换为ArrayAdapter
,然后再将其与搜索查询进行比较。此外,默认实现 - String
- 使用查询字符串作为前缀,FYI。
另外,正如其他答案中所述 - 不要在构造函数和ArrayFilter
方法中保留原始列表。使用数组适配器本身的getView
方法。
您还可能希望更改类声明以包含自定义项目类型而不是通用getItem
:
Object