Android 列表<;类别>;使用strings.xml
使用Android的Studio,我从一个导航抽屉活动开始。然后,我添加了一个主细节活动,从DummyContent类模板生成一个“真实”类。应用程序在硬编码项时工作。我需要使用多种语言,因此我必须使用strings.xml进行语言翻译 我使用活动扩展了ProductsAZ.java,但尝试使用“this”和“getString()”失败。我可以指定stringheader1=getString(R.String.header1);。添加新产品项时,如何使用此设计/类从strings.xml调用stringsAndroid 列表<;类别>;使用strings.xml,android,string,list,arraylist,master-detail,Android,String,List,Arraylist,Master Detail,使用Android的Studio,我从一个导航抽屉活动开始。然后,我添加了一个主细节活动,从DummyContent类模板生成一个“真实”类。应用程序在硬编码项时工作。我需要使用多种语言,因此我必须使用strings.xml进行语言翻译 我使用活动扩展了ProductsAZ.java,但尝试使用“this”和“getString()”失败。我可以指定stringheader1=getString(R.String.header1);。添加新产品项时,如何使用此设计/类从strings.xml调用
addItem(new Product("1", "1_Title_here", "1_Header_here", "1_Body_here"));
addItem(new Product("2", "2_Title_here", "2_Header_here", "2_Body_here"));
addItem(new Product("3", "3_Title_here", "3_Header_here", "3_Body_here"));
愿望是:
addItem(new Product("1", "@string/1_Title_here", "@string/1_Header_here", "@string/1_Body_here"));
ProductAZ.java
import android.app.Activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ProductAZ extends Activity {
// An array of items.
// public static List<Product> ITEMS = new ArrayList<>();
public static List<Product> ITEMS = new ArrayList<>();
// A map of items, by ID.
public static Map<String, Product> ITEM_MAP = new HashMap<>();
// Add items.
static {
addItem(new Product("1", "1_Title_here", "1_Header_here", "1_Body_here"));
addItem(new Product("2", "2_Title_here", "2_Header_here", "2_Body_here"));
addItem(new Product("3", "3_Title_here", "3_Header_here", "3_Body_here"));
}
private static void addItem(Product product) {
ITEMS.add(product);
ITEM_MAP.put(product.id, product);
}
// An item representing a piece of content.
public static class Product {
public final String id;
public final String title;
public final String header;
public final String details;
public Product(String id, String title, String header, String details) {
this.id = id;
this.title = title;
this.header = header;
this.details = details;
}
@Override
public String toString() {
return title;
}
}
}
ProductDetailActivity.java
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.MenuItem;
import java.util.List;
/**
* An activity representing a list of Products. This activity
* has different presentations for handset and tablet-size devices. On
* handsets, the activity presents a list of items, which when touched,
* lead to a {@link ProductDetailActivity} representing
* item details. On tablets, the activity presents the list of items and
* item details side-by-side using two vertical panes.
*/
public class ProductListActivity extends AppCompatActivity {
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle(getTitle());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// Show the Up button in the action bar.
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
View recyclerView = findViewById(R.id.product_list);
assert recyclerView != null;
setupRecyclerView((RecyclerView) recyclerView);
if (findViewById(R.id.product_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-w900dp).
// If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(ProductAZ.ITEMS));
}
public class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {
private final List<ProductAZ.Product> mValues;
public SimpleItemRecyclerViewAdapter(List<ProductAZ.Product> items) {
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.product_list_content, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setText(mValues.get(position).title);
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ProductDetailFragment.ARG_ITEM_ID, holder.mItem.id);
ProductDetailFragment fragment = new ProductDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.product_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, ProductDetailActivity.class);
intent.putExtra(ProductDetailFragment.ARG_ITEM_ID, holder.mItem.id);
context.startActivity(intent);
}
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public ProductAZ.Product mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.id);
mContentView = (TextView) view.findViewById(R.id.content);
}
@Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
}
}
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
/**
* An activity representing a single Product detail screen. This
* activity is only used narrow width devices. On tablet-size devices,
* item details are presented side-by-side with a list of items
* in a {@link ProductListActivity}.
*/
public class ProductDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// Show the Up button in the action bar.
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
// savedInstanceState is non-null when there is fragment state
// saved from previous configurations of this activity
// (e.g. when rotating the screen from portrait to landscape).
// In this case, the fragment will automatically be re-added
// to its container so we don't need to manually add it.
// For more information, see the Fragments API guide at:
//
// http://developer.android.com/guide/components/fragments.html
//
if (savedInstanceState == null) {
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ProductDetailFragment.ARG_ITEM_ID,
getIntent().getStringExtra(ProductDetailFragment.ARG_ITEM_ID));
ProductDetailFragment fragment = new ProductDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.add(R.id.product_detail_container, fragment)
.commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpTo(this, new Intent(this, ProductListActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
product_detail.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/product_detail"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textIsSelectable="true"
tools:context="com.bobh.znd6.ProductDetailFragment"/>
product_list.xml(小屏幕)
产品列表内容.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem"/>
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem"/>
</LinearLayout>
已解决。我从中找到了解决办法。我需要删除ProductAZ.java中的switch语句。然后在DetailListActivity.java中,我编辑了onCreate方法。使用前一篇文章的大纲来配合我的课程/方法: 创建时受保护的void(Bundle savedInstanceState){
已解决。我在中找到了解决方案。我需要删除ProductAZ.java中的switch语句。然后在DetailListActivity.java中,我编辑了onCreate方法。使用前一篇文章中适合我的类/方法的大纲: 创建时受保护的void(Bundle savedInstanceState){
这已经解决了…我需要删除ProductAZ.java类中的switch语句,在DetailListActivity.java中我编辑了onCreate方法。这已经解决了…我需要删除ProductAZ.java类中的switch语句,在DetailListActivity.java中我编辑了onCreate方法。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/product_list"
android:name="com.bobh.znd5.ProductListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context="com.bobh.znd6.ProductListActivity"
tools:listitem="@layout/product_list_content"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem"/>
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem"/>
</LinearLayout>
super.onCreate(savedInstanceState);
if (DummyContent.ITEMS.isEmpty())
{
DummyContent.addItem(new DummyItem("1", getResources().getString(R.string.menu1)));
DummyContent.addItem(new DummyItem("2", getResources().getString(R.string.menu2)));
DummyContent.addItem(new DummyItem("3", getResources().getString(R.string.menu3)));
}