我是Android开发的新手,并且一直关注Android网站上提供的教程.我目前正在阅读Views的教程部分,特别是Grid Views:Hello,Grid View Tutorial.
我无法理解如何通过适配器进行查看.我知道您必须覆盖适配器类中的getView()方法,并在此方法中定义视图的设置方式.我不明白getView()实际上被调用的地方是什么?也许我在这里有错误的心态,但在下面的代码中(网格视图教程)我没有看到任何对getView()的调用(或适配器类中使用的任何其他东西,如getCount()) .
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<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:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
Run Code Online (Sandbox Code Playgroud)
Start.java
package com.examples.hellogridlayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Start extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView)findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View v, int position, long …Run Code Online (Sandbox Code Playgroud) 我正在尝试在MapView中实现双击式缩放功能.事件总是第一次发生,但从未发生过.以下是我的代码.我有一种感觉,它与地图控制器在第一次触发事件后迷路有关.
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class mainmap extends MapActivity implements OnTouchListener{
long lasttime = -1;
MapController mapc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapc = mapView.getController();
mapView.setOnTouchListener(this);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
if(event.getEventTime()-lasttime<2000){
mapc.zoomInFixing((int)event.getX(),(int)event.getY());
}
}
lasttime=event.getEventTime();
return true;
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现自定义MapView.在我的MapActivity(名为mainmap)中,我有一个扩展MapView的内部类:
private class Lmapview extends MapView{
public Lmapview(Context context, AttributeSet attrs) {
super(context, attrs);
gestures = new GestureDetector(mainmap.this, new GestureListener(this));
}
public boolean OnTouchEvent(MotionEvent event){
return gestures.onTouchEvent(event);
}
}
Run Code Online (Sandbox Code Playgroud)
我将我的main.xml格式化为查找内部类,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<view
xmlns:android="http://schemas.android.com/apk/res/android"
class="com.mondo.tbuddy.mainmap$Lmapview"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey=*****
/>
Run Code Online (Sandbox Code Playgroud)
另外,在Androidmanifest.xml中,我有相应的<uses-library android:name="com.google.android.maps"/>条目.
当我尝试运行我的应用程序时,我在logcat中得到(除其他外):
ERROR/AndroidRuntime(14999):引起:android.view.InflateException:二进制XML文件行#2:错误膨胀类com.mondo.tbuddy.mainmap $ Lmapview
这是由我在logcat中找到的这个条目引起的:
ERROR/AndroidRuntime(14999):引起:java.lang.NoSuchMethodException:Lmapview(Context,AttributeSet)
如果我理解正确,我的应用程序崩溃,因为Android说它没有为我的自定义MapView(Lmapview类)找到合适的构造函数.然而,正如您在上面所看到的,它已被定义并且与它正在寻找的签名相匹配.
谁能给我一些见解?
谢谢.
android ×3