我有一个带有搜索列表视图的搜索框的列表视图。我在评估每个项目以启动不同的活动时遇到问题,因为当我搜索时,项目的位置和 ID 会发生变化,因为列表视图将仅显示搜索结果。可以有人帮忙吗??
这是我的代码:
public class MainActivity extends Activity implements OnItemClickListener {
EditText edittext;
ListView listview;
String[] text = { "One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten" };
int[] image = { R.drawable.one, R.drawable.two, R.drawable.three,
R.drawable.four, R.drawable.five, R.drawable.six, R.drawable.seven,
R.drawable.eight, R.drawable.nine, R.drawable.ten };
int textlength = 0;
ArrayList text_sort = new ArrayList();
ArrayList image_sort = new ArrayList();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edittext = (EditText) findViewById(R.id.EditText01);
listview = (ListView) findViewById(R.id.ListView01);
listview.setAdapter(new MyCustomAdapter(text, image));
listview.setOnItemClickListener(this);
edittext.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after)
{
}
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
textlength = edittext.getText().length();
text_sort.clear();
image_sort.clear();
for(int i=0; i<text.length; i++)
{
if (textlength <= text[i].length())
{
if (edittext.getText().toString().
equalsIgnoreCase((String) text[i].subSequence(0, textlength)))
{
text_sort.add(text[i]);
image_sort.add(image[i]);
}
}
}
listview.setAdapter(new MyCustomAdapter
(text_sort, image_sort));
}
});
}
class MyCustomAdapter extends BaseAdapter
{
String[] data_text;
int[] data_image;
MyCustomAdapter()
{
}
MyCustomAdapter(String[] text, int[] image)
{
data_text = text;
data_image = image;
}
MyCustomAdapter(ArrayList text, ArrayList image)
{
data_text = new String[text.size()];
data_image = new int[image.size()];
for(int i=0; i<text.size(); i++)
{
data_text[i] = (String) text.get(i);
data_image[i] = (Integer) image.get(i);
}
}
public int getCount()
{
return data_text.length;
}
public String getItem(int position)
{
return null;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.listview, parent, false);
TextView textview = (TextView) row.findViewById(R.id.TextView01);
ImageView imageview = (ImageView) row.findViewById(R.id.ImageView01);
textview.setText(data_text[position]);
imageview.setImageResource(data_image[position]);
return (row);
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub
//HOW SHOULD I HANDLE THE CLICKS HERE
}
}
我的解决方案不起作用:
1)获取item的位置,去对应的activity
2) 获取 id ................................... …………