过滤列表项打开原始列表项“活动 [英] Filtered list item opens the original list items' activity

查看:135
本文介绍了过滤列表项打开原始列表项“活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里的大量时间,搜索和其他任何地方,我绝望找到解决的办法。

因此​​,这里是我的问题。

我创建了一个列表视图和最重要的是我添加了一个搜索栏。

当我使用搜索栏来筛选结果...当我点击,而不是打开的具体点击的活动,即7项7,它总是从第一个。

我期待着您的帮助球员;因为我需要它!

解决方案

 公共类组扩展ListActivity {

 // ArrayList的多数民众赞成将要举办的搜索结果
ArrayList的< HashMap的<字符串,对象>>搜索结果;

 // ArrayList的将保存原始数据
ArrayList的< HashMap的<字符串,对象>> originalValues​​;
LayoutInflater充气;

@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.grouplist);
最后的EditText搜索盒=(EditText上)findViewById(R.id.searchBox);
ListView控件playersListView =(ListView控件)findViewById(android.R.id.list);


充气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

最后的EditText搜索盒=(EditText上)findViewById(R.id.searchBox);
ListView控件playersListView =(ListView控件)findViewById(android.R.id.list);

充气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//这些阵列仅仅是数据
//我将使用填充的ArrayList
字符串名称[] = {/ *名称列表* /};
字符串团队[] = {/ *的球队名单* /};
整数[]照片= {R.drawable ... / *可绘制列表* /};
整数[] ID = {/ *职位* /};

 originalValues​​ =新的ArrayList< HashMap的<字符串,对象>>();

//临时HashMap中来填充项目中的ListView
HashMap的<字符串,对象>温度;

//排在ListView总数
INT noOfPlayers = names.length;

//现在填充的ArrayList的球员
的for(int i = 0; I< noOfPlayers;我++){
临时=新的HashMap<字符串,对象>();

temp.put(名,名称[I]);
temp.put(团队,团队[I]);
temp.put(照片,照片[I]);
temp.put(ID,ID [I]);

//行添加到ArrayList
    originalValues​​.add(临时);
 }
    //的SearchResult = OriginalValues​​开始
  的SearchResult =新的ArrayList< HashMap的<字符串,对象>>(originalValues​​);

最后CustomAdapter适配器=新CustomAdapter(这一点,R.layout.players,SearchResult所);

 //最后,适配器设置为默认的ListView
playersListView.setAdapter(适配器);
sea​​rchBox.addTextChangedListener(新TextWatcher(){


公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
    //获取的EditText文本
    。字符串searchString = searchBox.getText()的toString();
    INT正文长度= searchString.length();

    //清除初始数据集
    sea​​rchResults.clear();

    的for(int i = 0; I< originalValues​​.size();我++){
        。字符串playerName = originalValues​​.get(I)获得(姓名)的toString();
        如果(正文长度&其中; = playerName.length()){
            //字符串的比较的EditText名称中带有
            // ArrayList的
            如果(searchString.equalsIgnoreCase(playerName.substring(0,正文长度)))
                sea​​rchResults.add(originalValues​​.get(ⅰ));
        }
    }

    adapter.notifyDataSetChanged();
}

公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){

}

公共无效afterTextChanged(编辑S){

}
});


//听上点击一个列表项
    playersListView.setOnItemClickListener(新OnItemClickListener(){
                公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){

        INT POS =的Integer.parseInt(searchResults.get(位置)获得(ID)的toString());
             开关(POS){
              情况下0:
               意图newActivity =新的意图(TeamsList.this,Barca.class);
               startActivity(newActivity);
               打破;
              情况1:
               等等...
             }
       }
    }
 });

}
 

自定义适配器类:

 私有类CustomAdapter扩展ArrayAdapter< HashMap的<字符串,对象>> {

公共CustomAdapter(上下文的背景下,INT textViewResourceId,ArrayList的< HashMap的<字符串,对象>>字符串){

//让机器人做初始化:)
超(背景下,textViewResourceId,字符串);
}

//类缓存的意见,一排
私有类ViewHolder {
ImageView的照片;
TextView的名字,团队;

}

ViewHolder viewHolder;

@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){

如果(convertView == NULL){
    convertView = inflater.inflate(R.layout.players,NULL);
    viewHolder =新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);

    //您可以在列表视图设计名为ID只能有一个TextView的
    viewHolder.id =(TextView中)convertView.findViewById(R.id.id);


    //缓存的意见链接到转换视图
    convertView.setTag(viewHolder);

  } 其他
    viewHolder =(ViewHolder)convertView.getTag();

 INT PHOTOID =(整数)searchResults.get(位置)获得(图片);

 //设置要显示的数据
viewHolder.photo.setImageDrawable(getResources()getDrawable(PHOTOID));
viewHolder.name.setText(searchResults.get(位置)获得(姓名)的toString());
viewHolder.team.setText(searchResults.get(位置)获得(小组)的toString());
viewHolder.id.setText(searchResults.get(位置)获得(ID)的toString());


//返回到显示的视图
返回convertView;

    }
  }
}
 

After a tremendous amount of time searching in here, and everywhere else I am hopeless to find a solution.

So here is my problem.

I have created a list-view and on top of that I added a search-bar.

When I use the search-bar, to filter the results... when I click on item 7, instead of opening the specific clicked activity i.e. 7, it always starts from the first one.

I am looking forward to your help guys; because I need it!

解决方案

public class Group extends ListActivity {

 // ArrayList thats going to hold the search results
ArrayList<HashMap<String, Object>> searchResults;

 // ArrayList that will hold the original Data
ArrayList<HashMap<String, Object>> originalValues;
LayoutInflater inflater;

@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);

final EditText searchBox = (EditText) findViewById(R.id.searchBox);
ListView playersListView = (ListView) findViewById(android.R.id.list);

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

// these arrays are just the data that
// I'll be using to populate the ArrayList
String names[] = {/*list of names*/ };
String teams[] = {/*list of teams*/};
Integer[] photos = {R.drawable.... /*list of drawables*/};
Integer[] id ={/*Position*/};

 originalValues = new ArrayList<HashMap<String, Object>>();

// temporary HashMap for populating the Items in the ListView
HashMap<String, Object> temp;

// total number of rows in the ListView
int noOfPlayers = names.length;

// now populate the ArrayList players
for (int i = 0; i < noOfPlayers; i++) {
temp = new HashMap<String, Object>();

temp.put("name", names[i]);
temp.put("team", teams[i]);
temp.put("photo", photos[i]);
temp.put("id", id[i]);

// add the row to the ArrayList
    originalValues.add(temp);
 }
    // searchResults=OriginalValues initially
  searchResults = new ArrayList<HashMap<String, Object>>(originalValues);

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

 // finally,set the adapter to the default ListView
playersListView.setAdapter(adapter);
searchBox.addTextChangedListener(new TextWatcher() {


public void onTextChanged(CharSequence s, int start, int before, int count) {
    // get the text in the EditText
    String searchString = searchBox.getText().toString();
    int textLength = searchString.length();

    // clear the initial data set
    searchResults.clear();

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

    adapter.notifyDataSetChanged();
}

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

}

public void afterTextChanged(Editable s) {

}
});


// listening to single list item on click
    playersListView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,      int position, long id) {

        int pos=Integer.ParseInt(searchResults.get(position).get("id").toString());
             switch (pos) {
              case 0:
               Intent newActivity = new Intent(TeamsList.this, Barca.class);
               startActivity(newActivity);
               break;
              case 1:
               etc...
             }
       }
    }
 });

}

Custom adapter Class:

private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>> {

public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {

// let android do the initializing :)
super(context, textViewResourceId, Strings);
}

// class for caching the views in a row
private class ViewHolder {
ImageView photo;
TextView name, team;

}

ViewHolder viewHolder;

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

if (convertView == null) {
    convertView = inflater.inflate(R.layout.players, null);
    viewHolder = new ViewHolder();

    // cache the views
    viewHolder.photo = (ImageView) convertView.findViewById(R.id.photo);
    viewHolder.name = (TextView) convertView.findViewById(R.id.name);
    viewHolder.team = (TextView) convertView.findViewById(R.id.team);

    //Take one textview in listview design named id
    viewHolder.id = (TextView) convertView.findViewById(R.id.id);


    // link the cached views to the convert view
    convertView.setTag(viewHolder);

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

 int photoId = (Integer) searchResults.get(position).get("photo");

 // set the data to be displayed
viewHolder.photo.setImageDrawable(getResources().getDrawable(photoId));
viewHolder.name.setText(searchResults.get(position).get("name").toString());
viewHolder.team.setText(searchResults.get(position).get("team").toString());
viewHolder.id.setText(searchResults.get(position).get("id").toString());


// return the view to be displayed
return convertView;

    }
  }
}

这篇关于过滤列表项打开原始列表项“活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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