2015-07-20 50 views
-1

我想在我的佈局文件中以編程方式將文本添加到textview和單選按鈕。我已經調試了應用程序,並看到從光標返回正確的值,但我無法將這些值綁定到textview的。在displayquestions功能Android:無法在佈局文件中設置值Programitically

問題

package com.Example.assesmenttool; 


public class Aptitude extends Activity implements OnClickListener { 

    DBHelper helper; 
    SQLiteDatabase db; 
    Button btnBegin,btnnext; 
    int count = 0; 

    //Sub category Buttons 
    Button btnaptitude5,btnaptitude4,btnaptitude3,btnaptitude2,btnaptitude1; 

    String Question_ID,Title,TitleDescription,QuestionText,QuestionTemplate,QuestionImage; 

    //String SubModuleQuestion_ID; 

    TextView tvTitle,tvInstructions,tvQuestionText; 

    RadioGroup rgtemplate4images; 
    ImageView img; 
    RelativeLayout aptitudesubcateg,temp4optimage; 



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

     setContentView(R.layout.aptitude); 

      aptitudesubcateg = (RelativeLayout) findViewById(R.id.aptitudesubcateg); 
      temp4optimage = (RelativeLayout) findViewById(R.id.temp4optimage); 


     aptitudesubcateg.setVisibility(View.GONE); 
     temp4optimage.setVisibility(View.GONE); 


     btnaptitude1 = (Button)findViewById(R.id.btnaptitude1); 
     btnaptitude2 = (Button)findViewById(R.id.btnaptitude2); 
     btnaptitude3 = (Button)findViewById(R.id.btnaptitude3); 
     btnaptitude4 = (Button)findViewById(R.id.btnaptitude4); 
     btnaptitude5 = (Button)findViewById(R.id.btnaptitude5); 

     btnaptitude1.setOnClickListener(this); 
     btnaptitude2.setOnClickListener(this); 
     btnaptitude3.setOnClickListener(this); 
     btnaptitude4.setOnClickListener(this); 
     btnaptitude5.setOnClickListener(this); 

     helper=new DBHelper(this); 
     //Resources res = getResources(); 
     //String[] myBooks = res.getStringArray(R.array.my_questions); 



     btnBegin = (Button)findViewById(R.id.btnBeginAptitude); 

     btnBegin.setOnClickListener(this); 

    } 

    public int getcount(String moduleID, String SubModuleID) 
    { 
     count = 0; 
     String selectQuery = "Select * from TableQuestionMaster where Module_ID = "+moduleID+" AND SubModule_ID ="+SubModuleID; 

      SQLiteDatabase db = helper.getReadableDatabase(); 
      Cursor cursor = db.rawQuery(selectQuery, null); 
      count = cursor.getCount(); 

      Toast.makeText(this, String.valueOf(count) ,Toast.LENGTH_LONG).show(); 

      cursor.close(); 
      db.close(); 

    return count; 
    } 



     public void getAllQuestionsAptitude(String moduleID, String SubModuleID,String SubModuleQuestion_ID){ 



      SQLiteDatabase db = helper.getReadableDatabase(); 
      Cursor c = null; 

      if (count>0) 
      { 

      // SubModuleQuestion_ID = "1"; 
        String selectQuestion = "Select * from TableQuestionMaster where Module_ID = "+moduleID+" AND SubModule_ID ="+SubModuleID+" AND SubModuleQuestion_ID ="+SubModuleQuestion_ID; 
         c = db.rawQuery(selectQuestion, null); 
      if (c.moveToFirst()) { 
       do { 


        Question_ID = c.getString(c.getColumnIndex("Question_ID")); 

        Title = c.getString(c.getColumnIndex("Title")); 


        TitleDescription = c.getString(c.getColumnIndex("TitleDescription")); 


        QuestionText = c.getString(c.getColumnIndex("QuestionText")); 


        QuestionTemplate = c.getString(c.getColumnIndex("QuestionTemplate")); 


        QuestionImage = c.getString(c.getColumnIndex("QuestionImage")); 

        Toast.makeText(this,Question_ID+" "+Title+" "+TitleDescription+" "+QuestionText+" "+QuestionTemplate+" "+QuestionImage,Toast.LENGTH_LONG).show(); 



       } while (c.moveToNext());   
      } 
      } 
      else 

      { 
       Toast.makeText(this,"NO question to display",Toast.LENGTH_LONG).show(); 

      } 


      // closing connection 
      c.close(); 
      db.close(); 

     } 

     public void displayquestions() 
     { 
      Toast.makeText(this,"count on display Q is"+ count,Toast.LENGTH_LONG).show(); 
      if (count >0) 
      { 
       getAllQuestionsAptitude("1","1",String.valueOf(count)); 

        aptitudesubcateg.setVisibility(View.GONE);  
        temp4optimage.setVisibility(View.VISIBLE);  
        tvTitle = (TextView)findViewById(R.id.tvTitleTemplate4optoinsImage); 
        tvInstructions = (TextView)findViewById(R.id.tvInstructionstemplate4); 
        tvQuestionText = (TextView)findViewById(R.id.tvQuestionTextTemplate4Image); 

        img = (ImageView)findViewById(R.id.imageViewtemplate4options); 

        tvTitle.setText(Title); 
        tvInstructions.setText(TitleDescription); 
        tvQuestionText.setText(QuestionText); 

        String imgName = QuestionImage; // specify here your image name fetched from db 
        String uri = "drawable/" + imgName; 
        int icon = getResources().getIdentifier(uri, "drawable", getPackageName()); 
        img.setImageResource(icon); 

        //NextQuestion(); 
      } 
      else 
      { 
       Toast.makeText(this,"NO question to display",Toast.LENGTH_LONG).show(); 
      } 


     } 

     public void NextQuestion() 
     { 
      if (count>0) 
      { 
      count = count - 1; 
      displayquestions(); 
      } 

     } 




    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     if (v==btnBegin) 
     { 
      aptitudesubcateg.setVisibility(View.VISIBLE); 
       temp4optimage.setVisibility(View.GONE); 

     }  
      if (v== btnaptitude1) 
      {    
         getcount("1", "1"); 
         displayquestions();   

      } 

      if (v== btnaptitude2) 
      { 
        getcount("1", "2"); 
        displayquestions(); 
      // getAllQuestionsAptitude("1","2"); 
      } 

      if (v== btnaptitude3) 
      { 
        getcount("1", "3"); 
        displayquestions(); 

       //getAllQuestionsAptitude("1","3"); 
      } 

      if (v== btnaptitude4) 
      { 
      // getAllQuestionsAptitude("1","1"); 
       getcount("1", "4"); 
       displayquestions(); 
      } 

      if (v== btnaptitude5) 
      { 
      // getAllQuestionsAptitude("1","1"); 
       getcount("1", "5"); 
       displayquestions(); 
      }   


     } 



    } 

XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/back" > 

    <TextView 
     android:id="@+id/tvInstructionsAptitude" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/tvQuestionTextTemplate4Image" 
     android:layout_alignParentTop="true" 
     android:layout_alignRight="@+id/TextView01" 
     android:layout_marginTop="93dp" 
     android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam nibh ex, elementum non bibendum ac, consequat a sem. Proin aliquet arcu id lacus interdum, sed fermentum neque sollicitudin. Vestibulum mauris massa, vulputate tincidunt quam vel, egestas tincidunt nisi. Ut ut nulla sagittis, aliquet lectus vitae, pretium nisi. Duis maximus, magna vitae ultricies consectetur, ex felis congue turpis, id scelerisque nisl ex at turpis. Aenean dui sapien, euismod at sem non, rhoncus molestie urna. Mauris condimentum risus felis, nec iaculis orci accumsan nec. Vivamus pharetra ultricies quam a aliquam. Praesent facilisis turpis malesuada massa interdum consequat. In hac habitasse platea dictumst. Integer efficitur nibh risus, ac mattis mi porta ut. Nam leo ex, pulvinar eu orci vel, pretium maximus velit. Pellentesque rhoncus sapien felis, in cursus eros malesuada imperdiet." 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/tvQuestionTextTemplate4Image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="53dp" 
     android:layout_marginTop="40dp" 
     android:text="Instructions" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/TextView01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/tvQuestionTextTemplate4Image" 
     android:layout_alignBottom="@+id/tvQuestionTextTemplate4Image" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="36dp" 
     android:text="Time : 10 Mins" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/btnBeginAptitude" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/tvInstructionsAptitude" 
     android:layout_alignRight="@+id/tvInstructionsAptitude" 
     android:layout_below="@+id/tvInstructionsAptitude" 
     android:layout_marginTop="42dp" 
     android:text="Begin" /> 

    <include 
     android:id="@+id/aptitudesubcateg" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 

     layout="@layout/aptitudesubcategory" /> 

    <include 
     android:id="@+id/temp4optimage" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     layout="@layout/template4optionsimage" /> 

</RelativeLayout> 

成果

As u can the values are coming in toast but are unable to set them

+0

結果是什麼? –

+0

獲取任何錯誤?爲什麼在初始化displayquestions()函數中的textviews而不是onCreate()方法 – Soham

+0

縮小代碼以改變您的問題 – yshahak

回答

1

您可以通過在oncreate()方法而不是displayquestions()下面的行來嘗試一次嗎?

  tvTitle = (TextView)findViewById(R.id.tvTitleTemplate4optoinsImage); 
      tvInstructions = (TextView)findViewById(R.id.tvInstructionstemplate4); 
      tvQuestionText = (TextView)findViewById(R.id.tvQuestionTextTemplate4Image); 

      img = (ImageView)findViewById(R.id.imageViewtemplate4options); 
+0

的佈局中的文字瀏覽我嘗試了其他的東西 –

+0

@tusharnarang你會得到任何錯誤嗎? – Soham

+0

沒有沒有錯誤..我正在檢查我的堆棧跟蹤 –

1

如果您cursor只返回一行(我猜它通過查看query)你並不需要一個循環,通過在一個循環中編寫strings被初始化,但他們正在用新的價值觀寫。

+0

那麼我應該寫什麼,而不是光標移動到下一個? –

+0

是的,你是絕對正確的我的光標僅得到一個行..但三個參數的一個改變按鈕點擊 –

+1

您可以嘗試 '如果(指針!= NULL && cursor.getCount()> 0){// 做你的東西 }' –