0

我正在制作一个应用程序,用户可以在其中看到 ListView 中的单词列表,并且可以单击它们进行翻译。我想要的是在列表中看到英文单词,并且警报对话框会显示一些乱码(我稍后会将乱码更改为西班牙语定义)。

现在,我按顺序排列了两个字符串,因此当您单击列表中的第一个单词时,警报将包含第二组字符串的第一个单词。当您单击列表中的第二个单词时,我希望第二个字符串的第二个单词显示在警报中。当您单击列表中的第三个单词时,我希望第二个字符串的第三个单词显示在警报中。我唯一的问题是,警报不会显示第二个字符串中的相应项目。警报仅显示在列表中单击的相同单词。

我意识到我可以为每个单词做一个活动,但这似乎很麻烦,所以我决定使用警报对话框。

有人可以更新我的代码以向我展示如何使第二个字符串中的单词出现在警报中吗?

这是我的代码:

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();


 }


}
4

1 回答 1

1

你已经完成了几乎你需要的东西。

采用

.setMessage("" + spanishBasic[position])

代替

.setMessage("" + getListView().getItemAtPosition(position))

我的代码在这里

 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. */


   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setListAdapter(new ArrayAdapter<String>(this,
               android.R.layout.simple_list_item_1, basicWords));
       getListView().setTextFilterEnabled(true);
   }

 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("" + spanishBasic[position])
   .setPositiveButton("Back to List",
     new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {}}
     )
   .show();


 }


}

祝你好运,祝你有美好的一天。。

于 2012-07-12T03:28:29.153 回答