将影像(从网址下载),并编辑文本框在网格视图中的Andr​​oid网格、视图、文本框、影像

2023-09-04 03:45:28 作者:伊依.

有个人请帮我把一个图像(显示图像的URL网格视图)和Edittextbox Android中网格视图

这是我网格视图XML布局:

 <的TextView
  机器人:layout_width =FILL_PARENT
 机器人:layout_height =WRAP_CONTENT
 />

 < GridView控件
 机器人:ID =@ + ID / order_details_grid
 机器人:layout_width =FILL_PARENT
 机器人:layout_height =FILL_PARENT
 机器人:为numColumns =4
 机器人:columnWidth中=100像素
 机器人:stretchMode =columnWidth中
 机器人:重力=中心/>
 < / LinearLayout中>
 

下面是code,我用它来填充网格,它工作正常,我需要把图像从URL和与edittextbox到来:

  super.onCreate(savedInstanceState);
  的setContentView(R.layout.order_details);
  文件yourFile =新的文件(/ SD卡/ JSON / orderdetails.txt);
  的FileInputStream流= NULL;
尝试 {
    流=新的FileInputStream(yourFile);
}赶上(FileNotFoundException异常E){
    // TODO自动生成的catch块
    e.printStackTrace();
}
   字符串的jstring =;
  尝试 {
      FileChannel FC = stream.getChannel();
      MappedByteBuffer BB = fc.map(FileChannel.MapMode.READ_ONLY,0,fc.size());


      如果不使用默认的/ *,通过在德codeR。* /
      的jstring = Charset.defaultCharset()去code(BB)的ToString()。

  }赶上(IOException异常E){
    // TODO自动生成的catch块
    e.printStackTrace();
}
  最后 {
      尝试 {
        stream.close();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
    }

  字符串fileContent =的jstring;
  JSONObject的jobj = NULL;
  JSONObject的jobj1 = NULL;
尝试 {
    jobj =新的JSONObject(fileContent);
}赶上(JSONException E1){
    // TODO自动生成的catch块
    e1.printStackTrace();
}

尝试 {

    JSONArray JA = jobj.getJSONArray(订单明细);

    listContent.add(条目名称);
    listContent.add(订单数量);
    listContent.add(代价);
    listContent.add(preFERED店);
    的for(int i = 0; I< ja.length();我++){
           串订单明细= ja.getString(ⅰ);
           的System.out.println(订单明细);
           jobj1 =新的JSONObject(订单明细);
           ItemName字符串= jobj1.getString(了itemname);
           listContent.add(了itemname);
           的System.out.println(了itemname);
           串ORDERQUANTITY = jobj1.getString(ORDERQUANTITY);
           listContent.add(ORDERQUANTITY);
           的System.out.println(ORD​​ERQUANTITY);
           字符串PRICE = jobj1.getString(代价);
           listContent.add(价);
           的System.out.println(价);
           串SHOPNAME = jobj1.getString(SHOPNAME);
           的System.out.println(SHOPNAME);
           listContent.add(SHOPNAME);

    }

}赶上(JSONException E1){
    // TODO自动生成的catch块
    e1.printStackTrace();
}
  GridView控件=(GridView控件)findViewById(R.id.order_details_grid);
  ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串> (这一点,android.R.layout.simple_list_item_1,的listContent);
  gridView.setAdapter(适配器);
 // gridView.setAdapter(新ImageAdapter(本));
 

}

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

  {
订单详细信息: [
    {
        了itemname:苹果笔记本电脑,
        ORDERQUANTITY:35,
        价格:4500 DHMS
        SHOPNAME:编号Indico图标套件403,
        SHOPIMAGEURL:http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg
    },
    {
        了itemname:华硕笔记本,
        ORDERQUANTITY:35,
        价格:3500 DHMS
        SHOPNAME:编号Indico图标套件403,
        SHOPIMAGEURL:
    },
   {
        了itemname:戴尔笔记本电脑,
        ORDERQUANTITY:35,
        价格:9500 DHMS
        SHOPNAME:编号Indico图标套件403,
        SHOPIMAGEURL:
    }
  ]
  }
 

解决方案

不要使用 ArrayAdapter ,创建可扩展的类 BaseAdapter 以创建自己的自定义适配器。

然后在getView方法,夸大为网格的每个单元一个XML布局(你可以把TextViews,EditText上,图像或任何你想在里面。)

在被覆盖的getView方法如下:

  @覆盖
  公共查看getView(INT位置,查看convertView,ViewGroup中父){


   查看电池;
   如果(convertView == NULL){
    电池=新景(背景);
    //你的布局文件夹中创建一个文件cell_layout.xml明确了布局的每个细胞
    电池= layoutInflater.inflate(R.layout.cell_layout,NULL);
   }其他{
    细胞=(查看)convertView;
   }

   ImageView的ImageView的=(ImageView的)cell.findViewById(R.id.image);
   imageView.setImageBitmap(位图[位置]);

   的TextView TextView的= =(TextView中)cell.findViewById(R.id.text);
   TextView的.setText(你好,从GridView的世界);
   返回细胞;
  }
 

一个更完整的例子可以找到的这里但很多的例子可以通过谷歌搜索自定义适配器中找到

加载来自URL的图片的问题是另外一个话题,从Armaan答案似乎不错。我建议你​​看看在 GreenDroid项目尤其它的 AsynImageView ,我用了很多东西。

Some one please help me to place an Image ( display image in the grid view from URL) and Edittextbox in Android Grid View

This is my grid view xml lay out:

  <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>

below is the code which i use to populate the grid and it is working fine , i need to place the image coming from the url and and 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));

}

This is my json file from which i fetched the json data:

           {
"ORDERDETAILS": [
    {
        "ITEMNAME": "APPLE-LAPTOP",
        "ORDERQUANTITY": "35",
        "PRICE": "4500 DHMS",
        "SHOPNAME": "Indico Icon Kits 403",
        "SHOPIMAGEURL": "https://m.xsw88.com/allimgs/daicuo/20230904/7104.png.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": ""
    }
  ]
  }

解决方案

Don't use an ArrayAdapter, create a class that extends BaseAdapter to create your own custom adapter.

Then in the getView method, inflate an xml layout for each cell of your grid ( your can place TextViews, EditText, images or whatever you want in it.)

The overriden getView method will look like :

  @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;
  }

A more complete example can be find here but a lot of examples can be found by googling "Custom Adapter"

The problem of loading a picture from an url is another topic, the answer from Armaan seems good. I would suggest you to have a look at the GreenDroid project and particularly it AsynImageView that I used a lot.