ide มองไม่เห็น mThumbIds ครับ ใน file ImageAdapter ทำไงดี
ประกาศ mThumbIds อยู่ใน MainActivity ครับ แต่ใน ImageAdapter ide มองไม่เห็น
ผมลอง ImageAdapter extends MainActivity ได้อยู่ แต่ใน MainActivity จะ error ตรง gridview.setAdapter( new ImageAdapter(this));
แล้วถ้าผม cast new ImageAdapter(this) เป็น ListAdapter ตามคำแนะนำของ ide ก็จะขึ้น ขออภัย app หยุดทำงาน
เป็น app android เกมจับคู่ภาพครับ
[open_code]
package com.nathatsoft.nathat.memorymatch;
import android.app.Activity;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import java.util.Random;
import android.app.Activity;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity implements
AdapterView.OnItemClickListener {
GridView gridview;
ImageView imageView, firstView;
// references to our images
public Integer[] mThumbIds = {R.drawable.lemmling, R.drawable.monkey,
R.drawable.obama, R.drawable.zebra, R.drawable.lemmling,
R.drawable.monkey, R.drawable.obama, R.drawable.zebra};
int opened = 0;
int firstClick, secondClick; //use for note
int numColumns = 4; //Number of GridView Columns
// Implementing Fisher–Yates shuffle
static void shuffleArray(Integer[] mThumbIds) {
Random rnd = new Random();
for (int i = mThumbIds.length - 1; i >= 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = mThumbIds[index];
mThumbIds[index] = mThumbIds
;
mThumbIds = a;
}
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shuffleArray(mThumbIds);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setNumColumns(numColumns);
//gridview.setAdapter(new ImageAdapter(this));
gridview.setAdapter( new ImageAdapter(this));
gridview.setOnItemClickListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("Main Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://host/path"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
imageView = (ImageView) v;
imageView.setAdjustViewBounds(true); //Adjust image in bound
opened++;
final Handler handler = new Handler(); // Handler for time delay
if (opened == 1) {
firstClick = position;
firstView = (ImageView) v;
imageView.setImageResource(mThumbIds[position]);
} else if (opened == 2) {
secondClick = position;
imageView.setImageResource(mThumbIds[position]);
imageView.setClickable(false);
if (mThumbIds[firstClick].compareTo(mThumbIds[secondClick]) == 0) {
Toast.makeText(MainActivity.this, "That's Great!",
Toast.LENGTH_SHORT).show();
handler.postDelayed(removeImage, 400);
} else {
handler.postDelayed(cardBack, 600);
}
}
}
private Runnable removeImage = new Runnable() {
@Override
public void run() {
imageView.setVisibility(View.GONE);
firstView.setVisibility(View.GONE);
opened = 0;
}
};
private Runnable cardBack = new Runnable() {
@Override
public void run() {
imageView.setImageResource(R.drawable.card_back);
firstView.setImageResource(R.drawable.card_back);
opened = 0;
}
};
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
}
[/close_code]
[open_code]
package com.nathatsoft.nathat.memorymatch;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
//imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
//imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
[/close_code]
ide มองไม่เห็น mThumbIds ครับ ใน file ImageAdapter ทำไงดี(Android)
ประกาศ mThumbIds อยู่ใน MainActivity ครับ แต่ใน ImageAdapter ide มองไม่เห็น
ผมลอง ImageAdapter extends MainActivity ได้อยู่ แต่ใน MainActivity จะ error ตรง gridview.setAdapter( new ImageAdapter(this));
แล้วถ้าผม cast new ImageAdapter(this) เป็น ListAdapter ตามคำแนะนำของ ide ก็จะขึ้น ขออภัย app หยุดทำงาน
เป็น app android เกมจับคู่ภาพครับ
[open_code]
package com.nathatsoft.nathat.memorymatch;
import android.app.Activity;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import java.util.Random;
import android.app.Activity;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity implements
AdapterView.OnItemClickListener {
GridView gridview;
ImageView imageView, firstView;
// references to our images
public Integer[] mThumbIds = {R.drawable.lemmling, R.drawable.monkey,
R.drawable.obama, R.drawable.zebra, R.drawable.lemmling,
R.drawable.monkey, R.drawable.obama, R.drawable.zebra};
int opened = 0;
int firstClick, secondClick; //use for note
int numColumns = 4; //Number of GridView Columns
// Implementing Fisher–Yates shuffle
static void shuffleArray(Integer[] mThumbIds) {
Random rnd = new Random();
for (int i = mThumbIds.length - 1; i >= 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = mThumbIds[index];
mThumbIds[index] = mThumbIds;
mThumbIds = a;
}
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shuffleArray(mThumbIds);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setNumColumns(numColumns);
//gridview.setAdapter(new ImageAdapter(this));
gridview.setAdapter( new ImageAdapter(this));
gridview.setOnItemClickListener(this);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("Main Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://host/path"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
imageView = (ImageView) v;
imageView.setAdjustViewBounds(true); //Adjust image in bound
opened++;
final Handler handler = new Handler(); // Handler for time delay
if (opened == 1) {
firstClick = position;
firstView = (ImageView) v;
imageView.setImageResource(mThumbIds[position]);
} else if (opened == 2) {
secondClick = position;
imageView.setImageResource(mThumbIds[position]);
imageView.setClickable(false);
if (mThumbIds[firstClick].compareTo(mThumbIds[secondClick]) == 0) {
Toast.makeText(MainActivity.this, "That's Great!",
Toast.LENGTH_SHORT).show();
handler.postDelayed(removeImage, 400);
} else {
handler.postDelayed(cardBack, 600);
}
}
}
private Runnable removeImage = new Runnable() {
@Override
public void run() {
imageView.setVisibility(View.GONE);
firstView.setVisibility(View.GONE);
opened = 0;
}
};
private Runnable cardBack = new Runnable() {
@Override
public void run() {
imageView.setImageResource(R.drawable.card_back);
firstView.setImageResource(R.drawable.card_back);
opened = 0;
}
};
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
}
[/close_code]
[open_code]
package com.nathatsoft.nathat.memorymatch;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
//imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
//imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
[/close_code]