2013-01-02 38 views
1

我有兩個RelativeLayouts,我以編程方式向ArrayList中添加TextViews,目的是基於單擊的TextView,我可以索引回原始ArrayList。對於我的簡單代碼示例,我擅自將這些項目拆分爲兩個RelativeLayouts。在循環onClick意外的行爲中添加多個TextViews

當單擊第一個TextView時,代碼將按預期工作。對話框顯示正確的單詞和索引。但是,關閉對話框後,如果單擊不同的TextView,則不會更新單詞和索引(代碼示例中的testWord和testID),並且只顯示第一個TextView的信息。看起來onClick只在第一次點擊時被調用。

下面是一個例子Java類(我對任何格式錯誤道歉,這是我第一次在這裏張貼):

package com.test.test; 
import java.util.ArrayList; 
import java.util.Scanner; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.text.method.ScrollingMovementMethod; 
import android.util.TypedValue; 
import android.view.Gravity; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class AndTestActivity extends Activity { 
    private final int DIALOG_CASE_ITEM_SELECT=0,CURR_ID=128,P_ID=256,P_HR=512,C_HR=1024; 
    private final String TEST="This is a test. Only a test."; 
    private int numPast,testID; 
    private String testWord; 
    private ArrayList<String> test; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Scanner s=new Scanner(TEST); 
     test=new ArrayList<String>(); 
     while(s.hasNext()){ 
      test.add(s.next()); 
     } 
     int currID=CURR_ID,pID=P_ID,pHrID=P_HR,cHrID=C_HR,tempLen=3;   
     numPast=0; 
     RelativeLayout rlP=(RelativeLayout)findViewById(R.id.caseItemListPastLayout); 
     RelativeLayout rlC=(RelativeLayout)findViewById(R.id.caseItemListCurrLayout); 
     for(int x=0;x<test.size();x++){         
      if(x>tempLen){      
       addToRL(test.get(x),true,rlC,currID,cHrID); 
       currID++; 
       cHrID++; 
      }else{    
       numPast++; 
        addToRL(test.get(x),false,rlP,pID,pHrID); 
       pID++; 
       pHrID++; 
      }  
     }  
    } 

    private void addToRL(String sb, boolean current,RelativeLayout rl,int id,int hrID){ 
     TextView citv=new TextView(this); 
     citv.setText((CharSequence)sb); 
     citv.setMovementMethod(new ScrollingMovementMethod()); 
     citv.setTextSize(15); 
     citv.setTextColor(Color.BLACK); 
     citv.setGravity(Gravity.CENTER_HORIZONTAL); 
     if(current){ 
      citv.setBackgroundColor(Color.RED);     
     }else{ 
      citv.setBackgroundColor(Color.WHITE); 
     } 
     citv.setPadding(20, 10, 20, 10); 
     citv.setWidth((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 360, getResources().getDisplayMetrics())); 
     citv.setId(id); 
     RelativeLayout.LayoutParams cilp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     if(current&&id!=CURR_ID)cilp.addRule(RelativeLayout.BELOW, hrID-1); 
     else{ 
      if(id!=P_ID)cilp.addRule(RelativeLayout.BELOW, hrID-1); 
     } 
     citv.setLayoutParams(cilp); 
     citv.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View v){ 
       TextView tv0=(TextView)v; 
       int tvID=tv0.getId()-P_ID; 
       if(tvID<0){ 
        tvID=(tv0.getId()-CURR_ID)+numPast; 
       } 

       testID=tvID; 
       testWord=test.get(testID); 

       showDialog(DIALOG_CASE_ITEM_SELECT); 
      } 
     }); 
     rl.addView(citv);       
     View hr=new View(this); 
     hr.setBackgroundColor(getResources().getColor(R.color.background)); 
     RelativeLayout.LayoutParams hrlp=new  RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 5); 
     hrlp.addRule(RelativeLayout.BELOW,id);   
     hr.setLayoutParams(hrlp); 
     hr.setId(hrID); 
     rl.addView(hr); 
    } 

    protected Dialog onCreateDialog(int id) { 
     Dialog dialog; 
     AlertDialog.Builder builder; 
     switch(id){ 
     case DIALOG_CASE_ITEM_SELECT: 
      builder = new AlertDialog.Builder(this); 
      builder.setMessage("word: "+testWord+" index: "+testID) 
        .setCancelable(true) 
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
      dialog = builder.create(); 
      break; 
     default: 
      dialog = null; 
     } 
     return dialog; 
    } 
} 

下面是佈局相關的XML(沒有什麼特別,但我想我d包括它所以有一個完全工作的例子):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:layout_height="wrap_content" android:layout_width="wrap_content"  android:orientation="vertical" 
    android:background="@color/background" 
    android:screenOrientation="portrait">  
    <ScrollView 
     android:id="@+id/caseItemCurrScroll" 
     android:fillViewport="true" 
     android:padding="10dp" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"> 
     <RelativeLayout android:id="@+id/caseItemListCurrLayout" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content"> 
     </RelativeLayout> 
    </ScrollView> 
    <View 
     android:id="@+id/caseItemScrollSpacer" 
     android:layout_width="fill_parent" 
     android:layout_below="@id/caseItemCurrScroll" 
     android:layout_height="5dp" 
     android:background="@color/background"/> 
    <ScrollView 
     android:id="@+id/caseItemPastScroll" 
     android:fillViewport="true" 
     android:padding="10dp" 
     android:layout_below="@id/caseItemScrollSpacer" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"> 
     <RelativeLayout android:id="@+id/caseItemListPastLayout" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content"> 
     </RelativeLayout> 
    </ScrollView> 
</RelativeLayout> 

這應該是一個完全的「工作」例如,增加的

<color name="background">#3E3E3E</color> 

在strings.xml中

+0

「我對任何格式錯誤表示歉意,這是我第一次在這裏發帖」你的代碼格式很好。如果你想在句子的「中間」使用代碼,使用'。 – Sam

回答

0

onCreateDialog()只調用一次,而每次打開對話框時調用onPrepareDialog()

覆蓋onPrepareDialog()並致電((AlertDialog) dialog).setMessage()在這裏。

+0

謝謝!我非常專注於TextView的值,我沒有想到對話框。 – vassago