0

I have done the code but it appears the Search is not working

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;
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jeffrey
  • 11
  • 1
  • how do you expect the search to be working? because in `onQueryTextSubmit(String query)` you are just returning false instead of reacting to it? – Katharina Jun 14 '17 at 13:31
  • Try to implement Filterable. Take this link as reference: https://stackoverflow.com/questions/24769257/custom-listview-adapter-with-filter-android – Yyy Jun 14 '17 at 13:33
  • 1
    Define "not working". Does it return an error? Unexpected results? Computer explodes? – tom redfern Jun 14 '17 at 13:33

0 Answers0