安卓:有另一种方式,从一个字符串数组显示数据? [英] Android: Is there another way to display data from a string array?

查看:75
本文介绍了安卓:有另一种方式,从一个字符串数组显示数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示的步骤配方,配方被存储在一个字符串数组。

I am trying to display the steps to a recipe, the recipe is stored in a string array.

林试图获得每个步骤将被插入到文本视图,并具有以下code:

Im trying to get each step to be inserted into a text view, and have the following code:

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]);
    }
  }
}

我也想使其动态的,所以没有人知道我该怎么做呢? (例如,取决于什么用户点击该配方所示)。

I am also wanting to make it dynamic so does anyone know how I can do that? (e.g. depending on what the user clicked that recipe is shown).

感谢

推荐答案

这可能是更容易实际使用你的 ListActivity 的ListView ...你甚至可以显示出另一个你的食谱的ListView ,因为这无疑将是更直观的用户...

It might be easier to actually use your ListActivity's ListView... You can even show your recipe in another ListView, as it will undoubtedly be more intuitive for the user...

一个简单的code,它这将一定程度走在了下面几行:

A simple code that does this would go somewhat in the following lines:

文件1:RecipeListActivity.java(显示配方列表)

File 1: RecipeListActivity.java (shows list of recipes)

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(示出在列表配方)

File 2: RecipeActivity.java (shows a recipe in a list)

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


另外,这里是一个code复合材料其下面的​​食谱列表一个TextView为了显示配方内容,而无需打开另一个活动:


Alternately, here is a code which composites a TextView below the recipes list in order to show the recipe content without opening another Activity:

文件:RecipeListActivity.java

File: 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中

File: 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>

这篇关于安卓:有另一种方式,从一个字符串数组显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆