5

बनाना मैं क्लिक पर पंक्ति वस्तुओं के रंगों को बदलने की सुविधा के लिए एक बहुत ही सरल कर्सर कस्टम कर्सर एडाप्टर बनाना चाहता हूं। निम्नलिखित कोडकस्टम सरल कर्सर एडाप्टर

private static int save = -1; 

public void onListItemClick(ListView parent, View v, int position, long id) { 

    parent.getChildAt(position).setBackgroundColor(Color.BLUE); 

    if (save != -1 && save != position){ 
     parent.getChildAt(save).setBackgroundColor(Color.BLACK); 
    } 

    save = position;     

} 

का उपयोग करते हुए मैं इस धागे से कोड मिला https://stackoverflow.com/a/7649880/498449

मैं एक साधारण कर्सर अनुकूलक इस्तेमाल किया होगा और onClick में कोड रखा है, लेकिन ListFragment में डिफ़ॉल्ट सूची दृश्य पुनः उपयोग कर लेता है क्योंकि, जैसे ही आप एकाधिक दृश्यों को स्क्रॉल करते हैं उन्हें हाइलाइट किया जाता है। आईआरसी पर बात करते हुए, यह सुझाव दिया गया था कि मैं एक कस्टम कर्सर एडाप्टर बनाउंगा। हालांकि, मुझे यह नहीं लगता कि यह कैसे करना है, और जहां उपरोक्त कोड स्निपेट फिट होगा, वहां सबसे अच्छा अभ्यास नहीं लग रहा है। मदद की बहुत सराहना कर सकता है।

public class AreaCursorAdapter extends CursorAdapter { 
    private Context context; 


    public AreaCursorAdapter(Context context, Cursor c) { 
     super(context, c); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
public void bindView(View view, Context context, Cursor cursor) { 
    TextView list_item = (TextView)view.findViewById(android.R.id.text1); 
    list_item.setText(cursor.getString(cursor.getColumnIndex(INDICATOR_NAME))); 

} 

@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_1, parent, false); 
    bindView(v, context, cursor); 
    return v; 
} 

} 

मैंने कर्सर एडाप्टर को ऑनलाइन प्राप्त कुछ कोड के साथ अद्यतन किया है। हालांकि, मुझे दो समस्याएं हैं। 1. मैं कर्सर लोडर का उपयोग कर रहा हूं, इसलिए मेरे पास कन्स्ट्रक्टर में पास करने के लिए "कर्सर" ऑब्जेक्ट नहीं है। 2. मुझे एक्लिप्स से चेतावनी मिल रही है कि निर्माता को बहिष्कृत कर दिया गया है।

+0

newview में आप LayoutInflater.inflate के साथ एक दृश्य बनाने के लिए, bindView में आप अपने चयन पूर्णांक के अनुसार सामग्री और पृष्ठभूमि निर्धारित करते हैं, में onclick आप सिर्फ चयन int को बदलें और एडाप्टर को बताएं कि इसकी सामग्री में कोई बदलाव आया है। – zapl

+0

@zapl क्या आप एक कोड स्निपेट दिखा सकते हैं। इसके अलावा, मैं कर्सर लोडर का उपयोग करके इसे कार्यान्वित कर रहा हूं, इसलिए मेरे पास कन्स्ट्रक्टर को पास करने के लिए "कर्सर" ऑब्जेक्ट नहीं है। –

+0

मेरे पास अभी एक स्निपेट नहीं है :(google शायद है। लोडर के साथ 'adapter.swapCursor()' 'onLoadFinished 'में है। – zapl

उत्तर

18

आप इस तरह के बारे में यह करने के लिए सक्षम होना चाहिए:

class YourListFragment extends ListFragmentOrSomethingElse { 
    private AreaCursorAdapter mAdapter; 

    @Override  
    public void onCreate() { 
     mAdapter = new AreaCursorAdapter(this, null); 
     setListAdapter(mAdapter); 
    } 

    @Override 
    public void onListItemClick(ListView parent, View v, int position, long id) { 
     mAdapter.setSelectedPosition(position); 
    } 

    @Override 
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { 
     mAdapter.swapCursor(cursor); 
     // should reset that here maybe 
     mAdapter.setSelectedPosition(-1); 
    } 
} 

public class AreaCursorAdapter extends CursorAdapter { 
    private Context context; 
    private int mSelectedPosition; 
    LayoutInflater mInflater; 

    public AreaCursorAdapter(Context context, Cursor c) { 
     // that constructor should be used with loaders. 
     super(context, c, 0); 
     mInflater = LayoutInflater.from(context); 
    } 

    public void setSelectedPosition(int position) { 
     mSelectedPosition = position; 
     // something has changed. 
     notifyDataSetChanged(); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     TextView list_item = (TextView)view.findViewById(android.R.id.text1); 
     list_item.setText(cursor.getString(cursor.getColumnIndex(INDICATOR_NAME))); 
     int position = cursor.getPosition(); // that should be the same position 
     if (mSelectedPosition == position) { 
      view.setBackgroundColor(Color.RED); 
     } else { 
      view.setBackgroundColor(Color.WHITE); 
     } 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     View v = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false); 
     // edit: no need to call bindView here. That's done automatically 
     return v; 
    } 

} 
+0

आप एक जीवन बचतकर्ता हैं! मैं सक्षम था इसे उपरोक्त कोड के साथ काम करने के लिए प्राप्त करें। –

+0

मैं प्रदाता सूची सूची में प्रदाता से पूछताछ करता हूं? – nia

+0

@nia वह 'कर्सर लोडर' का उपयोग करेगा। क्वेरी 'onrereLoader'' के अंदर है (नहीं उपरोक्त कोड में दिखाया गया है) http://helpmeco.de/2012/3/using-an-android-cursor-loader-with-a-content-provider उदाहरण के लिए – zapl

संबंधित मुद्दे