【问题标题】:Listview with custom CursorAdapter and带有自定义 CursorAdapter 的 Listview 和
【发布时间】:2015-12-28 06:34:45
【问题描述】:

我尝试使用自定义视图创建自定义 Listview。

慢列表,一些重复的行,效果不好。

我不知道代码是否正确。

这是我的代码:

public class ActDictionary extends Activity
{

    private TextView searchBox;
    private ListView wordsList;
    private Cursor c;

    private ActDictionary.CustomA wordListAdapter;

    private boolean defaultLanguage;

    private String TableIs;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO: Implement this method
        super.onCreate(savedInstanceState);
        Wind.in(this);
        setContentView(R.layout.act_dictionary);

        searchBox=(TextView)findViewById(R.id.actdictionaryEditText1);
        wordsList=(ListView)findViewById(R.id.act_dictionary_list);
        defaultLanguage=true;

        ((TextView)findViewById(R.id.actdictionaryTextView1)).setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View p1)
                {
                    defaultLanguage=!defaultLanguage;
                    setListData();
                }
            });
        setListData();
    }
    public void setListData(){
        if(defaultLanguage){
            ((TextView)findViewById(R.id.actdictionaryTextView1)).setText("Franch >> English");
            c=Dictionary.in(this).database("dictionary.db").codes().__get_all_rows("fr2en");
            c.moveToFirst();
            TableIs="fr2en";
            loaddata();
        }else{
            ((TextView)findViewById(R.id.actdictionaryTextView1)).setText("English >> Franch");
            c=Dictionary.in(this).database("dictionary.db").codes().__get_all_rows("en2fr");
            c.moveToFirst();
            TableIs="en2fr";
            loaddata();
        }
        //String from[] ={"a","b"};
        //int to[]={R.id.dictionaryrawTextView1,R.id.dictionaryrawTextView2};

    }
    public void loaddata(){
        wordListAdapter=new CustomA(this,c,TableIs);//R.layout.dictionary_raw,c,from,to);

        wordsList.setAdapter(wordListAdapter);
        searchBox.addTextChangedListener(new TextWatcher(){

                @Override
                public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
                {
                    // TODO: Implement this method
                }

                @Override
                public void onTextChanged(CharSequence p1, int p2, int p3, int p4)
                {
                    wordListAdapter.notifyDataSetChanged();
                    wordListAdapter.getFilter().filter(p1);
                }

                @Override
                public void afterTextChanged(Editable p1)
                {

                }
            });
        wordListAdapter.setFilterQueryProvider(new FilterQueryProvider(){

                @Override
                public Cursor runQuery(CharSequence p1)
                {
                    String str=p1.toString();
                    if(defaultLanguage){
                        return Dictionary.in(getBaseContext()).database("dictionary.db").codes().__get_all_rows("fr2en",str+"%");
                    }
                    return Dictionary.in(getBaseContext()).database("dictionary.db").codes().__get_all_rows("en2fr",str+"%");
                }
            });
    }





public class CustomA extends CursorAdapter
    {

        private LayoutInflater inflater;

        private Context mctx;

        private Cursor mcu;

        private boolean starIs;

        private String table;

        @Override
        public View newView(Context p1, Cursor p2, ViewGroup p3)
        {
            // TODO: Implement this method
            return null;
        }

        @Override
        public void bindView(View p1, Context p2, Cursor p3)
        {

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            convertView=null;
            View v=inflater.inflate(R.layout.dictionary_raw,parent,false);
            //LinearLayout l=(LinearLayout)v.findViewById(R.id.dictionaryrawLinearLayout1);
            WrapLayout l=(WrapLayout)v.findViewById(R.id.flow_layout);
            TextView tv=(TextView)v.findViewById(R.id.dictionaryrawTextView1);
            final ImageView img=(ImageView)v.findViewById(R.id.star);
            tv.setText(getCursor().getString(getCursor().getColumnIndex("value1")));
            String temp=getCursor().getString(getCursor().getColumnIndex("value2"));
            final String star=Dictionary.in(getBaseContext()).database("dictionary.db").codes().___getStar(table,getCursor().getString(getCursor().getColumnIndex("_id")));//mcu.getString(getCursor().getColumnIndex("value4"));
            String values[]=temp.split(",|\\;");
            if(star.equals("0")){
                img.setImageResource(R.drawable.ic_star_off);
            }else{
                img.setImageResource(R.drawable.ic_star_on);

            }
            String color[]={"#123232","#544344","#774499","#330088","#546789","#223322"};
            TextView[] textView=new TextView[values.length];
            for (int i=0;i<values.length;i++){
                textView[i]=new TextView(getBaseContext());
                textView[i].setText(values[i]);
                textView[i].setTextColor(Color.WHITE);

                textView[i].setBackgroundResource(R.drawable.button);
                l.addView(textView[i]);
            }
            //final ImageView button01 = (ImageView) findViewById(R.id.button01);
            final int cc=getCursor().getInt(getCursor().getColumnIndex("_id"));
            img.setOnClickListener(new OnClickListener() {
                    int button01pos = Integer.parseInt(star);
                    public void onClick(View v) {
                        if (button01pos == 0) {
                            img.setImageResource(R.drawable.ic_star_on);
                            button01pos = 1;
                            Dictionary.in(getBaseContext()).database("dictionary.db").codes().___upadteValue(table,"1",cc);

                            Toast.makeText(getBaseContext(),""+cc,1).show();
                        } else if (button01pos == 1) {
                            img.setImageResource(R.drawable.ic_star_off);
                            button01pos = 0;

                            Dictionary.in(getBaseContext()).database("dictionary.db").codes().___upadteValue(table,"0",cc);
                        }
                    }
                });
            return v;

        }




public CustomA(Context ctx,Cursor cu,String c){
            super(ctx,cu);
            mctx=ctx;
            mcu=cu;
            table=c;
            inflater=LayoutInflater.from(ctx);
        }
    }
}

而数据库是

public class Codes
{

    private SQLiteDatabase mDb; 

    public Codes(SQLiteDatabase db)
    {
        this.mDb = db;
    }

    public String ___getStar(String table, String string)
    {
        Cursor c= mDb.rawQuery("SELECT  value4 from "+table+" WHERE _id = ? ",new String[]{string});
        c.moveToFirst();
        return c.getString(c.getColumnIndex("value4"));
    }

    public Cursor __get_all_rows_star(String table){
        return mDb.rawQuery("select * from "+table+" where value4 ='1'",null);
    }
    public Cursor __get_all_rows_star(String tableName,String args){
        return mDb.rawQuery("select * from "+tableName+" where value1 like ? ORDER BY value1 COLLATE NOCASE LIMIT 50",new String[]{args});
    }
    public void ___upadteValue(String table, String value,int id){
        mDb.execSQL("UPDATE "+table+" SET value4 = '"+value+"' WHERE _id = "+id);
        System.err.println("UPDATE "+table+" SET value4 = "+value+" WHERE _id = "+id);
    }
    public Cursor __get_all_rows(String tableName){
        return mDb.rawQuery("select * from "+tableName+" ORDER BY RANDOM() LIMIT 100",null);
    }
    public Cursor __get_all_rows(String tableName,String args){
        return mDb.rawQuery("select * from "+tableName+" where value1 like ? ORDER BY value1 COLLATE NOCASE LIMIT 50",new String[]{args});
    }


    public Cursor test(){
        return mDb.rawQuery("select * from fr2en where value4= ?",new String[]{"1"});
    }
}

【问题讨论】:

  • 只需使用SimpleCursorAdapter(或ResourceCursorAdapter),如果您不知道view reusing 的工作原理,则无需自定义CursorAdapter
  • 我尝试使用 SimpleCursorAdapter 但是使用这部分 TextView[] textView=new TextView[values.length]; for (int i=0;i

标签: android android-listview cursor android-widget


【解决方案1】:

为您的适配器使用 ViewHolder 模式。 检查此链接Custom Adapter

请参阅链接中的第 9.5 节

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    相关资源
    最近更新 更多