0

我已经创建了自己的自定义 ListView 适配器,所以我试图过滤一些项目,任何想法我可以如何做到这一点。在标准适配器中很容易,所以我尝试了这个,但它不起作用:

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class lista_imagini extends ListActivity{

    String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
            "consectetuer", "adipiscing", "elit", "morbi", "vel",
            "ligula", "vitae", "arcu", "aliquet", "mollis",
            "etiam", "vel", "erat", "placerat", "ante",
            "porttitor", "sodales", "pellentesque", "augue",
            "purus"};

    ImageAndTextAdapter adaptor = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clist);

        final ListView ls = getListView();
        if (ls == null) Log.d("DHA", "Cacat");
        ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Toast.makeText(getApplicationContext(), ls.getItemAtPosition(arg2).toString(), Toast.LENGTH_LONG).show();

            }

        });

        adaptor = new ImageAndTextAdapter(getApplicationContext(),R.layout.customlist,items);
        setListAdapter(adaptor);


    }

    public class ImageAndTextAdapter extends ArrayAdapter<String> {

        private LayoutInflater mInflater;

        private String[] mStrings;

        private int mViewResourceId;

        public ImageAndTextAdapter(Context ctx, int viewResourceId,
                String[] strings) {
            super(ctx, viewResourceId, strings);

            mInflater = (LayoutInflater)ctx.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);

            mStrings = strings;

            mViewResourceId = viewResourceId;
        }

        @Override
        public int getCount() {
            return mStrings.length;
        }

        @Override
        public String getItem(int position) {
            return mStrings[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = mInflater.inflate(mViewResourceId, null);

            ImageView iv = (ImageView)convertView.findViewById(R.id.icon2);
            iv.setImageResource(R.drawable.fail);

            TextView tv = (TextView)convertView.findViewById(R.id.label2);
            tv.setText(mStrings[position]);

            return convertView;
        }
    }

    public void testnow(View V)
    {
        items[3] = "Heeee";
        adaptor.getFilter().filter("ipsum");
        adaptor.notifyDataSetChanged();
        getListView().refreshDrawableState();

    }

}
4

1 回答 1

1

您将不得不扩展过滤器类。然后您将获得更改 getCount 和 getItem。检查此答案以获取示例

于 2012-05-10T15:10:28.687 回答