Hallo an alle ich hoffe ihr könnt mir Helfen.
Folgendes Problem:
Ich habe eine ListView und Search Button, beide Methoden funktionieren soweit, nur wenn beim Suche das Fahrzeug nach oben verschoben wird dann passen die Activitys nicht mehr, besser gesagt die Activitys bleiben an ihrer Positionen.
Die Frage ist, wie kann ich es machen das sich die Activitys mit bewegen?
import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by George on 04.08.2016.
*/
public class MainActivityPz extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private static final String TAG = MainActivityPz.class.getSimpleName();
SimpleAdapter adapter;
ListView list;
// Array of strings storing country names
String[] fahrzeuge = new String[]{
"Fuchs", //0
"Boxer", //1
"Dingo I", //2
"Biber", //3
"Dingo 2 GE A2", //4
"Dingo 2 GE C1 GSI", //5
"Fennek STF Lu/Bo", //6
"Leopard 2", //7
"Spz Marder", //8
"Mars II" //9
};
// Array of integers points to images stored in /res/drawable/
int[] imgid = new int[]{
R.drawable.fuchs, //0
R.drawable.boxer, //1
R.drawable.dingoeins, //2
R.drawable.biber, //3
R.drawable.dingozweigeazwei, //4
R.drawable.dingozweigsi, //5
R.drawable.fennek, //6
R.drawable.leopard, //7
R.drawable.marder, //8
R.drawable.marszwei //9
};
// Array of strings to store currencies
String[] einheit = new String[]{
"Kpf", //0
"Kpf", //1
"Kpf", //2
"Pionier", //3
"Kpf", //4
"Inst", //5
"Aufklärer", //6
"Kpf", //7
"Kpf", //8
"Artillerie" //9
};
@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 < 10; 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<?> arg0, View arg1, int pos,
long arg3) {
String team = fahrzeuge[pos];
if (team.equalsIgnoreCase("Fuchs")) {
Intent intent = new Intent(MainActivityPz.this, FuchsActivity.class);
startActivity(intent);
} else if (team.equalsIgnoreCase("Boxer")) {
Intent intent = new Intent(MainActivityPz.this, BoxerActivity.class);
startActivity(intent);
} //...and so on
// TODO Auto-generated method stub
String Slecteditem = fahrzeuge [+pos];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
System.out.println(pos+" --postion");
}
});
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);
}
@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);
}
}
}
Alles anzeigen