我在我的应用中使用片段。每个片段都有一个列表视图。 ListView是包含单个图像视图的自定义列表视图。
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@android:drawable/ic_notification_overlay" />
列表视图项目是图像。我想实现searchview。我想按每个listview项目的名称搜索,例如“jingle bells etc”以及数字,即“1,2 ...等等。
我的包含listview标题的java文件是。
public class carolFrag extends Fragment {
//code
public carolFrag() {
// Required empty public constructor
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
private static final String TAG = "carolFrag";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup main_content, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_carol, main_content, false);
final int[] menuImage = {R.drawable.carol_1_title, R.drawable.carol_2_title,R.drawable.carol_3_title,R.drawable.carol_4_title,R.drawable.carol_5_title,R.drawable.carol_6_title,R.drawable.carol_7_title,R.drawable.carol_8_title,R.drawable.carol_9_title,R.drawable.carol_10_title,R.drawable.carol_11_title};
final ListView listView = (ListView) view.findViewById(R.id.CarolList);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent1 = new Intent (getActivity(), PackageC.class)
intent1.putExtra("position", position);
startActivity(intent1);
}
});
AdapterCarols adapter = new AdapterCarols(getContext(), menuImage);
listView.setAdapter(adapter);
// Inflate the layout for this fragment
return view;
}
}
我无法制作任何逻辑,因为数组中有drawable。
答案 0 :(得分:0)
因为阵列中充满了可绘制的数据,并且它不是数据库的东西,也不是每个解决方案,所以我将会得到一个解决方案。我相信,简单的是让另一个数组可能是一个 - String [] - 其中drawable的名称与它们在原始中的顺序相同 - final int [] menuImage - 你可以在String数组中进行搜索并保存结果的位置。然后使用这些位置创建一个新数组(int []),在这个新数组中放入与这些位置匹配的drawable。最后更改列表视图的适配器,传递带有结果的数组和notifydatachange();在列表视图中,它可以直观地更新。
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
main_content, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_carol, main_content, false);
final int[] menuImage = {R.drawable.carol_1_title, R.drawable.carol_2_title,R.drawable.carol_3_title,R.drawable.carol_4_title,R.drawable.carol_5_title,R.drawable.carol_6_title,R.drawable.carol_7_title,R.drawable.carol_8_title,R.drawable.carol_9_title,R.drawable.carol_10_title,R.drawable.carol_11_title};
final ListView listView = (ListView) view.findViewById(R.id.CarolList);
SearchView sv = view.findviewById(R.id.searchview);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
public boolean onQueryTextChange(String par1)
{
return false;
}
public boolean onQueryTextSubmit(String par1)
{
int [] searchresults = PerformSearch(par1);
AdapterCarols newadapter = new
AdapterCarols(getContext(), searchresults);
listView.setAdapter(adapter);
listview.invalidate(); // don't remember listview refresh method. I work with Recyclerview, they have a notifyDatasetChange(); method.
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent1 = new Intent (getActivity(), PackageC.class)
intent1.putExtra("position", position);
startActivity(intent1);
}
});
AdapterCarols adapter = new AdapterCarols(getContext(), menuImage);
listView.setAdapter(adapter);
// Inflate the layout for this fragment
return view;
}
private int[] PerformSearch(par1)
{
String [] strcarols = {carol_1_title,carol_2_title,carol_3_title,carol_4_title,carol_5_title,carol_6_title,carol_7_title,carol_8_title,carol_9_title,carol_10_title,carol_11_title};
int[] menuImage = {R.drawable.carol_1_title, R.drawable.carol_2_title,R.drawable.carol_3_title,R.drawable.carol_4_title,R.drawable.carol_5_title,R.drawable.carol_6_title,R.drawable.carol_7_title,R.drawable.carol_8_title,R.drawable.carol_9_title,R.drawable.carol_10_title,R.drawable.carol_11_title};
int[] results = new int[];
int carolsresultcount = 0, index =0;
/// simplest way
for(String carol:strcarols)
{
if(carol.contains(par1))
{
results [carolsresultcount++] = menuImage [index];
}
index++;
}
return results;
}
我希望这可以解决你的问题。