यहाँ मेरी कस्टम CursorAdapter है: ViewHolder पैटर्न की कमी में इस गिरावटकस्टमहोसर एडाप्टर में सहीहोल्डर पैटर्न सही ढंग से कार्यान्वित किया गया है?
public class TasksAdapter extends CursorAdapter implements Filterable {
private final Context context;
public TasksAdapter(Context context, Cursor c) {
super(context, c);
this.context = context;
}
/**
* @see android.widget.CursorAdapter#newView(android.content.Context, android.database.Cursor, android.view.ViewGroup)
*/
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(android.R.layout.simple_list_item_checked, parent, false);
ViewHolder holder = new ViewHolder();
holder.textview = (CheckedTextView)v.findViewById(android.R.id.text1);
v.setTag(holder);
return v;
}
/**
* @see android.widget.CursorAdapter#bindView(android.view.View, android.content.Context, android.database.Cursor)
*/
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder)view.getTag();
int titleCol = cursor.getColumnIndexOrThrow(Tasks.TITLE);
int completedCol = cursor.getColumnIndexOrThrow(Tasks.COMPLETED);
String title = cursor.getString(titleCol);
boolean completed = Util.intToBool(cursor.getInt(completedCol));
holder.textview.setText(title);
holder.textview.setChecked(completed);
}
/**
* @see android.widget.CursorAdapter#runQueryOnBackgroundThread(java.lang.CharSequence)
*/
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
StringBuffer buffer = null;
String[] args = null;
if (constraint != null) {
buffer = new StringBuffer();
buffer.append("UPPER (");
buffer.append(Tasks.TITLE);
buffer.append(") GLOB ?");
args = new String[] { "*" + constraint.toString().toUpperCase() + "*" };
}
Cursor c = context.getContentResolver().query(Tasks.CONTENT_URI,
null, (buffer == null ? null : buffer.toString()), args,
Tasks.DEFAULT_SORT_ORDER);
c.moveToFirst();
return c;
}
/**
* @see android.widget.CursorAdapter#convertToString(android.database.Cursor)
*/
@Override
public CharSequence convertToString(Cursor cursor) {
final int titleCol = cursor.getColumnIndexOrThrow(Tasks.TITLE);
String title = cursor.getString(titleCol);
return title;
}
static class ViewHolder {
CheckedTextView textview;
}
}
करता है? मुझे यकीन नहीं था क्योंकि यह एक कर्सर एडाप्टर था, जहां getView
नहीं था। अगर कोई समस्या या सुझाव हैं, तो क्या आप उन्हें इंगित कर सकते हैं।
तो मैं View.findViewById (int) की तरह महंगा कॉल कर सकते हैं और अपने आवेदन अंतराल नहीं होगा? –
'findViewById (int)' उतना महंगा नहीं है जितना आप सोचते हैं। यह केवल एक संदर्भ लौटाएगा (यदि कोई मौजूद है)। व्यूहोल्डर तकनीक अन्य प्रकार की समस्याओं को हल करने के लिए मौजूद है: आपको वास्तव में जो चाहिए उससे अधिक दृश्य न बनाएं (इस प्रकार, यह अत्यधिक दृश्य inflations से बचाता है, जो महंगा है)। – Cristian
@ क्रिस्टियन, धन्यवाद। मैंने कक्षा को 'SimpleCursorAdapter' तक विस्तारित करने के लिए 'newView' और 'bindView' का उपयोग किया। नीचे दिया गया कोड देखें। –