[android] Java bindings to native core.
This commit is contained in:
parent
fae65739df
commit
22f2835cb8
8 changed files with 145 additions and 21 deletions
|
@ -5,7 +5,7 @@
|
|||
android:versionName="1.0" >
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="11"
|
||||
android:minSdkVersion="9"
|
||||
android:targetSdkVersion="18" />
|
||||
|
||||
<application
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include "../../std/string.hpp"
|
||||
|
||||
string JString2StdString(JNIEnv * env, jstring javaStr)
|
||||
inline string JString2StdString(JNIEnv * env, jstring javaStr)
|
||||
{
|
||||
string res;
|
||||
char const * buffer = env->GetStringUTFChars(javaStr, 0);
|
||||
|
@ -14,7 +14,7 @@ string JString2StdString(JNIEnv * env, jstring javaStr)
|
|||
return res;
|
||||
}
|
||||
|
||||
jstring StdString2JString(JNIEnv * env, string stdString)
|
||||
inline jstring StdString2JString(JNIEnv * env, string const & stdString)
|
||||
{
|
||||
return env->NewStringUTF(stdString.c_str());
|
||||
}
|
||||
|
|
23
android/res/layout/listitem_article_info.xml
Normal file
23
android/res/layout/listitem_article_info.xml
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="horizontal" >
|
||||
|
||||
<TextView
|
||||
style="@android:style/TextAppearance.Medium"
|
||||
android:id="@+id/title"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1"
|
||||
android:text="Hello World!" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/thumbnail"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:layout_weight="0"
|
||||
android:src="@drawable/ic_launcher" />
|
||||
|
||||
</LinearLayout>
|
|
@ -9,7 +9,9 @@ import android.view.ViewGroup;
|
|||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.example.travelguide.cpp.Storage;
|
||||
import com.example.travelguide.dummy.DummyContent;
|
||||
import com.example.travelguide.widget.StorageArticleInfoAdapter;
|
||||
|
||||
/**
|
||||
* A list fragment representing a list of ArticleInfos. This fragment also
|
||||
|
@ -23,7 +25,7 @@ import com.example.travelguide.dummy.DummyContent;
|
|||
public class ArticleInfoListFragment extends ListFragment
|
||||
{
|
||||
private View mRootView;
|
||||
|
||||
private Storage mStorage;
|
||||
|
||||
/**
|
||||
* The serialization (saved instance state) Bundle key representing the
|
||||
|
@ -77,10 +79,10 @@ public class ArticleInfoListFragment extends ListFragment
|
|||
public void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// TODO: replace with a real list adapter.
|
||||
setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
|
||||
android.R.layout.simple_list_item_activated_1, android.R.id.text1, DummyContent.ITEMS));
|
||||
mStorage = new Storage();
|
||||
// TODO: remove it
|
||||
mStorage.query("", false, 0, 0);
|
||||
setListAdapter(new StorageArticleInfoAdapter(mStorage, getActivity()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -2,11 +2,18 @@ package com.example.travelguide.article;
|
|||
|
||||
public class ArticleInfo
|
||||
{
|
||||
private String mArticleId;
|
||||
|
||||
public ArticleInfo(String articleUrl, String iconUrl, String title)
|
||||
{
|
||||
this.mArticleUrl = articleUrl;
|
||||
this.mIconUrl = iconUrl;
|
||||
this.mTitle = title;
|
||||
}
|
||||
private String mArticleUrl;
|
||||
private String mIconUrl;
|
||||
private String mName;
|
||||
private String mTitle;
|
||||
|
||||
public String getName() { return mName; }
|
||||
public String getName() { return mTitle; }
|
||||
public String getIconUrl() { return mIconUrl; }
|
||||
public String getArticleId() { return mArticleId; }
|
||||
public String getArticleId() { return mArticleUrl; }
|
||||
}
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package com.example.travelguide.article;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ArticlesProvider
|
||||
{
|
||||
List<ArticleInfo> queryByLocation();
|
||||
List<ArticleInfo> query(String query);
|
||||
}
|
18
android/src/com/example/travelguide/cpp/Storage.java
Normal file
18
android/src/com/example/travelguide/cpp/Storage.java
Normal file
|
@ -0,0 +1,18 @@
|
|||
package com.example.travelguide.cpp;
|
||||
|
||||
import com.example.travelguide.article.ArticleInfo;
|
||||
|
||||
public class Storage
|
||||
{
|
||||
static
|
||||
{
|
||||
System.loadLibrary("tguide");
|
||||
}
|
||||
|
||||
|
||||
public native void query(String query, boolean useLocation, double lat, double lon);
|
||||
|
||||
public native int getResultSize();
|
||||
|
||||
public native ArticleInfo getArticleInfoByIndex(int index);
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
package com.example.travelguide.widget;
|
||||
|
||||
import com.example.travelguide.R;
|
||||
import com.example.travelguide.article.ArticleInfo;
|
||||
import com.example.travelguide.cpp.Storage;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class StorageArticleInfoAdapter extends BaseAdapter
|
||||
{
|
||||
private Storage mStorage;
|
||||
private Context mContext;
|
||||
|
||||
public StorageArticleInfoAdapter(Storage storage, Context context)
|
||||
{
|
||||
mStorage = storage;
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount()
|
||||
{
|
||||
return mStorage.getResultSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArticleInfo getItem(int position)
|
||||
{
|
||||
return mStorage.getArticleInfoByIndex(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position)
|
||||
{
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent)
|
||||
{
|
||||
final View view = convertView == null
|
||||
? LayoutInflater.from(mContext).inflate(R.layout.listitem_article_info, null)
|
||||
: convertView;
|
||||
|
||||
ViewHolder holder;
|
||||
if (view.getTag() == null)
|
||||
holder = new ViewHolder(view);
|
||||
else
|
||||
holder = (ViewHolder) view.getTag();
|
||||
|
||||
holder.fill(mStorage.getArticleInfoByIndex(position));
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
class ViewHolder
|
||||
{
|
||||
final TextView mTitle;
|
||||
final ImageView mThumbnail;
|
||||
|
||||
ArticleInfo mInfo;
|
||||
|
||||
ViewHolder(View view)
|
||||
{
|
||||
mTitle = (TextView) view.findViewById(R.id.title);
|
||||
mThumbnail = (ImageView) view.findViewById(R.id.thumbnail);
|
||||
}
|
||||
|
||||
void fill(ArticleInfo info)
|
||||
{
|
||||
mInfo = info;
|
||||
mTitle.setText(info.getName());
|
||||
// TODO set image
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue