循环onClick意外行为中添加了多个TextView

时间:2013-01-02 21:02:45

标签: android onclick textview

我有两个RelativeLayouts,我正在以编程方式从ArrayList添加TextViews,其意图是,基于单击的TextView,我可以索引回原始的ArrayList。对于我的琐碎代码示例,我随意将项目拆分为两个RelativeLayouts。

单击第一个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(没什么特别的,但我想我会把它包含在内,所以有一个完整的例子):

<?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>
中的

1 个答案:

答案 0 :(得分:0)

onCreateDialog()仅调用一次,而每次Dialog打开时调用onPrepareDialog()

覆盖onPrepareDialog()并在此处致电((AlertDialog) dialog).setMessage()