大家好我有一个问题:
我创建了一个listview,带有搜索栏等。因此,当我点击某个项目(通过switch-case)时,所选的新活动将打开,并且没有任何问题。
你可以在这里看到代码:
Filtered list item opens the original list items' activity
当我使用搜索栏过滤列表视图时,问题就开始了,而不是打开所选活动(让我们说活动10),它会把我引回活动1,等等。
到目前为止我所知道的是,很可能CustomAdapter以某种方式混合了我的活动,并且不知道选择了哪个活动;因此,它让我回到了开始。
请伙计们,我正在努力解决这个问题超过一个月,但仍然没有希望。
任何帮助表示感谢。
答案 0 :(得分:0)
在您的代码中,您根据listview的位置给出了click事件。例如: 球员= {" test1"," test2"," test3"}
案例1:当没有搜索到任何内容时: 第一个位置有玩家test1
案例2:用户搜索test2时。 现在listview搜索test2并只找到一个这样的实例,搜索列表将只有一个播放器,它将位于第一个位置。由于您已根据位置给出了点击事件,因此第一行将始终显示为Barca
<强>更新强>
您需要更改以下代码
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();
}
}
名为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;
}
}