以下类用于显示xml文件中包含的一组字符串。在onCreate方法中,从资源文件中提取字符串数组。这些字符串是一组可笑的笑话,它们被添加到Joke对象(m_arrJokeList)的ArrayList中,从字符串构造。
在onCreate方法中调用的addJoke方法旨在将这些笑话作为文本显示在滚动视图中。但是,这些似乎都不适用于我的设备或模拟器,因此代码肯定存在问题。我想知道如何解决这个问题以及如何使用这些视图的一些技巧。
以下是代码未完全实施。
package edu.calpoly.android.lab2;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
public class SimpleJokeList extends Activity {
// Contains the list Jokes the Activity will present to the user
protected ArrayList<Joke> m_arrJokeList;
// LinearLayout used for maintaining a list of Views that each display Jokes
protected LinearLayout m_vwJokeLayout;
// EditText used for entering text for a new Joke to be added to m_arrJokeList.
protected EditText m_vwJokeEditText;
// Button used for creating and adding a new Joke to m_arrJokeList using the
// text entered in m_vwJokeEditText.
protected Button m_vwJokeButton;
// Background Color values used for alternating between light and dark rows
// of Jokes.
protected int m_nDarkColor;
protected int m_nLightColor;
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
initLayout();
Resources localRsrc;
localRsrc = this.getResources();
ArrayList<Joke> jokeList = new ArrayList<Joke>();
String[] jokeStrings = localRsrc.getStringArray(R.array.jokeList);
int size = jokeStrings.length;
Joke tempJoke = new Joke();
for(int i=0;i < size;i++)
{
tempJoke.setJoke(jokeStrings[i]);
jokeList.add(tempJoke);
addJoke(tempJoke);
}
}
// Method used to encapsulate the code that initializes and sets the Layout
// for this Activity.
protected void initLayout() {
// TODO
//LinearLayout rootLayout;
m_arrJokeList = new ArrayList<Joke>();
m_vwJokeLayout = new LinearLayout(this); // why pass "this"
m_vwJokeLayout.setOrientation(LinearLayout.VERTICAL);
ScrollView extendedView = new ScrollView(this.getApplicationContext());
extendedView.addView(m_vwJokeLayout);
setContentView(extendedView);
}
// Method used to encapsulate the code that initializes and sets the Event
// Listeners which will respond to requests to "Add" a new Joke to the list.
protected void initAddJokeListeners() {
// TODO
}
// Method used for encapsulating the logic necessary to properly initialize
// a new joke, add it to m_arrJokeList, and display it on screen.
// @param strJoke
// A string containing the text of the Joke to add.
protected void addJoke(Joke jk) {
m_arrJokeList.add(jk);
TextView textJoke = new TextView(this);
textJoke.setText(jk.getJoke());
m_vwJokeLayout.addView(textJoke);
}
}
答案 0 :(得分:0)
把
Joke tempJoke = new Joke();
in for loop
for(int i=0;i < size;i++)
{
Joke tempJoke = new Joke();
tempJoke.setJoke(jokeStrings[i]);
jokeList.add(tempJoke);
addJoke(tempJoke);
}
然后试试吧。让我知道会发生什么。