0

I´m new to programming and tried to make an app where I have a Listview with clickable codes. When I click on an element and it opens another Listview (A TextView would be better, but I´m also not capable of doing it. But the priority is the search). The problem is, that the list is very long and I wanted to add a Search. I´ve tried for over a week and couldn´t find a solution. Is anyone of you able to help me, adding a Search? The link to the project is on the end of the post!

Here´s a pic of the Listview: Here´s the code of the MainActivity:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


ListView listView;
Button but_zu_codes;


ArrayAdapter<String> mAdapter;

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

    but_zu_codes = (Button) findViewById(R.id.codes);



   but_zu_codes.setOnClickListener(this);



    listView = (ListView) findViewById(R.id.listView);

    mAdapter = new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_list_item_1,
            getResources().getStringArray(R.array.tr_codes));

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(MainActivity.this, ListActivity.class);
            intent.putExtra("TroubleCodes", listView.getItemAtPosition(i).toString());
            startActivity(intent);

        }
    });
    listView.setAdapter(mAdapter);



}




@Override
public void onClick(View v) {

    if (v.getId() == R.id.codes) {
        Intent weiter = new Intent(MainActivity.this, MainActivity.class);
        startActivity(weiter);
    }

}
}


}

The code of the ListActivity:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class ListActivity extends AppCompatActivity {

ListView listView;
String[] states;


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



    listView = (ListView) findViewById(R.id.listView);
    Bundle mBundle = getIntent().getExtras();
    if (mBundle != null) {
        String country = mBundle.getString("TroubleCodes");
        if (country.equalsIgnoreCase("B0562")) {
            states = getResources().getStringArray(R.array.c_B0562);
        }

        else if (country.equalsIgnoreCase("B0563")) { states = getResources().getStringArray(R.array.c_B0563); }
        else if (country.equalsIgnoreCase("B1004")) { states = getResources().getStringArray(R.array.c_B1004); }
        else if (country.equalsIgnoreCase("B1005")) { states = getResources().getStringArray(R.array.c_B1005); }
        else if (country.equalsIgnoreCase("B1006")) { states = getResources().getStringArray(R.array.c_B1006); }

    }

    ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(ListActivity.this,
            android.R.layout.simple_list_item_1, states);
    listView.setAdapter(mAdapter);
}
}

Here´s a link to my android studio project: https://www.dropbox.com/s/9b5r7t6z91t5y0a/3_Codes.rar?dl=0

L. Berger
  • 1
  • 1

1 Answers1

1

You have multiple examples in the web. Keeping it short, you want

  1. an EditText with a OnTextChangedListener
  2. in the OnTextChangedListener, filter the whole list of items
  3. replace the elements of the ListView with the filtered list
  4. notify the ListView adapter so it renders the list again

Check out links like this for code examples: How to use search functionality in custom list view in Android

Community
  • 1
  • 1
Mikel Pascual
  • 2,202
  • 18
  • 27