6

एक संवाद बंद होने पर मैं एक टुकड़े में एक सूची दृश्य को ताज़ा करना चाहता हूं। अब तक रीफ्रेश केवल तब काम करता है जब मैं ऐप को पुनरारंभ करता हूं और मुझे वास्तव में क्यों नहीं पता है।बेसएडाप्टर से भरे टुकड़े में ListView को रीफ्रेश कैसे करें?

यहाँ वर्ग हैं:

यह उस पर ListView साथ टुकड़ा है, कि मैं ताज़ा करने के लिए, जब संवाद बंद कर दिया है चाहता हूँ।

public class RegisterListFragment extends Fragment { 


    public static final String TAG = "RegisterListFragment"; 

    RegisterListAdapter adapter; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     setRetainInstance(true); 
     // TODO Auto-generated method stub 

     View view = inflater.inflate(R.layout.act_select_list_fragment, container, false); 


     ListView list = (ListView) view.findViewById(R.id.register_list); 
     list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
            long arg3) { 
       Intent i = new Intent(getActivity(), com.example.smartkasse.Act_Register.MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       TextView selectedRegisterTextView = (TextView) arg1.findViewById(R.id.title); 
       String selectedRegisterName = (String) selectedRegisterTextView.getText(); 
       i.putExtra("selectedRegisterName", selectedRegisterName); 
       startActivity(i); 
       getActivity().finish(); 
      } 
     }); 
     adapter = new RegisterListAdapter(getActivity()); 
     list.setAdapter(adapter); 

     return view; 
    } 


    public void refreshData() { 
     adapter.notifyDataSetChanged(); 
    } 
} 

और यह एडाप्टर के साथ मैं सूची को भरने है:

public class RegisterListAdapter extends BaseAdapter { 
    View view = null; 

    Database db = new Database(mContext); 
    data=db.getAlltitles(); 
    if(convertView==null) 

    { 

     view = (View) inflater.inflate(R.layout.act__select_listview, null); 

     TextView title = (TextView) view.findViewById(R.id.title); 
     TextView artist = (TextView) view.findViewById(R.id.artist); 
     TextView duration = (TextView) view.findViewById(R.id.duration); 
     ImageView thumb_image = (ImageView) view.findViewById(R.id.list_image); 

     title.setText(data.get(position)); 
     //artist.setText(data.get(position)); 
     //duration.setText(data.get(position)); 
     //thumb_image.setImageResource(R.drawable.ic_launcher); 

     return view; 
    } 

    else 

    { 

     TextView title = (TextView) convertView.findViewById(R.id.title); 
     TextView artist = (TextView) convertView.findViewById(R.id.artist); 
     TextView duration = (TextView) convertView.findViewById(R.id.duration); 
     ImageView thumb_image = (ImageView) convertView.findViewById(R.id.list_image); 

     title.setText(data.get(position)); 
     // artist.setText(data.get(position)); 
     // duration.setText(data.get(position)); 
     //thumb_image.setImageResource(R.drawable.ic_launcher); 

     return convertView; 
    } 
} 

और यह मुख्य है कि सब कुछ एक साथ रखती है:

जब संवाद की positv बटन, तो दबाया जाता है डाटाबेस में नया डेटा जोड़ा गया है कि मैं चाहता हूं कि

सूची में देखने के लिए देखें " हिंद "संवाद। मैंने बहुत कोशिश की, लेकिन मुझे वास्तव में यह नहीं मिला। क्या आपसे मुझे मदद मिल सकती है।

धन्यवाद :)

+0

यह यूआई सूत्र में डेटा लोड करने के अच्छा विचार नहीं है। CursorLoader (http://developer.android.com/reference/android/content/CursorLoader.html) या कस्टम AsyncTaskLoader पर एक नज़र डालें। अपनी समस्या को हल करने के लिए आपको अपने एडाप्टर का नया उदाहरण बनाना होगा और इसे सूची दृश्य – JustAnotherCoder

उत्तर

4

आप ListView ताज़ा करने या किसी अन्य एडाप्टर के साथ स्मृति में डेटा रखने के लिए इस

public void refresh(){ 
    adapter = new RegisterListAdapter(getActivity()); 
    list.setAdapter(adapter); 
} 

कर सकते हैं।

संपादित: माफ करना, मैं अच्छी तरह से अपने प्रश्न पहली बार पढ़ा नहीं था, तो आप अपने अनुकूलक में यह लिख सकते हैं:

public void addNewRow(String row){ 
    this.data.add(row); 
    this.notifyDataSetChanged(); 
} 
+0

हाँ देखने के लिए सेट करें, जो मेरे लिए काम करता है। लेकिन मुझे लगता है कि ऐसा करने के लिए यह "सर्वोत्तम अभ्यास" नहीं है। क्या आप इस समस्या को ठीक करने के बारे में जानते हैं? क्योंकि इस तरह से मैं getActivity.findViewbyId (R.id.list) को भी कॉल कर सकता हूं और फिर एक नया एडाप्टर जोड़ सकता हूं। मुझे लगता है कि यह ऐसा करने का तरीका नहीं है। – wirthual

0

प्रयास करें ListView.invalidateViews() ताज़ा सामग्री

+0

पर सेट करना होगा, मैंने adapter.notifyDataSetChanged() के बजाय list.invalidateViews को आजमाया है। लेकिन यह भी काम नहीं किया-.- – wirthual

+0

यह सुनिश्चित करें कि आप कॉल करने से पहले डेटा बदल गए हैं। डेटाबेस.getAlltitles() - वापस आ गया परिणाम परिणाम बदल गया? – matreshkin

+0

अजीब बात यह है कि जब पहली वस्तु जोड़ दी जाती है और मैं गतिविधि बंद करता हूं और इसे फिर से शुरू करता हूं, तो यह काम करता है। मैं वास्तव में उलझन में हूँ। – wirthual

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