3つの画像/アイコンを含むGridViewレイアウトがあります。私がやろうとしているのは、アイテムがクリックされるたびに、Webサイトページ(Webview)が表示されるようにすることです。これが私がしたことです:
- main.xmlファイルで、LinearLayoutをルート要素としてGridViewおよびWebViewレイアウトを定義しました。
- Androidmanifest.xmlファイルでインターネットアクセスの許可が与えられました
- gridviewのコードを記述
- Webviewのコードを書きました。
- GridViewのsetOnItemClickListenerの下で、loadUrl()を使用して表示されるWebページのURLを指定しました。
それで、質問:「アイコン/画像がクリックされたときに表示されるWebページを作成するにはどうすればよいですか?」 エミュレーターでUIを見ることができます。ただし、アイコンをクリックしても応答はありません。私が推測するonClickItem()イベントで何か問題が発生しました。
お時間をいただきありがとうございます。私は次のコードを書きました:
package prototype.wenview.gridview;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends Activity
{
// Images to display-----------------------//
Integer[] imageIds = { R.drawable.market, R.drawable.news, R.drawable.weather };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); //calls the method from super class Activity
setContentView(R.layout.main); //sets the UI
GridView gridView = (GridView) findViewById(R.id.gridview);// Bring gridView into context from main.xml
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
WebView myWebView = (WebView)findViewById(R.id.webview);
myWebView.loadUrl("http://translate.google.com/translate?sl=auto&tl=hi&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.kvk.pravara.com%2Fweather.html");
}
}
); //setOnItemClickListener declaration ends here.
} //onCreate() ends here.
public class ImageAdapter extends BaseAdapter
{
private Context context; // Context class object - context
public ImageAdapter(Context c)
{
context = c;
}
//return number of images
public int getCount()
{
return imageIds.length;
}
public Object getItem(int position) // return type of getItem method is an Object
{
return position;
}
public long getItemId(int position)
{
return position;
}
//returns an ImageView view--------------
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if(convertView == null)
{
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageIds[position]);
return imageView;
}
}
}
XMLコード:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
/*Activity2.javaのXMLコード。このページは、グリッドビューのアイコンがクリックされた後にWebページを表示することを目的としています。* /
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
//アクティビティ2のJavaコード
パッケージin.niteesh.MultipleActivities;
import android.app.Activity; android.os.Bundleをインポートします。
public class Activity2 extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); //calls the method from super class Activity
setContentView(R.layout.activity2); //sets the UI
}
}
//Activity2の存在について言及したAndroidManifest.xmlファイル
<activity android:name=".Activity2" android:label="Activity 2">
<intent-filter>
<action android:name="net.learn2develop.ACTIVITY2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
// onItemClickのMainActivityクラスで、2番目のアクティビティに表示されるWebページ用に次のコードを記述しました
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
String url = "http://google.com/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(new Intent("net.learn2develop.ACTIVITY2"));//reference to start the activity
}
/問題は-アイコンをクリックすると、Activity2に切り替わりますが、Webページが表示されません。さまざまなターンアラウンドを試しましたが、解決策が見つかりませんでした/