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.カスタムアダプター

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

}

setadapter として

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 に答える