簡體   English   中英

自定義列表視圖中的OnItemClickListener

[英]OnItemClickListener in custom listview

我有一個帶有搜索的列表視圖,該列表可以正常工作,只是根據依賴於列表視圖數組位置的第二個數組顯示正確的詳細信息。 如果我不搜索列表,則單擊項目后所有內容均顯示完美,但是如果我搜索然后單擊該項目,它將返回項目位置而不是數組位置,我將如何獲得項目數組位置。 我的職位必須基於數字。 像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);

    }

}

確保您在listview的項目布局中不要聲明button或imagebutton,因為這會使onitemclicklistener不起作用

我想我已經找到了解決方案。 使用以下代碼。

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

我沒有測試代碼,但是隨時使用類似的方法。 讓我知道它是否可以解決您的問題。

謝謝大家。

此代碼有效:

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

            }

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM