【问题标题】:Android: Is there another way to display data from a string array?Android:还有其他方法可以显示字符串数组中的数据吗?
【发布时间】:2012-05-01 18:37:11
【问题描述】:

我正在尝试显示配方的步骤,配方存储在字符串数组中。

我试图将每个步骤插入到文本视图中,并具有以下代码:

public class MethodActivity extends ListActivity{

ListView recipes;
Intent myIntent;
String value;
TextView editvalue;
Intent intent;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  


    setContentView(R.layout.recipemethod);

    editvalue = (TextView)findViewById(R.id.method);
    value = getIntent().getStringExtra("searchValue");
    editvalue.setText(value);

    final String[]  words = getResources().getStringArray(R.array.Lasagna);
    final TextView tw=(TextView)findViewById(R.id.method);
    for(int item = 0;item<words.length;item++){
        tw.setText(words [item]);
    }
  }
}

我也想让它动态化,所以有人知道我该怎么做吗? (例如,根据用户单击的内容显示该食谱)。

谢谢

【问题讨论】:

    标签: android textview arrays


    【解决方案1】:

    实际使用您的ListActivityListView 可能更容易...您甚至可以在另一个ListView 中显示您的食谱,因为它无疑对用户来说更直观...

    执行此操作的简单代码将包含以下几行:

    文件 1:RecipeListActivity.java(显示食谱列表)

    package com.epichorns.testproject;
    
    import java.util.ArrayList;
    
    import android.app.ListActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    
    public class RecipeListActivity extends ListActivity {
    
        final Recipe[] mRecipesArray = {    new Recipe("Lasagna", new String[]{"Some lasagna noodles", "Some tomato sauce"}),
                                            new Recipe("PortalCake", new String[]{"GlaDOS' wit","Is a lie"}),
                                            new Recipe("EarthDestruction", new String[]{"Asteroid", "Kinetic energy"})};
    
    
        public class Recipe{
            public String name;
            public String[] steps;
    
            Recipe(String name, String[] steps){
                this.name = name;
                this.steps = steps;
            }
        }
    
        public ArrayList<String> FetchRecipesRawArray(Recipe[] recipes){
            ArrayList<String> ret = new ArrayList<String>();
            for(int i=0;i<recipes.length;i++){
                ret.add(recipes[i].name);
            }
            return ret;
        }
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);    
    
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FetchRecipesRawArray(mRecipesArray));
            setListAdapter(adapter); 
        }
    
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id){
            Intent intent = new Intent(this, RecipeActivity.class);
            intent.putExtra(RecipeActivity.EXTRA_RECIPEARRAY, mRecipesArray[position].steps);
            startActivity(intent);
    
        }
    }
    

    文件 2:RecipeActivity.java(在列表中显示食谱)

    package com.epichorns.testproject;
    
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    
    
    public class RecipeActivity extends ListActivity {
        final static public String EXTRA_RECIPEARRAY = "recipeArray";
    
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);  
    
            String[] recipeArray = getIntent().getStringArrayExtra(EXTRA_RECIPEARRAY);
            if(recipeArray!=null){
                if(recipeArray.length>0){
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, recipeArray);
                    setListAdapter(adapter);
                }
            }
        }
    }
    

    或者,这里有一段代码,它在食谱列表下方组合了一个 TextView,以便在不打开另一个 Activity 的情况下显示食谱内容:

    文件:RecipeListActivity.java

    package com.epichorns.testproject;
    
    import java.util.ArrayList;
    
    import android.app.ListActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    
    
    public class RecipeListActivity extends ListActivity {
    
        final Recipe[] mRecipesArray = {    new Recipe("Lasagna", new String[]{"Some lasagna noodles", "Some tomato sauce"}),
                                            new Recipe("PortalCake", new String[]{"GlaDOS' wit","Is a lie"}),
                                            new Recipe("EarthDestruction", new String[]{"Asteroid", "Kinetic energy"})};
    
        TextView mTextView_recipeTitle;
        TextView mTextView_recipe;
    
    
        public class Recipe{
            public String name;
            public String[] steps;
    
            Recipe(String name, String[] steps){
                this.name = name;
                this.steps = steps;
            }
        }
    
        public ArrayList<String> FetchRecipesRawArray(Recipe[] recipes){
            ArrayList<String> ret = new ArrayList<String>();
            for(int i=0;i<recipes.length;i++){
                ret.add(recipes[i].name);
            }
            return ret;
        }
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);    
    
            setContentView(R.layout.main);
            mTextView_recipe = (TextView)findViewById(R.id.recipeText);
            mTextView_recipeTitle = (TextView)findViewById(R.id.recipeTitle);
    
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FetchRecipesRawArray(mRecipesArray));
            setListAdapter(adapter); 
        }
    
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id){
    
            mTextView_recipeTitle.setText(mRecipesArray[position].name);
    
            mTextView_recipe.setText("");
    
            String[] recipeSteps = mRecipesArray[position].steps;
            for(int i=0;i<recipeSteps.length;i++){
                mTextView_recipe.append(recipeSteps[i]+"\n");
            }
    
        }
    }
    

    文件:main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:background="#400000FF"
    
            />
    
        <TextView
            android:id="@+id/recipeTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#4000FF00"
             />
    
        <TextView
            android:id="@+id/recipeText"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#40FF0000"
             />
    
    </LinearLayout>
    

    【讨论】:

    • 它不喜欢 setListAdapter(adapter),知道为什么会这样吗?
    • 您是否逐字复制代码?它在我的 HTC Desire 设备上编译得很好(记得在你的 AndroidManifest.xml 中声明你的所有活动)。注意:我还将为您提供一个备用代码库,列表位于顶部,文本视图位于底部,就像您最初设想的那样......请参阅下面我的第一个答案。
    • 只是测试它,但我相信错误仅仅是因为我没有扩展 ListActivity。傻我。会让你知道结果如何:)
    • 谢谢它现在正在工作,我唯一的问题是如果我在字符串 XML 中有一个字符串数组,我如何访问它以重新检索配方?对食谱数组进行硬编码是否安全?没说错。大声笑
    • 好吧,如果您的字符串数组名称是...recipes_array,那么您可以像这样检索它:String[] tmpArray = getResources().getStringArray(R.array.recipes_array); 在硬编码您的食谱时,它当然是可行且安全的。问题是:这样一个所有食谱都被硬编码的应用程序有什么用?从我的角度来看,如果您将您的东西存储在某种数据库中,并带有一个界面以便用户可以向其中添加自定义食谱,那不是更好吗?
    【解决方案2】:

    您将希望使用 tw.append() 而不是 setText,否则您将看到的只是数组中的最后一个字符串。

    让它动态化,我会使用一个片段(如果在较旧的 android 版本上使用 Android 支持库)模型,加载每个数组,在启动之前将字符串数组作为包附加到片段。

    片段啧啧:http://developer.android.com/guide/topics/fundamentals/fragments.html
    支持库:http://developer.android.com/training/basics/fragments/support-lib.html

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 2012-10-08
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 2011-02-05
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      相关资源
      最近更新 更多