私の主な活動では、3 つのカテゴリ (すべて、水平、垂直) を含むスピナーがあり、その下に gridView があります。リソースフォルダーに、垂直モードと水平モードの両方の画像がいくつかあります。スピナーで選択した項目に従って、画像を gridView に設定する必要があります。1 つのカテゴリを gridView に設定できます (たとえば、すべてのカテゴリ)。他のカテゴリを選択すると、gridView が更新されませんでした。gridView を更新するにはどうすればよいですか? 助けてください。
アクティビティに 3 つの gridView を配置し、それらの可視性を削除して、アダプターの設定時に表示できるようにしてみました (3 つの個別のアダプターを使用しましたが、機能しませんでした。
私のコードを以下に示します。
public class MainActivity extends Activity {
Spinner vsel;
ArrayList<String> vimage = new ArrayList<String>();
ArrayList<String> all = new ArrayList<String>();
ArrayList<String> himage = new ArrayList<String>();
ArrayList<Integer> allid = new ArrayList<Integer>();
ArrayList<Integer> hid = new ArrayList<Integer>();
ArrayList<Integer> vid = new ArrayList<Integer>();
ArrayList<Integer> tot = new ArrayList<Integer>();
ArrayList<String> spinner = new ArrayList<String>();
GridView gv,sgv,gv1,gv2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gv = (GridView) findViewById(R.id.gridView1);
gv1 = (GridView) findViewById(R.id.gridView2);
gv2 = (GridView) findViewById(R.id.gridView3);
Class resources = R.drawable.class;
java.lang.reflect.Field[] fields = resources.getFields();
for(java.lang.reflect.Field field : fields)
{
try {
if(field.getName().contains("split"))
{
all.add(field.getName());
allid.add(field.getInt(fields));
}
if(field.getName().contains("splith"))
{
//String iname = field.getName();
himage.add(field.getName());
hid.add(field.getInt(fields));
}
if(field.getName().contains("splitv"))
{
vimage.add(field.getName());
vid.add(field.getInt(fields));
}
/*else
{
all.add(field.getName());
allid.add(field.getInt(fields));
}*/
}
catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("All arraylist-> " + allid);
System.out.println("Horizontal arraylist-> " + hid);
System.out.println("Vertical arraylist-> " + vid);
// String[] viewsel = {"All","Vertical","Horizontal"};
spinner.add("All");
spinner.add("Vertical");
spinner.add("Horizontal");
vsel = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> sadpater = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinner);
sadpater.setDropDownViewResource(android.R.layout.simple_spinner_item);
vsel.setAdapter(sadpater);
vsel.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//String item = parent.getItemAtPosition(position).toString();
String item = parent.getSelectedItem().toString();
if(item.equals("All"))
{
gv2.setVisibility(View.INVISIBLE);
gv1.setVisibility(View.INVISIBLE);
Toast.makeText(MainActivity.this,item,Toast.LENGTH_SHORT).show();
gv.setVisibility(View.VISIBLE);
gv.setAdapter(new AppsAdapter(MainActivity.this,allid));
}
if(item.equals("Vertical"))
{
gv.setVisibility(View.INVISIBLE);
gv2.setVisibility(View.INVISIBLE);
gv1.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.this,item,Toast.LENGTH_SHORT).show();
gv1.setAdapter(new AppsAdapter1(MainActivity.this,vid));
}
if(item.equals("Horizontal"));
{
gv.setVisibility(View.INVISIBLE);
gv1.setVisibility(View.INVISIBLE);
Toast.makeText(MainActivity.this,item,Toast.LENGTH_SHORT).show();
gv2.setVisibility(View.VISIBLE);
gv2.setAdapter(new AppsAdapter3(MainActivity.this,hid));
}
}
public void onNothingSelected(AdapterView<?> view) {
// TODO Auto-generated method stub
}
});
}
public class AppsAdapter extends BaseAdapter {
private Context context;
ArrayList<Integer> dirsTemp = new ArrayList<Integer>();
public AppsAdapter(Context c,ArrayList<Integer> allid)
{
Log.d("In appsadapter1 all","Hii");
dirsTemp = allid;
context = c;
}
//---returns the number of images---
public final int getCount() {
return dirsTemp.size();
}
//---returns the ID of an item---
public final Object getItem(int position) {
return dirsTemp.get(position);
}
public final long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//final File f = (File) getItem(position);
int i = (Integer) getItem(position);
System.out.println("Dirstemp->"+dirsTemp);
ImageView iv;
if (convertView == null)
{
iv = new ImageView(context);
//iv.setPadding(5, 5, 5, 5);
}
else
{
iv = (ImageView) convertView;
}
iv.setImageResource(dirsTemp.get(position));
return iv;
}
}
public class AppsAdapter1 extends BaseAdapter {
private Context context1;
ArrayList<Integer> dirsTemp1 = new ArrayList<Integer>();
public AppsAdapter1(Context c,ArrayList<Integer>vid) {
Log.d("In appsadapter1 vertical","Hii");
dirsTemp1 = vid;
context1 = c;
}
//---returns the number of images---
public final int getCount() {
return dirsTemp1.size();
}
//---returns the ID of an item---
public final Object getItem(int position) {
return dirsTemp1.get(position);
}
public final long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//final File f = (File) getItem(position);
int i = (Integer) getItem(position);
ImageView iv1;
if (convertView == null)
{
iv1 = new ImageView(context1);
//iv.setPadding(5, 5, 5, 5);
}
else
{
iv1 = (ImageView) convertView;
}
iv1.setImageResource(dirsTemp1.get(position));
return iv1;
}
}
public class AppsAdapter3 extends BaseAdapter {
private Context context2;
ArrayList<Integer> dirsTemp2 = new ArrayList<Integer>();
public AppsAdapter3(Context c,ArrayList<Integer>hid) {
Log.d("In appsadapter1 horizontal","Hii");
dirsTemp2 = hid;
context2 = c;
}
//---returns the number of images---
public final int getCount() {
return dirsTemp2.size();
}
//---returns the ID of an item---
public final Object getItem(int position) {
return dirsTemp2.get(position);
}
public final long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//final File f = (File) getItem(position);
int i = (Integer) getItem(position);
ImageView iv2;
if (convertView == null)
{
iv2 = new ImageView(context2);
//iv.setPadding(5, 5, 5, 5);
}
else
{
iv2 = (ImageView) convertView;
}
iv2.setImageResource(dirsTemp2.get(position));
return iv2;
}
}
}