在我的主要活动中,我有一个微调器,它包含三个类别(全部,水平和垂直),下面有一个gridView。我在资源文件夹中有一些图像,用于垂直和水平模式。我需要根据微调器上的选定项目将图像设置为gridView。我能够将一个类别设置为gridView(例如所有类别)。当选择其他类别时,gridView没有更新。要更新gridView,我该怎么办?请帮助我?
我尝试使用在我的活动中放置三个gridViews并将其可见性设置为已消失,并在设置适配器时使其可见(我使用了三个独立的适配器但无法正常工作。
我的代码如下。
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;
}
}
}
答案 0 :(得分:1)
您不需要三个gridView和三个适配器。一个就够了。从微调器更改类别时,刷新列表allid, vid, hid
(用于适配器以显示gridview),然后刷新适配器上的notifyDataSetChanged()
。
编辑:试试这个:
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
public class ddsds 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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gv = (GridView) findViewById(R.id.gridView1);
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);
ArrayList<String> list ;
AppsAdapter adapter = null;
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"))
{
list = allid;
}
if(item.equals("Vertical"))
{
list = vid;
}
if(item.equals("Horizontal"));
{
list = hid));
}
if (adapter = null ) {
adapter = new AppsAdapter(MainActivity.this, list);
gv.setAdapter(adapter);
}
adapter.notifyDataSetChanged();
}
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;
}
}
}
}