0
我正在製作一個應用程序,用戶在ListView中看到一個單詞列表,並可以單擊它們進行翻譯。我想要的是在列表中看到英文單詞,並且警告對話框會引起一些亂碼(稍後我會將亂碼變爲西班牙語定義)。如何在Java中按位置顯示警報對話框中的項目?
現在,我有兩個字符串按順序排列,因此當您單擊列表中的第一個單詞時,警報將包含第二組字符串的第一個單詞。當您單擊列表中的第二個單詞時,我希望第二個字符串的第二個單詞出現在警報中。當您單擊列表中的第三個單詞時,我希望第二個字符串的第三個單詞出現在警報中。我唯一的問題是,警報不會顯示第二個字符串中的相應項目。警報僅顯示列表中單擊的單詞。
我意識到我可以爲每個單詞做一個活動,但這看起來很麻煩,所以我決定使用一個警告對話框。
有人可以更新我的代碼,告訴我如何使第二個字符串中的單詞出現在警報中嗎?
這是我的代碼:
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SpanishActivity extends ListActivity {
static final String[] basicWords = new String[] {
"Hello", "Goodbye", "Yes", "No",
"Why", "Where", "When", "What ", "Who", "How", "Absolutely", "I", "You",
"He", "She", "That", "Black", "White", "Red","Orange", "Blue", "Green",
"Yellow", "Purple", "Later" , "Now", "Today", "Tomorrow", "Left", "Right",
"Hand", "Mouth", "Tongue", "Nose", "Ear", "Eyes", "Leg", "Dog", "Cat",
"Elephant", "Snake", "Camel", "Pen", "Pencil", "Book", "Paper", "Hot",
"Cold", "Airplane", "Car", "Raining", "Sunny", "Cloudy", "Water", "Please",
"Help", "Work", "English", "America", "England", "Funny", "Thanks",
"Good", "Bad", "Happy", "Sad",
};
String[] spanishBasic = new String[]{
"afsdfb", "qerg", "nt4th", "erhn",
"ehrethn", "rth", "Kub", "ygfd ", "cvb", "ytrfvh", "jhgv", "Mvbay", "hgfv",
"gv", "cvbnhg", "gfd", "hgf", "ytr", "hgf","wthw", "wetergh", "wewrth",
"weth", "erg", "wrgwr" , "dfghj", "xdhtcjfy", "cfj", "zsrxdtcf", "oiuy",
"rxjtdcfky", "n", "dfgh", "sdfgh", "fgbh", "nkhn", "ayr", "ota", "dfgh",
"Hafghi", "ghjnp", "Ogtyh", "dfg", "fghn", "fghjnm", "ghn", "hjk",
"xdcfgh", "xcv", "hjdi", "fghish", "fghoop", "Bhjl", "bnani", "cvbn",
"Mghj", "bhjam", "hjayzi", "ghjk", "vhj", "vghj", "cfgh",
"bhj", "bhjra", "ghjshi", "fghhum",
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, basicWords));
getListView().setTextFilterEnabled(true);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
new AlertDialog.Builder(this)
.setTitle("Spanish")
.setMessage("" + getListView().getItemAtPosition(position))
.setPositiveButton("Back to List",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {}}
)
.show();
}
}
感謝這麼畝ch,這有很大幫助,我現在認爲我錯了。 – GurpreetSingh 2012-07-13 01:49:02