【问题标题】:OnItemClickListener in custom listview自定义列表视图中的 OnItemClickListener
【发布时间】: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


    【解决方案1】:

    确保在列表视图项目的布局中不要声明按钮或图像按钮,因为这会使 onitemclicklistener 不起作用

    【讨论】:

    • 你想点击返回例如 R.id.TextView01 吗?
    【解决方案2】:

    我想我已经找到了解决方案。使用以下代码。

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

    我没有测试过代码,但任何时候都使用过类似的方法。让我知道它是否能解决您的问题。

    【讨论】:

      【解决方案3】:

      感谢你们所有的好人。

      此代码有效:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-25
        相关资源
        最近更新 更多