0

有人请帮我在 Android Grid View 中放置一个图像(从 URL 的网格视图中显示图像)和 Edittextbox

这是我的网格视图 xml 布局:

  <TextView 
  android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 />

 <GridView 
 android:id="@+id/order_details_grid" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:numColumns="4" 
 android:columnWidth="100px" 
 android:stretchMode="columnWidth" 
 android:gravity="center"/> 
 </LinearLayout>

下面是我用来填充网格的代码,它工作正常,我需要放置来自 url 和 edittextbox 的图像:

    super.onCreate(savedInstanceState); 
  setContentView(R.layout.order_details);
  File yourFile = new File("/sdcard/json/orderdetails.txt");
  FileInputStream stream = null;
try {
    stream = new FileInputStream(yourFile);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
   String jString = "";
  try {
      FileChannel fc = stream.getChannel();
      MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());


      /* Instead of using default, pass in a decoder.*/ 
      jString = Charset.defaultCharset().decode(bb).toString();

  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
  finally {
      try {
        stream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

  String fileContent=jString;
  JSONObject jobj = null;
  JSONObject jobj1 = null;
try {
    jobj = new JSONObject(fileContent);
} catch (JSONException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

try {

    JSONArray ja = jobj.getJSONArray("ORDERDETAILS");   

    listContent.add("ITEM NAME");
    listContent.add("ORDER QUANTITY");
    listContent.add("PRICE");
    listContent.add("PREFERED SHOP");
    for(int i=0; i<ja.length(); i++){              
           String ORDERDETAILS = ja.getString(i);
           System.out.println(ORDERDETAILS);               
           jobj1 = new JSONObject(ORDERDETAILS);
           String ITEMNAME = jobj1.getString("ITEMNAME");          
           listContent.add(ITEMNAME);
           System.out.println(ITEMNAME);
           String ORDERQUANTITY = jobj1.getString("ORDERQUANTITY");            
           listContent.add(ORDERQUANTITY);
           System.out.println(ORDERQUANTITY);
           String PRICE = jobj1.getString("PRICE");          
           listContent.add(PRICE);             
           System.out.println(PRICE);
           String SHOPNAME = jobj1.getString("SHOPNAME");
           System.out.println(SHOPNAME);             
           listContent.add(SHOPNAME);

    }

} catch (JSONException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}    
  gridView = (GridView)findViewById(R.id.order_details_grid);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>      (this,android.R.layout.simple_list_item_1,listContent); 
  gridView.setAdapter(adapter);
 // gridView.setAdapter(new ImageAdapter(this));

}

这是我从中获取 json 数据的 json 文件:

           {
"ORDERDETAILS": [
    {
        "ITEMNAME": "APPLE-LAPTOP",
        "ORDERQUANTITY": "35",
        "PRICE": "4500 DHMS",
        "SHOPNAME": "Indico Icon Kits 403",
        "SHOPIMAGEURL": "http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg"
    },
    {
        "ITEMNAME": "ASUS-NOTEBOOK",
        "ORDERQUANTITY": "35",
        "PRICE": "3500 DHMS",
        "SHOPNAME": "Indico Icon Kits 403",
        "SHOPIMAGEURL": ""
    },
   {
        "ITEMNAME": "DELL-LAPTOP",
        "ORDERQUANTITY": "35",
        "PRICE": "9500 DHMS",
        "SHOPNAME": "Indico Icon Kits 403",
        "SHOPIMAGEURL": ""
    }
  ]
  }
4

2 回答 2

1

不要使用ArrayAdapter, 创建一个扩展BaseAdapter来创建您自己的自定义适配器的类。

然后在 getView 方法中,为网格的每个单元格添加一个 xml 布局(您可以在其中放置 TextView、EditText、图像或任何您想要的东西。)

覆盖的 getView 方法如下所示:

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {


   View cell;
   if(convertView==null){
    cell= new View(context);
    //create a file "cell_layout.xml" in your layout folder to define the layout for each cell
    cell= layoutInflater.inflate(R.layout.cell_layout, null);
   }else{
    cell= (View)convertView;
   }

   ImageView imageView = (ImageView)cell.findViewById(R.id.image);
   imageView.setImageBitmap(bitmap[position]);

   TextView textView = = (TextView)cell.findViewById(R.id.text);
   textView .setText("hello world from gridView");
   return cell;
  }

可以在此处找到更完整 的示例,但可以通过谷歌搜索“自定义适配器”找到很多示例

从 url 加载图片的问题是另一个话题,Armaan 的回答似乎不错。我建议你看看GreenDroid 项目,尤其是我经常使用的AsynImageView

于 2013-07-24T08:16:42.937 回答
1

检查以下链接并下载对您有帮助的示例应用程序

https://github.com/nostra13/Android-Universal-Image-Loader

于 2013-07-22T12:11:10.027 回答