我对Lab3 - AdvancedJokeList有个问题。您可以在此处查看实验室Lab3 - AdvancedJokeList
在第3.2部分“使用ListView进行AdvancedJokeList活动”中,我已经将内部的ScrollView和LinearLayout替换为文件"advanced.xml“上的ListView,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="@string/button"
android:id="@+id/addJokeButton" />
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:singleLine="true"
android:gravity="left"
android:id="@+id/newJokeEditText" />
</LinearLayout>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:drawSelectorOnTop="false"
android:id="@+id/jokeListViewGroup">
</ListView>
我修改了文件"AdvancedJokeList.java“,如下所示:
package edu.calpoly.android.lab3;
import java.util.ArrayList;
import edu.calpoly.android.lab3.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
public class AdvancedJokeList extends Activity {
protected LinearLayout root_layout;
protected LinearLayout top_layout;
private int color_counter = 0;
/**
* Contains the name of the Author for the jokes.
*/
protected String m_strAuthorName;
/**
* Contains the list of Jokes the Activity will present to the user.
**/
protected ArrayList<Joke> m_arrJokeList;
/**
* Adapter used to bind an AdapterView to List of Jokes.
*/
protected JokeListAdapter m_jokeAdapter;
/**
* ViewGroup used for maintaining a list of Views that each display Jokes.
**/
//protected LinearLayout m_vwJokeLayout;
protected ListView 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;
/**
* Context-Menu MenuItem ID's
* IMPORTANT: You must use these when creating your MenuItems or the tests
* used to grade your submission will fail.
*/
protected static final int REMOVE_JOKE_MENUITEM = Menu.FIRST;
protected static final int UPLOAD_JOKE_MENUITEM = Menu.FIRST + 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO
initLayout();
m_arrJokeList = new ArrayList<Joke>();
m_jokeAdapter = new JokeListAdapter(getApplicationContext(), m_arrJokeList);
m_vwJokeLayout.setAdapter(m_jokeAdapter);
setContentView(R.layout.advanced);
String[] jokestring = this.getResources().getStringArray(R.array.jokeList);
for(String str : jokestring) {
Joke temp = new Joke(str, "");
addJoke(temp);
}
initAddJokeListeners();
}
/**
* Method is used to encapsulate the code that initializes and sets the
* Layout for this Activity.
*/
protected void initLayout() {
// TODO
m_vwJokeLayout = (ListView)findViewById(R.id.jokeListViewGroup);
//m_vwJokeLayout = (LinearLayout)findViewById(R.id.jokeListViewGroup);
/*m_vwJokeLayout = new LinearLayout(this);
m_vwJokeLayout.setOrientation(LinearLayout.VERTICAL);
ScrollView Sv = new ScrollView(this);
m_vwJokeButton = new Button(this);
m_vwJokeButton.setText("Add Joke");
m_vwJokeEditText = new EditText(this);
m_vwJokeEditText.setSingleLine();
m_vwJokeEditText.setInputType(InputType.TYPE_NULL);
m_vwJokeEditText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
root_layout = new LinearLayout(this);
root_layout.setOrientation(LinearLayout.VERTICAL);
top_layout = new LinearLayout(this);
top_layout.setOrientation(LinearLayout.HORIZONTAL);
Sv.addView(m_vwJokeLayout);
top_layout.addView(m_vwJokeButton);
top_layout.addView(m_vwJokeEditText);
root_layout.addView(top_layout);
root_layout.addView(Sv);
setContentView(root_layout);*/
}
/**
* Method is 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
m_vwJokeButton = (Button)findViewById(R.id.addJokeButton);
m_vwJokeEditText = (EditText)findViewById(R.id.newJokeEditText);
m_vwJokeEditText.setInputType(InputType.TYPE_NULL);
m_vwJokeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(m_vwJokeEditText.getText().toString().equals("") == false)
{
String str = m_vwJokeEditText.getText().toString();
m_vwJokeEditText.setText("");
Joke temp = new Joke(str, "");
addJoke(temp);
//InputMethodManager imm = (InputMethodManager)
//getSystemService(Context.INPUT_METHOD_SERVICE);
//imm.hideSoftInputFromWindow(m_vwJokeEditText.getWindowToken(), 0);
}
}
});
}
/**
* Method used for encapsulating the logic necessary to properly add a new
* Joke to m_arrJokeList, and display it on screen.
*
* @param joke
* The Joke to add to list of Jokes.
*/
protected void addJoke(Joke joke) {
// TODO
/*TextView TV=new TextView(this);
TV.setTextSize(16);
if(color_counter % 2 == 0)
TV.setBackgroundColor(this.getResources().getColor(R.color.dark));
else
TV.setBackgroundColor(this.getResources().getColor(R.color.light));
color_counter ++;
m_vwJokeLayout.addView(TV);
TV.setText(joke.toString());*/
JokeView JV = new JokeView(this, joke);
if(color_counter % 2 == 0)
JV.setBackgroundColor(this.getResources().getColor(R.color.dark));
else
JV.setBackgroundColor(this.getResources().getColor(R.color.light));
color_counter ++;
m_arrJokeList.add(JV);
}
/**
* Method used to retrieve Jokes from online server. The getJoke script
* takes a single optional parameter, which should be encode in "UTF-8".
* This parameter allows tells script to only retrieve Jokes whose author
* name matches the value in the parameter.
*
* param-1) "author": The author of the joke.
*
* URL: http://simexusa.com/aac/getJokes.php?
*
*/
protected void getJokesFromServer() {
// TODO
}
/**
* This method uploads a single Joke to the server. This method should test
* the response from the server and display success or failure to the user
* via a Toast Notification
*
* The addJoke script on the server requires two parameters, both of which
* should be encode in "UTF-8":
*
* param-1) "joke": The text of the joke.
*
* param-2) "author": The author of the joke.
*
* URL: http://simexusa.com/aac/addJoke.php?
*
* @param joke
* The Joke to be uploaded to the server.
*
*/
protected void uploadJokeToServer(Joke joke) {
// TODO
}
}这里的问题是,我尝试了很多方法来将JokeView添加到ListView中,而不是LinearLayout中,但都没有成功。
发布于 2011-08-06 19:35:03
调用适配器的notifyDataSetChanged()方法或简单地将
m_jokeAdapter.notifyDataSetChanged();在你把你的笑话JV添加到m_arrJokeList之后。ListView可能有add()和addView()方法,但它们通常会返回异常。:)
https://stackoverflow.com/questions/5449826
复制相似问题