如何启动后,搜索过滤列表视图新的意图是什么? [英] How to start new intent after search-filtering listview?

查看:109
本文介绍了如何启动后,搜索过滤列表视图新的意图是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我有一个问题:

我创建了一个列表视图,与搜索栏等。所以,当我点击一个项目(通过的switch-case)所选择的新的活动打开,不存在任何问题。

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.

您可以看到code在这里:

you can see the code here:

<一个href="https://stackoverflow.com/questions/32663932/filtered-list-item-opens-the-original-list-items-activity">Filtered列表项打开原始列表项活动

该问题开始的时候我筛选列表视图,在搜索栏,而不是打开所选活动(让我们说活动10),它抛出我回活动1等等。

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.

我已经想通了,到目前为止是最有可能的CustomAdapter以某种方式混合我的活动,不知道是选择哪个活动;因此,它抛出我回到开头。

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.

任何帮助AP preciated。

Any help appreciated.

推荐答案

在你的code,你给click事件根据列表视图的位置。例如: 玩家= {测试1,测试2,TEST3}

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

案例1:搜索没事的时候:       第一个位置都有球员的test1

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

2种情况:当用户搜索test2的。    现在列表视图搜索TEST2和找到的这个只有一个实例,以及searchlist将只有一个播放机,这将是在第一位置。既然你给基于位置时点击事件,第一行总是会来巴萨

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

更新:

您需要更改code如下

you need to change your Code as below

Group.java

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

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();
}
}

新类Player.java

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;
}
}

这篇关于如何启动后,搜索过滤列表视图新的意图是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆