【问题标题】:how to get custom listview with searchbox is clicked?如何获得带有搜索框的自定义列表视图被点击?
【发布时间】:2012-06-26 13:30:18
【问题描述】:

我有三个列表,其中包含“根、顶部和向下”。 但是当我用“top”过滤列表时,然后我点击列表,它出现的是“root”。我怎样才能使它成为我们过滤的可点击列表?

list.java

    public class root extends Activity {

EditText edittext;
ListView listview;

private String[] text = { "ROOT", "TOP", "DOWN" };

private int[] image = { R.drawable.root, R.drawable.top, R.drawable.down };

int textlength = 0;

ArrayList<String> text_sort = new ArrayList<String>();
ArrayList<Integer> image_sort = new ArrayList<Integer>();

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainlistview);

    this.getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    edittext = (EditText) findViewById(R.id.EditText01);
    listview = (ListView) findViewById(R.id.ListView01);
    listview.setAdapter(new MyCustomAdapter(text, image));

    listview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            if ("ROOT".equals(text[position])) {
                Intent myIntent = new Intent(view.getContext(),
                        root.class);
                startActivityForResult(myIntent, 0);

            }
            if ("TOP".equals(text[position])) {
                Intent myIntent = new Intent(view.getContext(),
                        top.class);
                startActivityForResult(myIntent, 0);
            }
            if ("DOWN".equals(text[position])) {
                Intent myIntent = new Intent(view.getContext(),
                        down.class);
                startActivityForResult(myIntent, 0);

        }

    });

    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();
            image_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]);
                        image_sort.add(image[i]);
                    }
                }
            }

            listview.setAdapter(new MyCustomAdapter(text_sort, image_sort));

        }
    });
}

class MyCustomAdapter extends BaseAdapter {

    String[] data_text;
    int[] data_image;

    MyCustomAdapter() {

    }

    MyCustomAdapter(String[] text, int[] image) {
        data_text = text;
        data_image = image;
    }

    MyCustomAdapter(ArrayList<String> text, ArrayList<Integer> image) {

        data_text = new String[text.size()];
        data_image = new int[image.size()];

        for (int i = 0; i < text.size(); i++) {
            data_text[i] = text.get(i);
            data_image[i] = image.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);
        ImageView imageview = (ImageView) row
                .findViewById(R.id.ImageView01);

        textview.setText(data_text[position]);
        imageview.setImageResource(data_image[position]);

        return (row);

    }
}

}

main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/EditText01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Search" >
</EditText>

<ListView
    android:id="@+id/ListView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</ListView>

listview.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff200"
android:gravity="left|center"
android:paddingBottom="5px"
android:paddingLeft="5px"
android:paddingTop="5px" >

<ImageView
    android:id="@+id/ImageView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</ImageView>

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10px"
    android:textColor="#0099CC"
    android:textSize="20px"
    android:textStyle="bold" >
</TextView>

【问题讨论】:

    标签: android listview onclicklistener custom-lists


    【解决方案1】:

    问题出在你的onItemClickListener 或你的ListView

    listview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
    
            if ("ROOT".equals(text[position])) {
                Intent myIntent = new Intent(view.getContext(),class);
                startActivityForResult(myIntent, 0);
    

    过滤您的ListView 后,您的ListView 将仅显示一项。当您点击此项时,onItemClick 将被触发。因为您点击的项目位于新过滤的ListView 的第一个位置,所以变量position 为0。Array text[] 中的第一个项目是“ROOT”,因此将启动 RootActivity。

    解决方案

    询问您的适配器您单击了哪个视图并将其与字符串进行比较:

    在你的onItemClicked(..)

    View selectedView = yourAdapter.getItemAtPosition(position)
    TextView myText = (TextView) selectedView.findViewById(R.id.TextView01).getText() 
    
    if(myText.equals("ROOT")){
       // start Activity..
    

    【讨论】:

    • 我不知道,所以我只是把这个“View selectedView = MyCustomAdapter.getItemAtPosition(position) TextView myText = (TextView) selectedView.findViewById(R.id.TextView01).getText()” - 在我的:“public void onItemClick(AdapterView> parent, View view, int position, long id) {"
    • 我上传了文件mediafire.com/?binttnilkoh5iuc你能帮我修复它吗..谢谢
    • 如果您按字面复制粘贴,我提供的代码可能无法正常工作。这是为了让您了解可能的解决方案:)
    • 你能帮我修复我的示例项目吗? mediafire.com/?binttnilkoh5iuc
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 2013-04-03
    相关资源
    最近更新 更多