【发布时间】:2015-10-15 15:49:03
【问题描述】:
我有一个可以正常工作的列表视图,除了显示来自第二个数组的正确详细信息,具体取决于列表视图数组的位置。如果我不搜索列表,则在单击项目后一切都会完美显示,但是如果我搜索然后单击该项目,它将返回项目位置而不是数组位置,我将如何获得项目数组位置。 我必须根据数字确定我的位置。 像 0,1,2,3, ... 谢谢你。 任何帮助请。 你是我最后的希望:(
public class Main extends Activity {
EditText edittext;
ListView listview;
String[] text;
int textlength = 0;
ArrayList<String> text_sort = new ArrayList<String>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = new String[23];
for (int x = 1; x < 23 + 1; x = x + 1) {
String this_subject = "subject_" + String.valueOf(x);
int resID = getResources().getIdentifier(this_subject, "string",
getPackageName());
text[x - 1] = getResources().getString(resID);
}
edittext = (EditText) findViewById(R.id.EditText01);
listview = (ListView) findViewById(R.id.ListView01);
listview.setAdapter(new MyCustomAdapter(text, null));
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();
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]);
}
}
}
listview.setAdapter(new MyCustomAdapter(text_sort, null));
}
});
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Here I have to send the location of each item to an intent
//this code send it without search action
/*Intent i = new Intent(getApplicationContext(), myclass);
String Subject_number = String.valueOf(position + 1);
i.putExtra("subject_number", Subject_number);
startActivity(i);*/
}
});
}
class MyCustomAdapter extends BaseAdapter {
String[] data_text;
MyCustomAdapter() {
}
MyCustomAdapter(String[] text, int[] image) {
data_text = text;
}
MyCustomAdapter(ArrayList<String> text, ArrayList<Integer> image) {
data_text = new String[text.size()];
for (int i = 0; i < text.size(); i++) {
data_text[i] = text.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);
textview.setText(data_text[position]);
Animation animation = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.slide_in_right);
row.startAnimation(animation);
return (row);
}
}
【问题讨论】:
标签: android arrays listview android-listview