1

我有两个RelativeLayouts,我以编程方式将TextViews 添加到ArrayList 中,目的是根据单击的TextView,我可以索引回原始ArrayList。对于我的琐碎代码示例,我将项目任意拆分为两个相对布局。

单击第一个 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>

在 strings.xml 中

4

1 回答 1

0

onCreateDialog()只调用一次,而onPrepareDialog()每次对话框打开时调用。

覆盖onPrepareDialog()并调用((AlertDialog) dialog).setMessage()这里。

于 2013-01-02T21:23:46.327 回答