请帮助我解决我的问题,获取要在EditText Android上显示的GridView的图像源名称。如果单击imageview然后在EditText android ..中显示名称图像在图像名称之前从资源名称中提取而不是从GridView中已经存在...!
此视图图片ilustration:
grid_view:
<?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">
<GridView
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="368dp"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>
的strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Tes Buta Warna</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="inputdata">INPUT DATA</string>
<string name="test">TEST</string>
<string-array
name="string_array_name">
<item>Edwin</item>
<item>Yulius</item>
<item>Natan</item>
<item>Ria</item>
</string-array>
</resources>
ImageAdapter.Java
package com.tes.butawarna;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.plat_ishihara_01,
R.drawable.plat_ishihara_02,
R.drawable.plat_ishihara_04,
R.drawable.plat_ishihara_05,
R.drawable.plat_ishihara_07,
R.drawable.plat_ishihara_08,
R.drawable.plat_ishihara_09,
R.drawable.plat_ishihara_10,
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
AndroidGridLayout.java
package com.tes.butawarna;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.GridView;
public class AndroidGridLayoutActivity extends Activity {
String[] nama;
EditText namagambar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
namagambar=(EditText)findViewById(R.id.editText1);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), Training.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
答案 0 :(得分:0)
您需要定义与drawable相同名称的字符串数组。
/res/values/string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array
name="string_array_name">
<item>Edwin</item>
<item>Yulius</item>
<item>Natan</item>
<item>Ria</item>
</string-array>
</resources>
代码
String[] names = getResource().getStringArray(R.array.string_array_name);
// you must implement this class
YourAdapter adapter = new YourAdapter(names, /* and some params */);
youGridView.setAdapter(adapter);
YouAdapter.java(扩展android.widget.ArrayAdapter)
@override
public View getView(int position, View convertView, ViewGroup parent) {
// you must inflate gridview' child layout
View view = layoutInflater.inflate( ... );
ImageView imageView = (ImageView) view.findViewById( ... );
TextView imageView = (TextView) view.findViewById( ... );
// ...
String name = getItem();
int id = getIdentifier(name, "drawable", getApplicationInfo().packageName);
imageView.setImageResource(id);
textView.setText(name);
}
这仅在所有图像都在资源目录中时才有效。