我上课了,我被困了。该任务是采用现有的应用程序来保存Twitter搜索并添加过滤器选项。我很确定我应该添加一个文本控件(按钮或listview / expandablelistview)并使用数组填充过滤器选项,但我无法使其工作。
我正在为Android 2.3.3使用eclipse和编程(因为这就是本书所说的)
请指出正确的方向。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:padding="5dp"
android:stretchColumns="*" >
<!-- tableRow0 -->
<TableRow
android:id="@+id/tableRow0"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/queryEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="2"
android:hint="@string/queryPrompt"
android:imeOptions="actionNext"
android:inputType="text" >
</EditText>
</TableRow>
<!-- tableRow1 -->
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ExpandableListView
android:id="@+id/filExpandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/tagEditText"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:hint="@string/tagPrompt"
android:imeOptions="actionDone"
android:inputType="text" >
</EditText>
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/save" >
</Button>
</TableRow>
<!-- tableRow2 -->
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_orange" >
<TextView
android:id="@+id/taggedSearchesTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_span="2"
android:padding="5dp"
android:text="@string/taggedSearches"
android:textColor="@android:color/black"
android:textSize="18sp" >
</TextView>
</TableRow>
<!-- tableRow3 -->
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/light_orange" >
<ScrollView
android:id="@+id/queryScrollView"
android:layout_width="match_parent"
android:layout_span="2"
android:padding="5dp" >
<TableLayout
android:id="@+id/queryTableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:stretchColumns="*" >
</TableLayout>
</ScrollView>
</TableRow>
<!-- tableRow4 -->
<TableRow
android:id="@+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/clearTagsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_span="2"
android:text="@string/clearTags" >
</Button>
</TableRow>
爪哇
// main (and only) Activity class for the Favorite Twitter Searches app
public class FavoriteTwitterSearches extends Activity
{
private SharedPreferences savedSearches; // user's favorite searches
private TableLayout queryTableLayout; // shows the search buttons
private EditText queryEditText; // where the user enters queries
private EditText tagEditText; // where the user enters a query's tag
// called when the activity is first created
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); // call the superclass version
setContentView(R.layout.main); // set the layout
// get the SharedPreferences that contains the user's saved searches
savedSearches = getSharedPreferences("searches", MODE_PRIVATE);
// get a reference to the queryTableLayout
queryTableLayout =
(TableLayout) findViewById(R.id.queryTableLayout);
// get references to the two EditTexts
queryEditText = (EditText) findViewById(R.id.queryEditText);
tagEditText = (EditText) findViewById(R.id.tagEditText);
// register listeners for the Save and Clear Tags Buttons
Button saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(saveButtonListener);
Button clearTagsButton =
(Button) findViewById(R.id.clearTagsButton);
clearTagsButton.setOnClickListener(clearTagsButtonListener);
refreshButtons(null); // add previously saved searches to GUI
} // end method onCreate
// recreate search tag and edit Buttons for all saved searches;
// pass null to create all the tag and edit Buttons.
private void refreshButtons(String newTag)
{
// store saved tags in the tags array
String[] tags =
savedSearches.getAll().keySet().toArray(new String[0]);
Arrays.sort(tags, String.CASE_INSENSITIVE_ORDER); // sort by tag
// if a new tag was added, insert in GUI at the appropriate location
if (newTag != null)
{
makeTagGUI(newTag, Arrays.binarySearch(tags, newTag));
} // end if
else // display GUI for all tags
{
// display all saved searches
for (int index = 0; index < tags.length; ++index)
makeTagGUI(tags[index], index);
} // end else
} // end method refreshButtons
// add new search to the save file, then refresh all Buttons
private void makeTag(String query, String tag)
{
// originalQuery will be null if we're modifying an existing search
String originalQuery = savedSearches.getString(tag, null);
// get a SharedPreferences.Editor to store new tag/query pair
SharedPreferences.Editor preferencesEditor = savedSearches.edit();
preferencesEditor.putString(tag, query); // store current search
preferencesEditor.apply(); // store the updated preferences
// if this is a new query, add its GUI
if (originalQuery == null)
refreshButtons(tag); // adds a new button for this tag
} // end method makeTag
// add a new tag button and corresponding edit button to the GUI
private void makeTagGUI(String tag, int index)
{
// get a reference to the LayoutInflater service
LayoutInflater inflater = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// inflate new_tag_view.xml to create new tag and edit Buttons
View newTagView = inflater.inflate(R.layout.new_tag_view, null);
// get newTagButton, set its text and register its listener
Button newTagButton =
(Button) newTagView.findViewById(R.id.newTagButton);
newTagButton.setText(tag);
newTagButton.setOnClickListener(queryButtonListener);
// get newEditButton and register its listener
Button newEditButton =
(Button) newTagView.findViewById(R.id.newEditButton);
newEditButton.setOnClickListener(editButtonListener);
Button newDelButton =
(Button) newTagView.findViewById(R.id.newDelbutton);
newDelButton.setOnClickListener(delButtonListener);
// add new tag and edit buttons to queryTableLayout
queryTableLayout.addView(newTagView, index);
} // end makeTagGUI
// remove all saved search Buttons from the app
private void clearButtons()
{
// remove all saved search Buttons
queryTableLayout.removeAllViews();
} // end method clearButtons
// create a new Button and add it to the ScrollView
public OnClickListener saveButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
// create tag if both queryEditText and tagEditText are not empty
if (queryEditText.getText().length() > 0 &&
tagEditText.getText().length() > 0)
{
makeTag(queryEditText.getText().toString(),
tagEditText.getText().toString());
queryEditText.setText(""); // clear queryEditText
tagEditText.setText(""); // clear tagEditText
// hide the soft keyboard
((InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
tagEditText.getWindowToken(), 0);
} // end if
else // display message asking user to provide a query and a tag
{
// create a new AlertDialog Builder
AlertDialog.Builder builder =
new AlertDialog.Builder(FavoriteTwitterSearches.this);
builder.setTitle(R.string.missingTitle); // title bar string
// provide an OK button that simply dismisses the dialog
builder.setPositiveButton(R.string.OK, null);
// set the message to display
builder.setMessage(R.string.missingMessage);
// create AlertDialog from the AlertDialog.Builder
AlertDialog errorDialog = builder.create();
errorDialog.show(); // display the Dialog
} // end else
} // end method onClick
}; // end OnClickListener anonymous inner class
// clears all saved searches
public OnClickListener clearTagsButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
// create a new AlertDialog Builder
AlertDialog.Builder builder =
new AlertDialog.Builder(FavoriteTwitterSearches.this);
builder.setTitle(R.string.confirmTitle); // title bar string
// provide an OK button that simply dismisses the dialog
builder.setPositiveButton(R.string.erase,
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int button)
{
clearButtons(); // clear all saved searches from the map
// get a SharedPreferences.Editor to clear searches
SharedPreferences.Editor preferencesEditor =
savedSearches.edit();
preferencesEditor.clear(); // remove all tag/query pairs
preferencesEditor.apply(); // the changes
} // end method onClick
} // end anonymous inner class
); // end call to method setPositiveButton
builder.setCancelable(true);
builder.setNegativeButton(R.string.cancel, null);
// set the message to display
builder.setMessage(R.string.confirmMessage);
// create AlertDialog from the AlertDialog.Builder
AlertDialog confirmDialog = builder.create();
confirmDialog.show(); // display the Dialog
} // end method onClick
}; // end OnClickListener anonymous inner class
// load selected search in a web browser
public OnClickListener queryButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
// get the query
String buttonText = ((Button)v).getText().toString();
String query = savedSearches.getString(buttonText, "");
// create the URL corresponding to the touched Button's query
String urlString = getString(R.string.searchURL) + query;
// create an Intent to launch a web browser
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(urlString));
startActivity(webIntent); // execute the Intent
} // end method onClick
}; // end OnClickListener anonymous inner class
// edit selected search
public OnClickListener editButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
// get all necessary GUI components
TableRow buttonTableRow = (TableRow) v.getParent();
Button searchButton =
(Button) buttonTableRow.findViewById(R.id.newTagButton);
String tag = searchButton.getText().toString();
// set EditTexts to match the chosen tag and query
tagEditText.setText(tag);
queryEditText.setText(savedSearches.getString(tag, ""));
} // end method onClick
}; // end OnClickListener anonymous inner class
public OnClickListener delButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
// get all necessary GUI components
TableRow buttonTableRow = (TableRow) v.getParent();
queryTableLayout.removeView(buttonTableRow);
} // end method onClick
}; // end OnClickListener anonymous inner class
} // end class FavoriteTwitterSearches
答案 0 :(得分:0)
我实际上已经开始工作了。我使用了一个微调器并将数组编码到strings.xml文件中。我找到了一些关于如何将微调器选择传递给变量的教程,然后使其工作了大约一天。
它不漂亮但它有效。
我仍然非常感谢任何人都愿意提供的任何指示。
这是我更新的main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:padding="5dp"
android:stretchColumns="*" >
<!-- tableRow0 -->
<TableRow
android:id="@+id/tableRow0"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/queryEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="2"
android:hint="@string/queryPrompt"
android:imeOptions="actionNext"
android:inputType="text" >
</EditText>
</TableRow>
<!-- tableRow1 -->
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Spinner
android:id="@+id/filterSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/filterEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/tagEditText"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:hint="@string/tagPrompt"
android:imeOptions="actionDone"
android:inputType="text" >
</EditText>
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/save" >
</Button>
</TableRow>
<!-- tableRow2 -->
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_orange" >
<TextView
android:id="@+id/taggedSearchesTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_span="2"
android:padding="5dp"
android:text="@string/taggedSearches"
android:textColor="@android:color/black"
android:textSize="18sp" >
</TextView>
</TableRow>
<!-- tableRow3 -->
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/light_orange" >
<ScrollView
android:id="@+id/queryScrollView"
android:layout_width="match_parent"
android:layout_span="2"
android:padding="5dp" >
<TableLayout
android:id="@+id/queryTableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:stretchColumns="*" >
</TableLayout>
</ScrollView>
</TableRow>
<!-- tableRow4 -->
<TableRow
android:id="@+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/clearTagsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_span="2"
android:text="@string/clearTags" >
</Button>
</TableRow>
</TableLayout>
<!--
和我更新的java
// FavoriteTwitterSearches.java
// Stores Twitter search queries and tags for easily opening them
// in a browser.
package com.deitel.favoritetwittersearches;
import java.util.Arrays;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
// main (and only) Activity class for the Favorite Twitter Searches app
public class FavoriteTwitterSearches extends Activity
{
private SharedPreferences savedSearches; // user's favorite searches
private TableLayout queryTableLayout; // shows the search buttons
private EditText queryEditText; // where the user enters queries
private EditText tagEditText; // where the user enters a query's tag
private EditText filterEditText;
private String filtered;
private String filter;
private String filtext;
// called when the activity is first created
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); // call the superclass version
setContentView(R.layout.main); // set the layout
// get the SharedPreferences that contains the user's saved searches
savedSearches = getSharedPreferences("searches", MODE_PRIVATE);
// get a reference to the queryTableLayout
queryTableLayout =
(TableLayout) findViewById(R.id.queryTableLayout);
Spinner spinner = (Spinner) findViewById(R.id.filterSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.filters, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final Spinner filterSpinner = (Spinner) findViewById(R.id.filterSpinner);
filterSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
filter = filterSpinner.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
//get references to the two EditTexts
queryEditText = (EditText) findViewById(R.id.queryEditText);
tagEditText = (EditText) findViewById(R.id.tagEditText);
filterEditText = (EditText) findViewById(R.id.filterEditText);
// register listeners for the Save and Clear Tags Buttons
Button saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(saveButtonListener);
Button clearTagsButton =
(Button) findViewById(R.id.clearTagsButton);
clearTagsButton.setOnClickListener(clearTagsButtonListener);
refreshButtons(null); // add previously saved searches to GUI
} // end method onCreate
// recreate search tag and edit Buttons for all saved searches;
// pass null to create all the tag and edit Buttons.
private void refreshButtons(String newTag)
{
// store saved tags in the tags array
String[] tags =
savedSearches.getAll().keySet().toArray(new String[0]);
Arrays.sort(tags, String.CASE_INSENSITIVE_ORDER); // sort by tag
// if a new tag was added, insert in GUI at the appropriate location
if (newTag != null)
{
makeTagGUI(newTag, Arrays.binarySearch(tags, newTag));
} // end if
else // display GUI for all tags
{
// display all saved searches
for (int index = 0; index < tags.length; ++index)
makeTagGUI(tags[index], index);
} // end else
} // end method refreshButtons
// add new search to the save file, then refresh all Buttons
private void makeTag(String query, String tag, String filt)
{
// originalQuery will be null if we're modifying an existing search
String originalQuery = savedSearches.getString(tag, null);
if(filt != null)
{
query = query + filt;}
else
{query = query;}
// get a SharedPreferences.Editor to store new tag/query pair
SharedPreferences.Editor preferencesEditor = savedSearches.edit();
preferencesEditor.putString(tag, query); // store current search
preferencesEditor.apply(); // store the updated preferences
// if this is a new query, add its GUI
if (originalQuery == null)
refreshButtons(tag); // adds a new button for this tag
} // end method makeTag
// add a new tag button and corresponding edit button to the GUI
private void makeTagGUI(String tag, int index)
{
// get a reference to the LayoutInflater service
LayoutInflater inflater = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// inflate new_tag_view.xml to create new tag and edit Buttons
View newTagView = inflater.inflate(R.layout.new_tag_view, null);
// get newTagButton, set its text and register its listener
Button newTagButton =
(Button) newTagView.findViewById(R.id.newTagButton);
newTagButton.setText(tag);
newTagButton.setOnClickListener(queryButtonListener);
// get newEditButton and register its listener
Button newEditButton =
(Button) newTagView.findViewById(R.id.newEditButton);
newEditButton.setOnClickListener(editButtonListener);
Button newDelButton =
(Button) newTagView.findViewById(R.id.newDelbutton);
newDelButton.setOnClickListener(delButtonListener);
// add new tag and edit buttons to queryTableLayout
queryTableLayout.addView(newTagView, index);
} // end makeTagGUI
// remove all saved search Buttons from the app
private void clearButtons()
{
// remove all saved search Buttons
queryTableLayout.removeAllViews();
} // end method clearButtons
// create a new Button and add it to the ScrollView
public OnClickListener saveButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
if(filterEditText.getText().length() > 0)
{
filtered = filter + "%3A" + filterEditText.getText().toString();}
else
{filtered = "";}
{
// create tag if both queryEditText and tagEditText are not empty
if (queryEditText.getText().length() > 0 &&
tagEditText.getText().length() > 0)
makeTag(queryEditText.getText().toString(),
tagEditText.getText().toString(),
filtered);
queryEditText.setText(""); // clear queryEditText
tagEditText.setText(""); // clear tagEditText
filterEditText.setText("");
// hide the soft keyboard
((InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
tagEditText.getWindowToken(), 0);
} // end if
// display message asking user to provide a query and a tag
{
// create a new AlertDialog Builder
AlertDialog.Builder builder =
new AlertDialog.Builder(FavoriteTwitterSearches.this);
builder.setTitle(R.string.missingTitle); // title bar string
// provide an OK button that simply dismisses the dialog
builder.setPositiveButton(R.string.OK, null);
// set the message to display
builder.setMessage(R.string.missingMessage);
// create AlertDialog from the AlertDialog.Builder
AlertDialog errorDialog = builder.create();
errorDialog.show(); // display the Dialog
} // end else
} // end method onClick
}; // end OnClickListener anonymous inner class
// clears all saved searches
public OnClickListener clearTagsButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
// create a new AlertDialog Builder
AlertDialog.Builder builder =
new AlertDialog.Builder(FavoriteTwitterSearches.this);
builder.setTitle(R.string.confirmTitle); // title bar string
// provide an OK button that simply dismisses the dialog
builder.setPositiveButton(R.string.erase,
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int button)
{
clearButtons(); // clear all saved searches from the map
// get a SharedPreferences.Editor to clear searches
SharedPreferences.Editor preferencesEditor =
savedSearches.edit();
preferencesEditor.clear(); // remove all tag/query pairs
preferencesEditor.apply(); // the changes
} // end method onClick
} // end anonymous inner class
); // end call to method setPositiveButton
builder.setCancelable(true);
builder.setNegativeButton(R.string.cancel, null);
// set the message to display
builder.setMessage(R.string.confirmMessage);
// create AlertDialog from the AlertDialog.Builder
AlertDialog confirmDialog = builder.create();
confirmDialog.show(); // display the Dialog
} // end method onClick
}; // end OnClickListener anonymous inner class
// load selected search in a web browser
public OnClickListener queryButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
// get the query
String buttonText = ((Button)v).getText().toString();
String query = savedSearches.getString(buttonText, "");
// create the URL corresponding to the touched Button's query
String urlString = getString(R.string.searchURL) + query;
// create an Intent to launch a web browser
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(urlString));
startActivity(webIntent); // execute the Intent
} // end method onClick
}; // end OnClickListener anonymous inner class
// edit selected search
public OnClickListener editButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
// get all necessary GUI components
TableRow buttonTableRow = (TableRow) v.getParent();
Button searchButton =
(Button) buttonTableRow.findViewById(R.id.newTagButton);
String tag = searchButton.getText().toString();
// set EditTexts to match the chosen tag and query
tagEditText.setText(tag);
queryEditText.setText(savedSearches.getString(tag, ""));
} // end method onClick
}; // end OnClickListener anonymous inner class
public OnClickListener delButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
// get all necessary GUI components
TableRow buttonTableRow = (TableRow) v.getParent();
queryTableLayout.removeView(buttonTableRow);
} // end method onClick
}; // end OnClickListener anonymous inner class
} // end class FavoriteTwitterSearches