How to start new intent after search-filtering lis

2019-03-03 09:03发布

问题:

Hi guys I have a question:

I created a listview, with search-bar etc . So when I click on an item (through switch-case) the selected new activity opens and there are no problems.

you can see the code here:

Filtered list item opens the original list items' activity

The problems start when I filter the listview, with the search-bar, and instead of opening the selected activity (let us say activity 10), it throws me back to activity 1 and so forth.

What I have figured out so far is that most probably the CustomAdapter somehow mixes my activities and does not know which activity was chosen; hence, it throws me back to the beginning.

Please guys, I am trying for over a month to solve that issue and still no hope in the horizon.

Any help appreciated.

回答1:

In your code, you have given click event based on the position of listview. eg: players = {"test1","test2","test3"}

CASE 1: when nothing is searched: first position has player test1

case 2: when user search test2. Now listview search for test2 and find only one instance of this, and searchlist will have only one player and it will be at the first position. Since you have given click event based on position it, first row will always take to Barca

UPDATE:

you need to change your Code as below

Group.java

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;


public class Group extends ListActivity {

ArrayList<Players> originalValues;
LayoutInflater inflater;
int noOfPlayers = 10;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grouplist);
    final EditText searchBox = (EditText) findViewById(R.id.searchBox);
    ListView playersListView = (ListView) findViewById(android.R.id.list);

    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    originalValues = new ArrayList<Players>();

    for (int i = 0; i < noOfPlayers; i++) {
        // here you initialise the class with your own data
        Players players = new Players(i, "name" + i, "team", R.drawable.ic_launcher);

        originalValues.add(players);
    }

    final CustomAdapter adapter = new CustomAdapter(this, R.layout.players, originalValues);

    playersListView.setAdapter(adapter);
    searchBox.addTextChangedListener(new TextWatcher() {


        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String searchString = searchBox.getText().toString();
            int textLength = searchString.length();

            adapter.clearSearchResult();

            for (int i = 0; i < originalValues.size(); i++) {
                String playerName = originalValues.get(i).getName();
                if (textLength <= playerName.length()) {
                    // compare the String in EditText with Names in the
                    // ArrayList
                    if (searchString.equalsIgnoreCase(playerName.substring(0, textLength)))
                        adapter.addSeachResult(originalValues.get(i));
                }
            }

            adapter.notifyDataSetChanged();
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        public void afterTextChanged(Editable s) {

        }
    });


    playersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            switch ((int) adapter.getItemId(position)) {
                case 0:
                        Intent newActivity = new    Intent(Group.this, Barca.class);
                        startActivity(newActivity);
                    break;
                case 1:
                    etc
            }
        }
    });
}

}

CustomAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;


class CustomAdapter extends BaseAdapter {

ArrayList<Players> searchResults;

ViewHolder viewHolder;

public CustomAdapter(Context context, int textViewResourceId, ArrayList<Players> results) {
    searchResults = new ArrayList<>(results);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.players, null);
        viewHolder = new ViewHolder();

        viewHolder.photo = (ImageView) convertView.findViewById(R.id.photo);
        viewHolder.name = (TextView) convertView.findViewById(R.id.name);
        viewHolder.team = (TextView) convertView.findViewById(R.id.team);


        convertView.setTag(viewHolder);

    } else
        viewHolder = (ViewHolder) convertView.getTag();

    int photoId = (Integer) searchResults.get(position).getPicture();

    viewHolder.photo.setImageDrawable(parent.getContext().getResources().getDrawable(photoId));
    viewHolder.name.setText(searchResults.get(position).getName());
    viewHolder.team.setText(searchResults.get(position).getTeam());


    return convertView;

}

public void clearSearchResult() {
    searchResults.clear();
}

public void addSeachResult(Players result) {
    this.searchResults.add(result);
}

private class ViewHolder {
    ImageView photo;
    TextView name, team;

}

@Override
public int getCount() {
    return searchResults.size();
}

@Override
public Object getItem(int position) {
    return searchResults.get(position);
}

@Override
public long getItemId(int position) {
    return searchResults.get(position).getId();
}
}

New Class called Player.java

public class Players {

public Players(int id,String name,String team,int picture){
    this.id = id;
    this.name = name;
    this.team = team;
    this.picture = picture;
}

private int id;
private String name;
private String team;
private int picture;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getTeam() {
    return team;
}

public void setTeam(String team) {
    this.team = team;
}

public int getPicture() {
    return picture;
}

public void setPicture(int picture) {
    this.picture = picture;
}

public int getId() {
    return id;
}
}