我的搜索列表视图工作正常,除了根据列表视图数组位置显示第二个数组的正确详细信息。如果我不搜索列表,一切都在项目点击后完美显示但是如果我搜索然后点击项目它返回项目位置而不是数组位置我将如何获得项目数组位置。 我必须根据数字确定我的立场。 像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);
}
}
答案 0 :(得分:0)
确保在listview项目的布局中不要声明按钮或图像按钮,因为这会使onitemclicklistener不起作用
答案 1 :(得分:0)
我想我找到了解决方案。使用以下代码。
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int orgPos;
if (text.length != text_sort.size() ) {
// The list on which we clicked is sorted!
String clickedText = text_sort.get(position);
int i = 0; boolean found = false;
while ( i < text.length && found == false) {
if (clickedText == text[i]) {
orgPos = i;
found = true;
}
else { i++; }
}
}
else {
// Original List click happened
orgPos = position;
}
// You got original position as orgPos now intent
}
});
我没有测试代码,但是随时都使用了类似的方法。如果它能解决您的问题,请告诉我。
答案 2 :(得分:0)
此代码有效:
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int orgPos = 0;
if (text.length != text_sort.size()) {
// The list on which we clicked is sorted!
String clickedText = text_sort.get(position);
int i1 = 0;
boolean found = false;
while (i1 < text.length && found == false) {
if (clickedText == text[i1]) {
orgPos = i1;
found = true;
} else {
i1++;
}
}
Intent i2 = new Intent(getApplicationContext(),
Secendbardari1.class);
String Subject_number = String.valueOf(orgPos + 1);
i2.putExtra("subject_number", Subject_number);
startActivity(i2);
} else {
Intent i = new Intent(getApplicationContext(),
Secendbardari1.class);
String Subject_number = String.valueOf(position + 1);
i.putExtra("subject_number", Subject_number);
startActivity(i);
}
}