我已经完成了代码,但似乎搜索无效
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
ListView lv;
SearchView searchView;
ArrayAdapter<String> adapter;
String[] apptitle = {"†orch", "Quiz It!"};
String[] inf = {"click for info", "click for info"};
int[] img = {R.drawable.ic_launcher, R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
lv = (ListView) findViewById(R.id.idlistview);
searchView = (SearchView) findViewById(R.id.idsearch);
Adapterim adapter = new Adapterim(getApplicationContext(), apptitle, inf, img);
lv.setAdapter(adapter);
/*adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, appTitle);
lv.setAdapter(adapter);*/
searchView.setOnQueryTextListener(this);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
}
// ArrayAdapter extends, Let's create class
class Adapterim extends ArrayAdapter<String> {
//For the information we need for our content
//Create array variable
int[] img = {};
String[] apptitle = {};
String[] inf = {};
//Context and layoutinflater as required
//Let's create them
Context c;
LayoutInflater inflater;
//The parameters of the generated method
public Adapterim(Context context, String[] apptitle, String[] inf, int[] img) {
//Values to be entered into the super method
//
super(context, R.layout.listview_class, apptitle);
// Equalize the parameters to the variables in this class
this.img = img;
this.apptitle = apptitle;
this.inf = inf;
this.c = context;
}
// Let's create our inner class and
// create our components
public class Viewholder {
TextView attv;
TextView inftv;
ImageView imgim;
}
//With this automatic method, each element in the array can be edited one by one
//We add data to the components in ListView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_class, null, true);
}
// Create an object from our class we created
final Viewholder holder = new Viewholder();
// And the components we create in our inner structure
//I sync with the components in the layout
holder.attv = (TextView) convertView.findViewById(R.id.custom_textView);
holder.inftv = (TextView) convertView.findViewById(R.id.custom_textView2);
holder.imgim = (ImageView) convertView.findViewById(R.id.custom_imageView);
//In order to assign the array elements as data for each component
//Assign position value to our arrays
//And set the values of our components
holder.imgim.setImageResource(img[position]);
holder.attv.setText(apptitle[position]);
holder.inftv.setText(inf[position]);
//It must be a return value and I will return View
return convertView;
}
}