2013-02-13 46 views
5

अभी मैं अपने सूची दृश्य को अपडेट करने के लिए setAdapter का उपयोग करता हूं, लेकिन मुझे लगता है कि notifiyDatasetChanged() का उपयोग करने का उचित तरीका है और मैं इसे अपने मुख्य वर्ग (यह एडाप्टर में) में काम करने के लिए नहीं मिल सकता है। यहाँ त्रुटि है:मैं ListAdapter के साथ काम करने के लिएडेटाडेटेड() को सूचित कैसे करूं?

विधि notifyDatasetChanged() प्रकार ListAdapter

मैं वहाँ ऐसा करने का एक बेहतर तरीका है अनुमान लगा रहा हूँ के लिए अपरिभाषित है - किसी को भी सही दिशा में मुझे बात कर सकते हैं? है एडाप्टर

public class ScoreList extends SherlockFragmentActivity { 

    private ListView listViewScore; 

    static List<Score> listScore = new ArrayList<Score>(); 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.score_list); 
     ctx = this; 
     listScore = dbh.getAllScores(); 

     listViewScore = (ListView) findViewById(R.id.score_list); 

     listViewScore.setAdapter(new ScoreListAdapter(ctx, 
       R.layout.score_row_item, listScore)); 
     listViewScore.getAdapter().notifyDatasetChanged(); //this is where I get the error 
    } 
} 

यहाँ:

यहाँ मेरी कोड के प्रासंगिक भागों है

public class ScoreListAdapter extends ArrayAdapter<Score> { 
    private int resource; 
    private LayoutInflater inflater; 

    public ScoreListAdapter(Context ctx, int resourceId, List<Score> objects) { 
     super(ctx, resourceId, objects); 
     resource = resourceId; 
     inflater = LayoutInflater.from(ctx); 
     //context = ctx; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     convertView = (LinearLayout) inflater.inflate(resource, null); 

     Score score = getItem(position); 

     TextView txtName = (TextView) convertView.findViewById(R.id.name); 
     txtName.setText(score.getName()); 

     TextView txtScoreChange = (TextView) convertView 
       .findViewById(R.id.scoreChange); 
     int scoreChange = Integer.parseInt(score.getScoreChange()); 
     if (scoreChange > 0) 
      txtScoreChange.setText("+" + scoreChange); 
     else if (scoreChange < 0) 
      txtScoreChange.setText("" + scoreChange); 
     else 
      txtScoreChange.setText(""); 

     TextView txtScoreTotal = (TextView) convertView 
       .findViewById(R.id.scoreTotal); 
     txtScoreTotal.setText(score.getScoreTotal()); 

     final LinearLayout currentRow = (LinearLayout) convertView 
       .findViewById(R.id.scoreRowLayout);     

     notifyDataSetChanged(); 
     return convertView; 
    } 
} 
+0

यह 'सूचित करेंडेटाट चेंज()' - नोटिस पूंजी एस। आपके पास दिखाए गए लाइन पर आपके पास एक लोअर केस है। – Squonk

+0

सबसे पहले आपको सूचित करने के उपयोग की आवश्यकता हैडेटाडेटेड ... संलग्न पर्यवेक्षकों को सूचित करता है कि अंतर्निहित डेटा बदल दिया गया है और डेटा सेट को प्रतिबिंबित करने वाले किसी भी दृश्य को स्वयं को ताज़ा करना चाहिए। लागू करने से पहले इसे समझें ... अधिसूचना डेटासेट चेंज() का उपयोग तब किया जाएगा जब आपके एडाप्टर डेटा में डेटा अपडेट किया गया हो या बस बदला गया .. यह एडाप्टर से जुड़ा संग्रह बदल गया है। सृजन में इसे कॉल करने की कोई आवश्यकता नहीं – Pragnani

+0

धन्यवाद, मैंने बहुत सारे कोड काट दिया है, इसलिए मैं इसे सृजन पर नहीं बुला रहा था, यह सिर्फ उस तरह से देखा गया। – GrilledCheese

उत्तर

3

अपने कस्टम अनुकूलक का एक उदाहरण बना है, तो आप इसे कहीं भी उपयोग कर सकते हैं आप की तरह ...

public class ScoreList extends SherlockFragmentActivity { 

private ListView listViewScore; 

private ScoreListAdapter adapter; 

static List<Score> listScore = new ArrayList<Score>(); 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.score_list); 
    ctx = this; 
    listScore = dbh.getAllScores(); 

    listViewScore = (ListView) findViewById(R.id.score_list); 

    adapter = new ScoreListAdapter(ctx, R.layout.score_row_item, listScore); 
    listViewScore.setAdapter(adapter); 
    adapter.notifyDatasetChanged(); 
} 
} 

वैसे, अगर आपके listScore सरणी पहले से ही भरी हुई है, तो आप

उपयोग करने के लिए की जरूरत नहीं है
adapter.notifyDatasetChanged(); 
+0

धन्यवाद, यह एक बहुत ही सरल स्पष्टीकरण था और यह पूरी तरह से काम करता है (सिवाय इसके कि मुझे अधिसूचना में सेट को कैपिटल करना थाडेटाट चेंज(); जैसा कि किसी और ने नोट किया) – GrilledCheese

+0

आह हाँ, मैंने इसे अभी अनदेखा किया है :) लेकिन जैसा कि मैंने कहा, यह आवश्यक नहीं है यदि आपकी सूची पहले ही लोड हो चुकी है तो इसे अपनी स्थिति में उपयोग करें। – yahya

1

न फोन notifyDataSetChanged(); सृजन के दौरान विधि।

केवल यह है जब आपके listViewScore की सामग्री बदलती .. फोन और उस समय

पर इसका इस्तेमाल

((ScoreListAdapter)listView.getAdapter()).notifyDataSetChanged(); 

साथ

listView.getAdapter().notifyDatasetChanged(); 

की जगह और जादू देखने के लिए ...

धन्यवाद।