1

我想搜索扩展 simplecursoradapter 的 customlistview。我尝试了很多方法,但我没有得到,这是我的代码。我错过了什么。提前致谢。1.具有搜索功能的类

public class Alertadditem extends Activity {
private CustomContacts custAdapter;
Editext edt;
ListView listview;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alertdesignlist);
String[] from = new String[] { Contactsnew.CONTENT };
int[] to = new int[] { R.id.content123};
custAdapter = new CustomContacts(this,
        R.layout.activity_contactitem, null, from, to);
listview.setAdapter(custAdapter);
listview.setTextFilterEnabled(true);
edt.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            //setUpSearchList();
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            dataAdapter.getFilter().filter(s);  
        }

    });

2.customadapter

public class CustomContacts extends SimpleCursorAdapter {

public CustomContacts(Context context, int layout, Cursor c, String[] from,
        int[] to) {
    super(context, layout, c, from, to);
    // TODO Auto-generated constructor stub
}
public CharSequence convertToString(Cursor cursor) {
    return cursor.getString(cursor.getColumnIndex(Contactsnew.CONTENT));
    }
 @Override 
  public View getView(int position, View convertView, ViewGroup parent) {  

   //get reference to the row
   View view = super.getView(position, convertView, parent); 
   //check for odd or even to set alternate colors to the row background
   if(position % 2 == 0){  
    view.setBackgroundColor(0xFFC1E7FF);
   }
   else {
    view.setBackgroundColor(0xFF6495ED);
   }
   final TextView ttext = (TextView)view.findViewById(R.id.finaltext);
    final LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll11);
   final TextView tcontent = (TextView)view.findViewById(R.id.content123);
   final CheckBox check = (CheckBox)view.findViewById(R.id.itemcheck);
    check.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(check.isChecked()){
            tcontent.setPaintFlags(tcontent.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            ttext.setVisibility(v.VISIBLE);
            ll.setVisibility(v.VISIBLE);


        }else{
            tcontent.setPaintFlags(tcontent.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
            ttext.setVisibility(v.GONE);
            ll.setVisibility(LinearLayout.GONE);
        }

    }

});
   return view;  
  }  
}
4

2 回答 2

2

试试这个

public class CustomContacts extends android.widget.CursorAdapter {

 private LayoutInflater mLayoutInflater;
 private Context mContext;

public CustomContacts(Context context, Cursor c) {
    super(context, c);
    // TODO Auto-generated constructor stub
     mContext = context;
     mLayoutInflater = LayoutInflater.from(context); 
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    // TODO Auto-generated method stub
     String data = cursor.getString(cursor.getColumnIndexOrThrow(Contactsnew.CONTENT));

     if(cursor.getPosition() % 2 == 0){  
            view.setBackgroundColor(0xFFC1E7FF);
           }
           else {
            view.setBackgroundColor(0xFF6495ED);
     }

       final TextView ttext = (TextView)view.findViewById(R.id.finaltext);
       final LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll11);
       final TextView tcontent = (TextView)view.findViewById(R.id.content123);
       if (tcontent != null) {
           tcontent.setText(data);
        }

       final CheckBox check = (CheckBox)view.findViewById(R.id.itemcheck);
       check.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(check.isChecked()){
                tcontent.setPaintFlags(tcontent.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                ttext.setVisibility(v.VISIBLE);
                ll.setVisibility(v.VISIBLE);


            }else{
                tcontent.setPaintFlags(tcontent.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
                ttext.setVisibility(v.GONE);
                ll.setVisibility(LinearLayout.GONE);
            }

        }

     });
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = mLayoutInflater.inflate( R.layout.activity_contactitem,parent, false);
    return v;
}

}

并将适配器设置为

custAdapter = new CustomContacts(this,your_cursor);
listview.setAdapter(custAdapter);

'R.id.finaltext'、'R.id.ll11' 和 'R.id.itemcheck' 必须包含在布局 'R.layout.activity_contactitem'

于 2013-08-16T13:45:14.210 回答
1

我也有同样的问题,在你的 db 类中尝试这些代码,并从你的主类中对这些代码进行分类。

public static Cursor fetchAllvalues1() {

  Cursor mCursor = db.query(Contactsnew.TABLE02, new String[] {Contactsnew.userid,Contactsnew.userId,Contactsnew.CATEGORY,Contactsnew.TITLE},
    null, null, null, null, null);

  if (mCursor != null) {
   mCursor.moveToFirst();
  }
  return mCursor;
 }
public static Cursor fetchCountriesByName1(String inputText) throws SQLException {
  Log.w(TAG, inputText);
  Cursor mCursor = null;
  if (inputText == null  ||  inputText.length () == 0)  {
   mCursor = db.query(Contactsnew.TABLE02, new String[] {Contactsnew.userid,Contactsnew.userId,Contactsnew.CATEGORY,Contactsnew.TITLE},
     null, null, null, null, null);

  }
  else {
      mCursor=db.rawQuery(" SELECT * FROM "+ Contactsnew.TABLE02 + " WHERE " 
                + Contactsnew.userId + " = " + GetSet.getUserId() + " AND " + Contactsnew.TITLE +
                         " LIKE ? " ,new String []{inputText +"%"});
  }
  if (mCursor != null) {
   mCursor.moveToFirst();
  }
  return mCursor;

 }
于 2013-09-11T13:57:43.800 回答