Android Listview Search Filter

I

IIIGeorgeIII

Neues Mitglied
1
Gute Tag an alle, ;-) ich hoffe ihr könnt mir da weiter Helfen

Folgendes Problem:
In meiner App möchte Ich auf bestimmten Artikel klicken die nach Filterung abgeschlossen wurde, das Problem ist, dass nach dem Filtern die Position der einzelnen Klassen nicht mehr zu einender passen.

Code:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitypz_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar8);
        setSupportActionBar(toolbar);

        List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

        for (int i = 0; i < fahrzeuge.length; i++) {
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("listview_title", fahrzeuge[i]);
            hm.put("listview_discription", einheit[i]);
            hm.put("listview_image", Integer.toString(imgid[i]));
            aList.add(hm);
        }

        String[] from = {"listview_image", "listview_title", "listview_discription"};
        int[] to = {R.id.iconpkw, R.id.item, R.id.textViewpkw1011};

        adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.mylistpkw, from, to);
        list = (ListView) findViewById(R.id.list4);
        list.setAdapter(adapter);
        list.setTextFilterEnabled(true);





        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int pos, long id) {

                String openClass =  fahrzeuge[pos];

                if (openClass.equals("Fuchs")) {
                    Intent myIntent = new Intent(view.getContext(), FuchsActivity.class);
                    startActivityForResult(myIntent, 0);
                }else if (openClass.equals("Boxer")) {
                    Intent myIntent = new Intent(MainActivityPz.this, BoxerActivity.class);
                    startActivityForResult(myIntent, 0);
                }else if (openClass.equals("Dingo I")) {
                    Intent myIntent = new Intent(view.getContext(), DingoeinsActivity.class);
                    startActivityForResult(myIntent, 0);
                }else if (openClass.equals("Biber")) {
                    Intent myIntent = new Intent(MainActivityPz.this, BiberActivity.class);
                    startActivityForResult(myIntent, 0);
                }else if (openClass.equals("Dingo 2 GE A2")) {
                    Intent myIntent = new Intent(MainActivityPz.this, DingoZweiAActivity.class);
                    startActivityForResult(myIntent, 0);
                }else if (openClass.equals("Dingo 2 GE C1 GSI")) {
                    Intent myIntent = new Intent(view.getContext(), DingoZweiCActivity.class);
                    startActivityForResult(myIntent, 0);
                }else if (openClass.equals("Fennek STF Lu/Bo")) {
                    Intent myIntent = new Intent(MainActivityPz.this, FennekActivity.class);
                    startActivityForResult(myIntent, 0);
                }else if (openClass.equals("Leopard 2")) {
                    Intent myIntent = new Intent(MainActivityPz.this, LeopardActivity.class);
                    startActivityForResult(myIntent, 0);
                }else if (openClass.equals("Spz Marder")) {
                    Intent myIntent = new Intent(MainActivityPz.this, MarderActivity.class);
                    startActivityForResult(myIntent, 0);
                }else if (openClass.equals("Mars II")) {
                    Intent myIntent = new Intent(MainActivityPz.this, MarszweiActivity.class);
                    startActivityForResult(myIntent, 0);
                }



            }
        });
 }
 
Hallo George,

nimm am Besten einen eigenen CustomAdapter und diesem übergibst du eine KlassenStruktur in einem Array(List)
Sortieren kannst du dann ggf. mit "Collections"

lg
Stefan
 
Zuletzt bearbeitet:
Hi , Danke für den Tipp, kannst du mir vielleicht ein gutes tutorial empfehlen ? :)
 
Schau mal hier,
das bezieht sich zwar jetzt auf einen GridView verdeutlicht dir aber , wie man einen Custom-Adapter erstellt und benutzt.

Custom GridView with ImageView and TextView In Android

und anstatt zwei Array zu deklarieren
String[] from = {"listview_image", "listview_title", "listview_discription"};
int[] to = {R.id.iconpkw, R.id.item, R.id.textViewpkw1011};

Machst du z.b sowas

public class _MyStructure
{
String from;
int to;
}


deklarieren :
private ArrayList <_MyStructure> MS;
MS = new ArrayList();

Adden :
_MyStructure ms_tmp = new _MyStructure();
ms_temp.from = "fooo";
ms_tmp.to =foo_value;
MS.add(ms_tmp);


P.S alles nur schnell aus der Hüfte runtergetippt , nicht getestet


lg
Stefan
 
Zuletzt bearbeitet:
Danke schon mal , ich probiere das mal aus ;-)
 
Viel Erfolg ...
 
Hallo ich melde mich zurück :-D, habe das jetzt so Programmiert wie vorgeschlagen wurde, die Liste Funktioniert nur das suchen will einfach nicht ;-(, App stürzt ab nach Buchstabe Eingabe.

MainActivityPz
public class MainActivityPz extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private static final String TAG = MainActivityPz.class.getSimpleName();


ListView lv;
Context context;
PzCustomAdapter adapter;
ArrayList prgmName;

public static int[] prgmImages = {
R.drawable.fuchs,
R.drawable.boxer,
R.drawable.dingoeins,
R.drawable.biber,
R.drawable.dingozweigeazwei,
R.drawable.dingozweigsi,
R.drawable.fennek,
R.drawable.leopard,
R.drawable.marder,
R.drawable.marszwei};

public static String[] prgmNameList = {
"Fuchs",
"Boxer",
"Dingo I",
"Biber",
"Dingo 2 GE A2",
"Dingo 2 GE C1 GSI",
"Fennek STF Lu/Bo",
"Leopard 2",
"Spz Marder",
"Mars II"};

public static String[] prgmNameEinheit = {
"Kpf",
"Kpf",
"Kpf",
"Pionier",
"Kpf",
"Inst",
"Aufklärer",
"Kpf",
"Kpf",
"Artillerie"
};
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;


@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitypz_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar8);
setSupportActionBar(toolbar);

context = this;

lv = (ListView) findViewById(R.id.list4);
adapter = new PzCustomAdapter(this, prgmNameList, prgmImages, prgmNameEinheit);
lv.setAdapter(adapter);

lv.setTextFilterEnabled(true);


Log.v(TAG, "ausführliche Protokollierung, nicht in Produktion verwenden");
Log.d(TAG, "Debug-Ausgaben");
Log.i(TAG, "Informationen");
Log.w(TAG, "Warnung");
Log.e(TAG, "Fehler");


DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout8);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view8);
navigationView.setNavigationItemSelectedListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}


@override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout8);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}


@override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

getMenuInflater().inflate(R.menu.search_main, menu);

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

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


SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
@override
public boolean onQueryTextChange(String newText) {

// this is your adapter that will be filtered

adapter.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
adapter.getFilter().filter(query);
System.out.println("on query submit: "+query);

return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);

return super.onCreateOptionsMenu(menu);
}

@override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {


case R.id.ueber:

AlertDialog.Builder alert = new AlertDialog.Builder(this);


alert.setTitle("Über");
alert.setMessage("Datenschutzerklärung:");

// Set an EditText view to get user input
final TextView input = new TextView(this);
alert.setView(input);


input.setGravity(Gravity.CENTER_HORIZONTAL);
input.setText(R.string.uebertextpopup);


alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {


}
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});

alert.show();
return true;


default:
return super.onContextItemSelected(item);


}


}


@override
public boolean onNavigationItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.nav_home) {

Intent radfahrzeuge = new Intent(this, MainActivity.class);
startActivity(radfahrzeuge);

} else if (id == R.id.nav_pkw) {

Intent radfahrzeuge = new Intent(this, MainActivityPkw.class);
startActivity(radfahrzeuge);

} else if (id == R.id.nav_lkw) {

Intent radfahrzeuge = new Intent(this, MainActivityLkw.class);
startActivity(radfahrzeuge);

} else if (id == R.id.nav_anheunger) {
Intent anheunger = new Intent(this, MainActivityAnheunger.class);
startActivity(anheunger);

} else if (id == R.id.nav_pz) {
Intent rechner = new Intent(this, MainActivityPz.class);
startActivity(rechner);

} else if (id == R.id.nav_rechner) {
Intent rechner = new Intent(this, ZurrmittelActivity.class);
startActivity(rechner);
} else if (id == R.id.nav_map) {
Intent rechner = new Intent(this, MainActivityMap.class);
startActivity(rechner);
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout8);
drawer.closeDrawer(GravityCompat.START);

return false;
}


/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("MainActivityPz Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}

@override
public void onStart() {
super.onStart();

// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}

@override
public void onStop() {
super.onStop();

// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
}

PzCustomAdapter
public class PzCustomAdapter extends BaseAdapter{

String [] result,einheit;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public PzCustomAdapter(MainActivityPz mainActivity, String[] prgmNameList, int[] prgmImages, String[] prgmNameEinheit) {
// TODO Auto-generated constructor stub
result=prgmNameList;
einheit=prgmNameEinheit;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}

@override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public Filter getFilter() {
return null;
}


public class Holder
{
TextView tv,tv2;
ImageView img;
}
@override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.listviewpz_layout, null);
holder.tv=(TextView) rowView.findViewById(R.id.txtpz);
holder.tv2=(TextView) rowView.findViewById(R.id.curpz);
holder.img=(ImageView) rowView.findViewById(R.id.flagpz);
holder.tv.setText(result[position]);
holder.tv2.setText(einheit[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
// TODO Auto-generated method stub

if(result[position].equals("Fuchs")){
Intent intent = new Intent(context, FuchsActivity.class);
context.startActivity(intent);
}else if(result[position].equals("Boxer")){
Intent intent = new Intent(context, BoxerActivity.class);
context.startActivity(intent);
}


Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}


});
return rowView;
}

}
 

Ähnliche Themen

A
Antworten
10
Aufrufe
1.017
swa00
swa00
M
Antworten
4
Aufrufe
1.168
swa00
swa00
5
Antworten
0
Aufrufe
1.142
586920
5
Zurück
Oben Unten