gpt4 book ai didi

android - 在操作栏中搜索无法与 ListView 的自定义适配器一起正常工作

转载 作者:行者123 更新时间:2023-11-30 02:20:11 26 4
gpt4 key购买 nike

好吧,我正在尝试创建一个具有 listView 并且可以通过操作栏搜索选项进行搜索的 Activity 。

如果,我使用 android 的默认数组列表,它就可以正常工作。

但是当我尝试使用我自己的扩展 baseAdapter 的适配器时,它无法正常工作(无法正确搜索)。

我的全部代码在下面的链接中。

Link for my App

有人可以帮我解决这个问题吗?

代码是:

list ::

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mysearchapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />

</intent-filter>

<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />

</activity>
</application>
</manifest>

菜单(res/menu/second.xml)::

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/ic_launcher"
android:showAsAction="always"
android:title="Search"/>

</menu>

布局(资源/布局)::

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000" />

</RelativeLayout>

searchable.xml(在 res/xml 中)::

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="search..."
android:label="@string/app_name" />

自定义适配器::

        public class ImageAdapter extends BaseAdapter implements Filterable
{
private Context context;
private ArrayList<String> originalData = null;
ArrayList<String> filteredData = null;

ValueFilter valueFilter = new ValueFilter();

public ImageAdapter(Context context, ArrayList<String> mobileValues)
{
this.context = context;
this.originalData = mobileValues;
this.filteredData = mobileValues;
}

@Override
public int getCount() {
return filteredData.size();
}

@Override
public Object getItem(int position) {
return filteredData.get(position);
}

@Override
public long getItemId(int position) {
return filteredData.indexOf(getItem(position));
}

public View getView(int position, View convertView, ViewGroup parent)
{

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View gridView;

if (convertView == null) {

gridView = new View(context);

// get layout from mobile.xml
gridView = inflater.inflate(R.layout.city_list_item, null);

// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(originalData.get(position));

// set image based on selected text
// ImageView imageView = (ImageView)
// gridView.findViewById(R.id.grid_item_image);

// imageView.setImageResource(R.drawable.city_image);

} else {
gridView = (View) convertView;
}

return gridView;
}

@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}

@Override
public Filter getFilter() {
return valueFilter;
}

private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toUpperCase();

FilterResults results = new FilterResults();

/*
* if (constraint != null && constraint.length() > 0) {
*/
final ArrayList<String> lst = ImageAdapter.this.originalData;
int count = lst.size();

final ArrayList<String> filterList = new ArrayList<String>(count);

String filterableString;

for (int i = 0; i < count; i++) {
filterableString = lst.get(i);

if ((filterableString.toUpperCase()).contains(filterString)) {

filterList.add(filterableString);
}
}
results.count = filterList.size();
results.values = filterList;
/* } *//*
* else { results.count = mStringFilterList.size();
* results.values = mStringFilterList; }
*/
return results;

}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}

}
}

和 Activity::

    public class SecondActivity extends Activity {

// ArrayAdapter<String> myAdapter;
ImageAdapter myAdapter;
ListView listView;
// String[] dataArray = new String[] {"India","Androidhub4you", "Pakistan",
// "Srilanka", "Nepal", "Japan"};
private ArrayList<String> dataArray = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

dataArray.add("India");
dataArray.add("Androidhub4you");
dataArray.add("Pakistan");
dataArray.add("Srilanka");
dataArray.add("Nepal");
dataArray.add("Japan");

listView = (ListView) findViewById(R.id.listview);
// myAdapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_1, dataArray);
myAdapter = new ImageAdapter(SecondActivity.this, dataArray);
listView.setAdapter(myAdapter);
listView.setTextFilterEnabled(true);

listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

System.out.println(arg2 + " --postion");
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.second, menu);

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();

searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);

SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
myAdapter.getFilter().filter(newText);
System.out.println("on text chnge text: " + newText);
return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
// this is your adapter that will be filtered
myAdapter.getFilter().filter(query);
System.out.println("on query submit: " + query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);

return super.onCreateOptionsMenu(menu);

}
}

最佳答案

在你的适配器中移动这个

TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
textView.setText(originalData.get(position));

就在返回语句之前。

getView 方法的工作原理:

如果一个 View 已经可用,则采用该 View ,如果不可用,则创建一个新 View 。现在适配器有一个 View ,用数据加载它。

完整代码

public View getView(int position, View convertView, ViewGroup parent) {
View gridView;

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// gridView = new View(context); // this line is useless, you're overwriting the variable on the next line

// inflate new layout
// gridView = inflater.inflate(R.layout.city_list_item, null); // wrong
gridView = inflater.inflate(R.layout.city_list_item, parent, false); // right
} else {
// reuse previously created view
gridView = (View) convertView;
}

// set value into textview
TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
textView.setText(originalData.get(position));

// set image based on selected text
//ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
//imageView.setImageResource(R.drawable.city_image);

// finding views all over again is not effective, google "viewholder"

return gridView;
}

编辑:

要加载的正确项目不是来自原始数据集,而是来自过滤后的数据集。正确实现 String getItem(int) 方法后,始终在您的 View getView(int, View, parent) 中调用此方法。

textView.setText(getItem(position));

关于android - 在操作栏中搜索无法与 ListView 的自定义适配器一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28672325/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com