我是android开发的初学者,我创建了一个gridView并为它编写了一个适配器,我在片段中使用了这个网格视图!我从我的片段中获取服务器中的数据,这是2个字符串数组,如何使用我的适配器中的数据显示在gridview中? 提前谢谢!
String[] gridViewPicture;
String[] gridViewText;
//some codes to get my data from server
gridViewText[i] =jsonArray.getJSONObject(i).get("name").toString();
gridViewPicture[i] =jsonArray.getJSONObject(i).get("picture").toString();
这是我的GridView适配器:
class GridItem{
int imageSource;
String Text;
GridItem(int imageSource,String text)
{
this.imageSource=imageSource;
this.Text=text;
}
}
public class MyGridViewAdapter extends BaseAdapter {
ArrayList<GridItem> list;
Context context;
public MyGridViewAdapter(Context context){
this.context=context;
list = new ArrayList<GridItem>();
Resources contextResources = context.getResources();
String[] foodText= {"text1","text2","text3"}; //this is the text that should change and get it from server
int[] image={R.drawable.first_slider_1min,R.drawable.first_slider_2min,R.drawable.first_slider_3min};//this is the image that should change and get it from server
for(int i=0;i<foodText.length;i++)
{
GridItem gridItem= new GridItem(image[i],foodText[i]);
list.add(gridItem);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder{
ImageView myfoodImage;
TextView myfoodText;
ViewHolder(View view){
myfoodImage=(ImageView) view.findViewById(R.id.GridLayoutPhoto);
myfoodText=(TextView) view.findViewById(R.id.GridLayoutName);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if(row==null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row= inflater.inflate(R.layout.grid_layout,parent,false);
holder = new ViewHolder(row);
row.setTag(holder);
}
else
{
holder = (ViewHolder) row.getTag();
}
GridItem myGridItem = list.get(position);
holder.myfoodText.setText(myGridItem.Text);
holder.myfoodImage.setImageResource(myGridItem.imageSource);
return row;
}
}
答案 0 :(得分:1)
像这样更改Gridview适配器并从片段
传递数据 String[] foodText ;
int[] image ;
public MyGridViewAdapter(Context context , String[] foodText ,int[] image){
this.context=context;
this.foodText=foodText;
this.image=image;
list = new ArrayList<GridItem>();
Resources contextResources = context.getResources();
for(int i=0;i<foodText.length;i++)
{
GridItem gridItem= new GridItem(image[i],foodText[i]);
list.add(gridItem);
}
}
这里需要以这种方式将数据传递给适配器
MyGridViewAdapter adapter = new MyGridViewAdapter(ActivityContext ,gridViewText , gridViewPicture);
listview.setAdapter(adapter);
以其他方式将数据设置为适配器之前,您可以准备GridItem数据,然后直接将其传递给适配器。
喜欢这个
String[] gridViewPicture;
String[] gridViewText;
//some codes to get my data from server
gridViewText[i] =jsonArray.getJSONObject(i).get("name").toString();
gridViewPicture[i] =jsonArray.getJSONObject(i).get("picture").toString();
ArrayList<GridItem> list = new ArrayList<GridItem>;
int[] image={R.drawable.first_slider_1min,R.drawable.first_slider_2min,R.drawable.first_slider_3min};//this is the image that should change and get it from server
for(int i=0;i<foodText.length;i++)
{
GridItem gridItem= new GridItem(gridViewPicture[i],gridViewText[i]);
list.add(gridItem);
}
那么你的适配器应该是这样的
public class MyGridViewAdapter extends BaseAdapter {
ArrayList<GridItem> list;
Context context;
public MyGridViewAdapter(Context context, ArrayList<GridItem> list){
this.context=context;
this.list =list;
}
在第二种情况下,您可以直接将数据传递给适配器。
MyGridViewAdapter adapter = new MyGridViewAdapter(ActivityContext ,list );
listview.setAdapter(adapter);