[and] Use .html data from assets

This commit is contained in:
d-kunin 2013-08-06 21:02:45 +03:00
parent 41f75d6508
commit 03e8439305
11 changed files with 71 additions and 136 deletions

View file

@ -26,7 +26,7 @@
android:name="com.example.travelguide.ArticleInfoListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_weight="2"
tools:layout="@android:layout/list_content" />
<FrameLayout

View file

@ -25,7 +25,7 @@ public class ArticleInfoDetailActivity extends FragmentActivity
setContentView(R.layout.activity_articleinfo_detail);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
// getActionBar().setDisplayHomeAsUpEnabled(true);
// savedInstanceState is non-null when there is fragment state
// saved from previous configurations of this activity
@ -41,8 +41,9 @@ public class ArticleInfoDetailActivity extends FragmentActivity
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ArticleInfoDetailFragment.ARG_ITEM_ID,
getIntent().getStringExtra(ArticleInfoDetailFragment.ARG_ITEM_ID));
arguments.putSerializable(ArticleInfoDetailFragment.ARTICLE_INFO,
getIntent().getSerializableExtra(ArticleInfoDetailFragment.ARTICLE_INFO));
ArticleInfoDetailFragment fragment = new ArticleInfoDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction().add(R.id.articleinfo_detail_container, fragment).commit();

View file

@ -5,9 +5,12 @@ import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;
import com.example.travelguide.dummy.DummyContent;
import com.example.travelguide.article.ArticleInfo;
import com.example.travelguide.article.ArticlePathFinder;
import com.example.travelguide.article.AssetsArticlePathFinder;
/**
* A fragment representing a single ArticleInfo detail screen. This fragment is
@ -16,16 +19,14 @@ import com.example.travelguide.dummy.DummyContent;
*/
public class ArticleInfoDetailFragment extends Fragment
{
/**
* The fragment argument representing the item ID that this fragment
* represents.
*/
public static final String ARG_ITEM_ID = "item_id";
public static final String ARTICLE_INFO = "article_info";
/**
* The dummy content this fragment is presenting.
*/
private DummyContent.DummyItem mItem;
private ArticleInfo mItem;
private View mRootView;
private WebView mWebView;
private ArticlePathFinder mFinder;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
@ -39,26 +40,25 @@ public class ArticleInfoDetailFragment extends Fragment
{
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID))
{
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
}
if (getArguments().containsKey(ARTICLE_INFO))
mItem = (ArticleInfo) getArguments().getSerializable(ARTICLE_INFO);
if (mItem == null)
throw new RuntimeException("ArticleInfo must be specified.");
mFinder = new AssetsArticlePathFinder();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_articleinfo_detail, container, false);
mRootView = inflater.inflate(R.layout.fragment_articleinfo_detail, container, false);
mWebView = (WebView) mRootView.findViewById(R.id.webView);
((TextView) mRootView.findViewById(R.id.articleinfo_detail)).setText(mItem.getName());
// Show the dummy content as text in a TextView.
if (mItem != null)
{
((TextView) rootView.findViewById(R.id.articleinfo_detail)).setText(mItem.content);
}
mWebView.loadUrl(mFinder.getPath(mItem));
return rootView;
return mRootView;
}
}

View file

@ -4,6 +4,8 @@ import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.example.travelguide.article.ArticleInfo;
/**
* An activity representing a list of ArticleInfos. This activity has different
* presentations for handset and tablet-size devices. On handsets, the activity
@ -37,14 +39,7 @@ public class ArticleInfoListActivity extends FragmentActivity implements Article
if (findViewById(R.id.articleinfo_detail_container) != null)
{
// The detail container view will be present only in the
// large-screen layouts (res/values-large and
// res/values-sw600dp). If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
// In two-pane mode, list items should be given the
// 'activated' state when touched.
((ArticleInfoListFragment) getSupportFragmentManager().findFragmentById(R.id.articleinfo_list))
.setActivateOnItemClick(true);
}
@ -57,26 +52,21 @@ public class ArticleInfoListActivity extends FragmentActivity implements Article
* that the item with the given ID was selected.
*/
@Override
public void onItemSelected(String id)
public void onItemSelected(ArticleInfo info)
{
if (mTwoPane)
{
// In two-pane mode, show the detail view in this activity by
// adding or replacing the detail fragment using a
// fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ArticleInfoDetailFragment.ARG_ITEM_ID, id);
arguments.putSerializable(ArticleInfoDetailFragment.ARTICLE_INFO, info);
ArticleInfoDetailFragment fragment = new ArticleInfoDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction().replace(R.id.articleinfo_detail_container, fragment).commit();
}
else
{
// In single-pane mode, simply start the detail activity
// for the selected item ID.
Intent detailIntent = new Intent(this, ArticleInfoDetailActivity.class);
detailIntent.putExtra(ArticleInfoDetailFragment.ARG_ITEM_ID, id);
detailIntent.putExtra(ArticleInfoDetailFragment.ARTICLE_INFO, info);
startActivity(detailIntent);
}
}

View file

@ -1,11 +1,11 @@
package com.example.travelguide;
import static com.example.travelguide.util.Utils.hideIf;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.text.TextUtilsCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
@ -16,13 +16,11 @@ import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.example.travelguide.article.ArticleInfo;
import com.example.travelguide.async.QueryResultLoader;
import com.example.travelguide.cpp.Storage;
import com.example.travelguide.dummy.DummyContent;
import com.example.travelguide.widget.StorageArticleInfoAdapter;
import static com.example.travelguide.util.Utils.*;
/**
* A list fragment representing a list of ArticleInfos. This fragment also
* supports tablet devices by allowing list items to be given an 'activated'
@ -38,9 +36,7 @@ public class ArticleInfoListFragment extends ListFragment implements LoaderCallb
private View mRootView;
private TextView mSearchText;
private View mCross;
private View mSearchIcon;
private Storage mStorage;
/**
* The serialization (saved instance state) Bundle key representing the
@ -69,7 +65,7 @@ public class ArticleInfoListFragment extends ListFragment implements LoaderCallb
/**
* Callback for when an item has been selected.
*/
public void onItemSelected(String id);
public void onItemSelected(ArticleInfo info);
}
/**
@ -79,7 +75,7 @@ public class ArticleInfoListFragment extends ListFragment implements LoaderCallb
private static Callbacks sDummyCallbacks = new Callbacks()
{
@Override
public void onItemSelected(String id)
public void onItemSelected(ArticleInfo info)
{}
};
@ -90,13 +86,6 @@ public class ArticleInfoListFragment extends ListFragment implements LoaderCallb
public ArticleInfoListFragment()
{}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mStorage = new Storage();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
@ -143,10 +132,7 @@ public class ArticleInfoListFragment extends ListFragment implements LoaderCallb
public void onListItemClick(ListView listView, View view, int position, long id)
{
super.onListItemClick(listView, view, position, id);
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
mCallbacks.onItemSelected((ArticleInfo) getListAdapter().getItem(position));
}
@Override
@ -154,10 +140,7 @@ public class ArticleInfoListFragment extends ListFragment implements LoaderCallb
{
super.onSaveInstanceState(outState);
if (mActivatedPosition != ListView.INVALID_POSITION)
{
// Serialize and persist the activated item position.
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
}
}
/**
@ -190,7 +173,6 @@ public class ArticleInfoListFragment extends ListFragment implements LoaderCallb
{
mRootView = inflater.inflate(R.layout.fragment_articleinfo_list, null, false);
mSearchText = (TextView) mRootView.findViewById(R.id.searchText);
mSearchIcon = mRootView.findViewById(R.id.searchIcon);
mCross = mRootView.findViewById(R.id.clearSearch);
// setup listeners

View file

@ -1,6 +1,9 @@
package com.example.travelguide.article;
public class ArticleInfo
import java.io.Serializable;
@SuppressWarnings("serial")
public class ArticleInfo implements Serializable
{
public ArticleInfo(String articleUrl, String iconUrl, String title)

View file

@ -0,0 +1,6 @@
package com.example.travelguide.article;
public interface ArticlePathFinder
{
public String getPath(ArticleInfo info);
}

View file

@ -0,0 +1,13 @@
package com.example.travelguide.article;
public class AssetsArticlePathFinder implements ArticlePathFinder
{
@Override
public String getPath(ArticleInfo info)
{
String pathInAssets = "file:///android_asset/" + info.getArticleId() + ".html";
return pathInAssets;
}
}

View file

@ -1,61 +0,0 @@
package com.example.travelguide.dummy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Helper class for providing sample content for user interfaces created by
* Android template wizards.
* <p>
* TODO: Replace all uses of this class before publishing your app.
*/
public class DummyContent
{
/**
* An array of sample (dummy) items.
*/
public static List<DummyItem> ITEMS = new ArrayList<DummyItem>();
/**
* A map of sample (dummy) items, by ID.
*/
public static Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();
static
{
// Add 3 sample items.
addItem(new DummyItem("1", "Item 1"));
addItem(new DummyItem("2", "Item 2"));
addItem(new DummyItem("3", "Item 3"));
}
private static void addItem(DummyItem item)
{
ITEMS.add(item);
ITEM_MAP.put(item.id, item);
}
/**
* A dummy item representing a piece of content.
*/
public static class DummyItem
{
public String id;
public String content;
public DummyItem(String id, String content)
{
this.id = id;
this.content = content;
}
@Override
public String toString()
{
return content;
}
}
}

View file

@ -2,6 +2,7 @@ package com.example.travelguide.thumb;
import java.io.IOException;
import java.io.InputStream;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
@ -17,7 +18,7 @@ public class AssetsThumbnailProvider implements ThumbnailsProvider
public AssetsThumbnailProvider(Context context)
{
mContext = context;
mAssetManager = context.getAssets();
mAssetManager = mContext.getAssets();
}
@SuppressLint("DefaultLocale")

View file

@ -1,11 +1,5 @@
package com.example.travelguide.widget;
import com.example.travelguide.R;
import com.example.travelguide.article.ArticleInfo;
import com.example.travelguide.cpp.Storage;
import com.example.travelguide.thumb.AssetsThumbnailProvider;
import com.example.travelguide.thumb.ThumbnailsProvider;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
@ -14,6 +8,12 @@ import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.travelguide.R;
import com.example.travelguide.article.ArticleInfo;
import com.example.travelguide.cpp.Storage;
import com.example.travelguide.thumb.AssetsThumbnailProvider;
import com.example.travelguide.thumb.ThumbnailsProvider;
public class StorageArticleInfoAdapter extends BaseAdapter
{
private Storage mStorage;