2013-03-10 14 views
14

में मेरी TextView कैसे अपडेट करूं मैं अपनी पहली उचित Android एप्लिकेशन का निर्माण करने के टुकड़े का उपयोग करने की कोशिश कर रहा हूँ। मेरे पास एक मुख्य एक्सएमएल है। जिसमें दो लंबवत टुकड़े होते हैं, शीर्ष टुकड़ों में केवल दो टेक्स्ट व्यू होते हैं। इनमें से पहले स्थिर टेक्स्ट होता है और दूसरे में एक मान होता है जिसे मैं अंततः SQL से गतिशील रूप से प्राप्त कर रहा हूं।एंड्रॉयड: मैं एक टुकड़ा

तो मैं यह मेरा MainActivity.java है तो यह खुशी से TextView का मूल्य अपडेट हो जाता है मेरा पहला टुकड़ा में डाल:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    // Set the Text to try this out 
    TextView t = (TextView)findViewById(R.id.viewItem); 
    t.setText("Text to Display"); 
} 

मैं दो टुकड़ा XMLs है और एक जावा प्रत्येक तो मैं रखना चाहते हैं का समर्थन करने के इस क्षेत्र की जावा में सेटिंग जो मुख्य क्रियाशीलता के लिए जावा के बजाए टुकड़े का समर्थन करती है।

public class FragmentMainTop extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState){ 
    // -- inflate the layout for this fragment 
    return inflater.inflate(R.layout.fragmentmt, container,false); 

    // Set the Text to try this out 
    TextView t = (TextView)findViewById(R.id.viewItem); 
    t.setText("Text to Display"); 
} 

लेकिन मैं TextView लाइन पर कोई त्रुटि मिलती है अगर मैं इस कार्य करें:

"विधि findViewById (int) है -:

जब मैं यह टुकड़ा में डाल यह इस तरह दिखता है प्रकार FragmentMainTop "

तो क्यों यह नहीं इस विधि को पता है के लिए अपरिभाषित? मेरे पास सीटीएल/शिफ्ट/ओ है इसलिए मुझे पता है कि मेरे पास सभी सही आयात हैं। मैं MainActivity में अपने सभी कोड डाल खत्म करने के लिए मैं सभी कोड को दोहराने के लिए होगा अगर मैं फिर एक और गतिविधि में टुकड़ा का उपयोग करना चाहते क्योंकि नहीं करना चाहती।

उत्तर

45

तुम इतनी है कि आप अपने findViewById() विधि का उपयोग कर सकते एक चर करने के लिए फुलाया टुकड़ा लेआउट सौंपने होंगे। फिर आप इसे अपने विगेट्स को अपडेट करने के बाद इसे वापस कर देते हैं।

@Override 
public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState){ 
    // -- inflate the layout for this fragment 
    View myInflatedView = inflater.inflate(R.layout.fragmentmt, container,false); 

    // Set the Text to try this out 
    TextView t = (TextView) myInflatedView.findViewById(R.id.viewItem); 
    t.setText("Text to Display"); 

    return myInflatedView; 
} 

आम तौर पर, जब आप एक Activity भीतर findViewById() कहते हैं, आप Activity.findViewById() लागू कर रहे हैं। अपने Fragment कक्षा के भीतर एक ही कॉल का प्रयास करने में विफल रहेगा क्योंकि ऐसी Fragment.findViewById() विधि नहीं है। इसलिए, आपको अपने विगेट्स के संदर्भ प्राप्त करने के लिए अपने फुले हुए दृश्य के साथ View.findViewById() का उपयोग करना होगा।

+0

जब मुझे मिलता है तो खंडों को देखकर वापस लौटाया जाता है। FragAtivity()। लेकिन विचारों को गतिशील रूप से अद्यतन करना अभी भी काम नहीं करता है –

2

मैं एक विशेषज्ञ नहीं हूँ, लेकिन मैं एक विधि के भीतर है कि वापसी कथन कुछ देता है और इस बयान के बाद अगले लाइनों पहुंचा नहीं जा सकता कहेंगे: हे

तो आदेश को बदलने की कोशिश! :)

0

मैं एक TextView अद्यतन करने के लिए मेरी गतिविधि से इस कोड का उपयोग कर रहा एक्सएमएल टैग में जोड़ा। टेक्स्टव्यू कक्षा स्तर की वस्तुएं हैं। Heres कुछ कोड:

<fragment 
    android:id="@+id/fragmentRegister" 
    android:name="com.proshore.loveis.RegisterFragment" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/view1" /> 

यहाँ टुकड़ा कोड सिर्फ श्रेणी स्तर चर दिखा।

public class RegisterFragment extends Fragment implements OnClickListener { 

TextView textViewDOB, textViewLanguage; 

public RegisterFragment() { 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
//eluded methods here 
} 
} 

अब जहां मैं अपने टुकड़ा अद्यतन से गतिविधि:

public class RegisterActivity extends FragmentActivity { 
Button buttonEnter; 
RegisterFragment fragmentRegister; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_register); 
    buttonEnter = (Button) findViewById(R.id.buttonEnter); 
    fragmentRegister = (RegisterFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.fragmentRegister); 
} 

@Override 
protected void onStart() { 
    // TODO Auto-generated method stub 
    super.onStart(); 
    buttonEnter.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Log.d("dontknowitwillowrk", fragmentRegister.textViewLanguage 
        .getText().toString()); 
      fragmentRegister.textViewLanguage.setText("hello mister how do you do"); 


     } 
    }); 
} 
} 

नोट: यकीन नहीं करता है, तो यह एक अच्छा तरीका है।

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