简体   繁体   中英

OnItemClickListener in custom listview

I have a listview with search that works correctly except displaying the correct details from a second array dependant on the listview arrays position. if i dont search the list everything displays perfectly after the item click but if i search then click on the item it returns the item position not the array position how would i get the item array position. I must have my position based on numbers. Like 0,1,2,3, ... Thank you. any help pls. you are my last hope :(

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

    }

}

确保您在listview的项目布局中不要声明button或imagebutton,因为这会使onitemclicklistener不起作用

I guess I have found the solution. Use the following code.

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

I have not test the code but used similar approach any time. Let me know if it solves your problem.

thank all of you good people.

this code works:

            @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);

            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM