我正在创建游戏应用程序。当我点击GridView
项目在另一个活动上显示该项目时,我正在使用GridView
图像。在另一个活动中,我想匹配图像徽标和textview字符串(徽标名称)。我已经做了一些逻辑,但是当我运行应用程序时,它会转到else语句。
这是我的代码。
GridView活动代码:
public class GridView_Activity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview_layout);
GridView gridview = (GridView) findViewById(R.id.gridViewImage);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(GridView_Activity.this, "" + position, Toast.LENGTH_SHORT).show();
Intent mInDisplay=new Intent(GridView_Activity.this, Activity_One.class);
mInDisplay.putExtra("Index", position);
startActivity(mInDisplay);
}
});
}
}
**另一个活动代码**
public class Activity_One extends Activity {
Button btnSubmit;
ImageView imgDisplayImg;
String stringVAlue;
EditText edittext;
int index;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_image);
Bundle bdl = getIntent().getExtras();
index = bdl.getInt("Index");
System.out.println("Index Position = " + index);
ImageView mImage = (ImageView) findViewById(R.id.mImgView1);
mImage.setImageResource(Constants.mThumbIds[index]);
stringVAlue = String.valueOf(index);
System.out.println("stringVAlue = " + stringVAlue);
btnSubmit = (Button) findViewById(R.id.buttonAdd);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
edittext = (EditText) findViewById(R.id.edittextString_ImageName);
String stringtext = edittext.getText().toString().trim();
System.out.println("stringtext = " + stringtext);
if (stringVAlue != null) {
if (stringVAlue == "13" && stringtext.equals("Catty")) {
Toast.makeText(Activity_One.this,
"This Logo Name Is Right ", Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(Activity_One.this, "Wrong",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
这是ImageAdapter类
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return Constants.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(Constants.mThumbIds[position]);
return imageView;
}
}
答案 0 :(得分:3)
使用equals()
或equalsIgnoreCase()
比较Java中的两个字符串。
替换此
if (stringVAlue == "13" && stringtext.equals("Catty"))
有了这个
if (stringVAlue.equals("13") && stringtext.equals("Catty"))
答案 1 :(得分:0)
试试这个:
if ("13".equals(stringVAlue.trim()) && "Catty".equals(stringtext)) {
Toast.makeText(Activity_One.this, "This Logo Name Is Right ", Toast.LENGTH_SHORT).show();
}