i made ListView, i gave to the single row their own activity, but when i use filter (EditText) they are changing position. I mean if first row is called dog and its opening activity with dog image, when i try to find cat row with filter, its always opening dog image cause its on the first place. How can i connected dog row with dog activity? To make them move togerher? Below i paste my code. Please help me :( Intents are filled with String from string.xml (animals was just example)
public class MainActivity extends ActionBarActivity {
ArrayAdapter<String> dataAdapter = null;
ArrayList<String> elementy = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
ListaPiosenek();
powiadomienie();
}
private void ListaPiosenek() {
ArrayList<String> elementy = new ArrayList<String>();
elementy.add("Dom w górach");
elementy.add("Jedyne co mam");
elementy.add("Koncert");
elementy.add("Ocean");
elementy.add("Tolerancja");
elementy.add("Wiesiek idzie");
elementy.add("Wiosna, ach to ty");
elementy.add("Chłopaki nie płaczą");
elementy.add("Dziki Włóczęga");
elementy.add("Imperatyw");
elementy.add("Piosenka Turystyczna III");
elementy.add("Remedium");
elementy.add("Kiedy będę starą kobietą");
elementy.add("Długość dźwięku samotności");
elementy.add("Król");
elementy.add("Anna");
elementy.add("Syn Miasta");
elementy.add("Samotna fregata");
elementy.add("Balonik");
elementy.add("Partyzant");
elementy.add("Długość dźwięku samotności");
elementy.add("Długość dźwięku samotności");;
elementy.add("Długość dźwięku samotności");
elementy.add("Długość dźwięku samotności");
elementy.add("Długość dźwięku samotności");
elementy.add("Długość dźwięku samotności");
elementy.add("Długość dźwięku samotności");
//Collections.sort(elementy);
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,R.layout.single_row,elementy);
ListView list=(ListView)findViewById(R.id.listView1);
list.setAdapter(dataAdapter);
list.setFocusableInTouchMode(true);
list.requestFocus();
list.setTextFilterEnabled(true);
EditText myFilter =(EditText)findViewById(R.id.editText1);
myFilter.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
dataAdapter.getFilter().filter(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
private void powiadomienie() {
ListView list = (ListView)findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
if(position==0){
Intent i = new Intent(MainActivity.this,Utwor0.class);
startActivity(i);
}
else if(position==1){
Intent i = new Intent(MainActivity.this, Utwor1.class);
startActivity(i);
}
else if(position==2){
Intent i = new Intent(MainActivity.this, Utwor2.class);
startActivity(i);
}
else if(position==3){
Intent i = new Intent(MainActivity.this, Utwor3.class);
startActivity(i);
}
else if(position==4){
Intent i = new Intent(MainActivity.this, Utwor4.class);
startActivity(i);
}
else if (position==5){
Intent i = new Intent(MainActivity.this, Utwor5.class);
startActivity(i);
}
else if(position==6){
Intent i = new Intent(MainActivity.this, Utwor6.class);
startActivity(i);
}
else if(position==7){
Intent i = new Intent(MainActivity.this, Utwor7.class);
startActivity(i);
}
else if(position==8){
Intent i = new Intent(MainActivity.this, Utwor8.class);
startActivity(i);
}
else if(position==9){
Intent i = new Intent(MainActivity.this, Utwor9.class);
startActivity(i);
}
else if(position==10){
Intent i = new Intent(MainActivity.this, Utwor10.class);
startActivity(i);
}
else if(position==11){
Intent i = new Intent(MainActivity.this, Utwor11.class);
startActivity(i);
}
else if(position==12){
Intent i = new Intent(MainActivity.this, Utwor12.class);
startActivity(i);
}
else if(position==13){
Intent i = new Intent(MainActivity.this, Utwor13.class);
startActivity(i);
}
else if(position==14){
Intent i = new Intent(MainActivity.this, Utwor14.class);
startActivity(i);
}
else if(position==15){
Intent i = new Intent(MainActivity.this, Utwor15.class);
startActivity(i);
}
else if(position==16){
Intent i = new Intent(MainActivity.this, Utwor16.class);
startActivity(i);
}
else if(position==17){
Intent i = new Intent(MainActivity.this, Utwor17.class);
startActivity(i);
}
else if(position==18){
Intent i = new Intent(MainActivity.this, Utwor18.class);
startActivity(i);
}
else if(position==19){
Intent i = new Intent(MainActivity.this, Utwor19.class);
startActivity(i);
}
}
});
}
}
Make the elementry ArrayList global to the class and change the logic like this,
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
String data = parent.getAdapter().getItem(position);
final int index = elementy.indexOf(data);
if(index == 0){
Intent i = new Intent(MainActivity.this,Utwor0.class);
startActivity(i);
}
}};
Its still not working. I read something about viewHolder but its still the same problem. I write VH below, maybe i'm doing something wrong, i dont know.
class ViewHolder{
TextView text=null;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView==null){
LayoutInflater inflater = getLayoutInflater();
convertView =inflater.inflate(R.layout.single_row, null);
holder=new ViewHolder();
holder.text=(TextView)convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
}
else{
holder=(ViewHolder)convertView.getTag();
}
holder.text.setText(elementy.get(position));
return (convertView);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.