筛选的列表项打开原始列表项的活动

时间:2015-09-19 03:38:57

标签: android listview android-activity android-listview android-search

经过大量的时间在这里和其他地方搜索,我无法找到解决方案。

所以这是我的问题。

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

当我使用搜索栏时,过滤结果...当我点击第7项时,它不是打开特定的点击活动,即7,而是始终从第一个开始。

我期待着你们的帮助;因为我需要它!

4 个答案:

答案 0 :(得分:1)

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

}

自定义适配器类:

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;

    }
  }
}

答案 1 :(得分:0)

我认为你无法在listview项目点击上找到正确的位置。所以你可以使用一个textview和visibility =&#34; Gone&#34;并在每行的textview中插入位置。现在,您可以轻松访问位置,同时单击具有textview值的项目,该值显示完美位置。希望它有效。感谢名单

答案 2 :(得分:0)

问题在于适配器已填充一次,但搜索结果会被搜索到的项目覆盖,因此点击它会引用列表的原始项目而不是过滤列表一次,因此我们必须使用过滤后的列表&#39 ;位置而不是原来的位置,我也遇到了这个问题,试试这个:

listView.setOnItemClickListener

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

    //Object objFilteredItem = parent.getItemAtPosition(position);
    //String a = searchResults.get(position);

    switch (Integer.parseInt((String) adapter.getItem(position))) {
    case 0:..
     Intent newActivity = new Intent(TeamsList.this,Barca.class);
     startActivity(newActivity);
     break;

    case 1:
     etc...
     break;
   }
 } 
});

答案 3 :(得分:0)

从我之前回答的问题here开始,您必须覆盖getItem(position)中的CustomAdapter方法。您正在正确设置onItemClick,但列表适配器不知道它从getItem(position)获得了什么。

编辑(详情):您需要在自定义适配器中添加类似的内容 -

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

您的自定义适配器中应该已经有list。如果没有,您可以向CustomAdapter添加列表参考:

private ArrayList<HashMap<String, Object>> list;

然后使用Group活动中的setter设置它:

customAdapter.setList(searchResults);