我想问一下,如果我想在运行时添加图像我该怎么办?...我的意思是我有一个国家列表,其中给出了名称和标志。我创建列表(并设置在适配器中)但我不知道如何相对于其国家/地区添加图像
enter code here
package com.example.hp.myapplication;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView lvProduct;
private ProductListAdapter adapter;
private List<Country> mProductList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvProduct = (ListView)findViewById(R.id.listview_product);
mProductList = new ArrayList<>();
Uri path = Uri.parse("android.resource://"+this.getPackageName () + R.drawable.afghanistan);
String Path = path.toString();
Uri path1 = Uri.parse("android.resource://"+this.getPackageName () + R.drawable.portugal);
String Path1 = path1.toString();
//Add sample data for list
//We can get data from DB, webservice here
mProductList.add(new Country(1, "iPhone4", 200, "Apple iPhone4 16GB",Path));
mProductList.add(new Country(3, "iPhone4S", 250, "Apple iPhone4S 16GB",Path1));
mProductList.add(new Country(4, "iPhone5", 300, "Apple iPhone5 16GB",Path));
mProductList.add(new Country(5, "iPhone5S", 350, "Apple iPhone5S 16GB",Path1));
mProductList.add(new Country(6, "iPhone6", 400, "Apple iPhone6 16GB",Path));
mProductList.add(new Country(7, "iPhone6S", 450, "Apple iPhone6S 16GB",Path1));
mProductList.add(new Country(8, "iPhone7", 500, "Apple iPhone7 16GB",Path));
mProductList.add(new Country(9, "iPhone7S", 600, "Apple iPhon7S 16GB",Path1));
mProductList.add(new Country(10, "iPhone8", 700, "Apple iPhone8 16GB",Path));
mProductList.add(new Country(11, "iPhone8S", 800, "Apple iPhone8S 16GB",Path1));
//Init adapter
adapter = new ProductListAdapter(getApplicationContext(), mProductList);
lvProduct.setAdapter(adapter);
lvProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do something
//Ex: display msg with product id get from view.getTag
Toast.makeText(getApplicationContext(), "Clicked product id =" + view.getTag(), Toast.LENGTH_SHORT).show();
}
});
}
}
enter code here
package com.example.hp.myapplication;
/**
* Created by HP on 16-Feb-17.
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class ProductListAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private List<Country> mProductList;
//Constructor
public ProductListAdapter(Context mContext, List<Country> mProductList) {
this.mContext = mContext;
// mProductList=new ArrayList<Country>();
mInflater = LayoutInflater.from(mContext);
this.mProductList = mProductList;
}
@Override
public int getCount() {
return mProductList.size();
}
@Override
public Object getItem(int position) {
return mProductList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
Country p = mProductList.get(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.tv_name);
holder.price= (TextView) convertView.findViewById(R.id.tv_price);
holder.Description= (TextView) convertView.findViewById(R.id.tv_description);
holder.img= (ImageView) convertView.findViewById(R.id.IMAGE);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
int number=p.getPrice();
String numberAsString = Integer.toString(number);
holder.name.setText(p.getName());
holder.price.setText(numberAsString);
holder.Description.setText(p.getDescription());
holder.img.setImageResource(R.drawable.spain);
return convertView;
}
static class ViewHolder
{
TextView name;
TextView price;
TextView Description;
ImageView img;
}
}
enter code here
package com.example.hp.myapplication;
/**
* Created by HP on 16-Feb-17.
*/
public class Country {
private int id;
private String name;
private int price;
private String description;
private String ImageUri;
//Constructor
public Country(int id, String name, int price, String description,String uri)
{
this.id = id;
this.name = name;
this.price = price;
this.description = description;
this.ImageUri=uri;
}
//Setter, getter
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage()
{
return ImageUri;
}
}
答案 0 :(得分:1)
简单的解决方案可能是将资源ID添加到您的Country类:
mProductList.add(new Country(11, "iPhone8S", 800, "Apple iPhone8S 16GB",R.drawable.portugal));
public Country(int id, String name, int price, String description,id resId){
...
this.resId=resId;
}
public String getResId(){
return resId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
Country p = mProductList.get(position);
....
holder.img.setImageResource(p.getResId);
return convertView;
}
答案 1 :(得分:0)
正如FierceFox所解释的那样,您为每个项目设置了以下图像:
echo "<div>".$this->formLabel($form->get('name'))."</div>";
echo "<div>".$this->formInput($form->get('name'))."</div>";
您想要的是设置国家/地区的图像。
holder.img.setImageResource(R.drawable.spain);
您需要在此处完全删除URI:
holder.img.setImageResource(country.getDrawableId());
并将其替换为drawable id:
mProductList.add(new Country(1, "iPhone4", 200, "Apple iPhone4 16GB",Path));
我不知道你要对这个URI做什么,因为你可以看到mProductList.add(new Country(1, "iPhone4", 200, "Apple iPhone4 16GB", R.drawable.portugal));
需要setImageResource
,这意味着你应该只给它一个可绘制的资源ID,不需要存储任何URI。
答案 2 :(得分:0)
我在这里讲基础知识的步骤。
现在在您的Country对象中,只需为资源ID添加一个参数,如下所示。
MyCountryClass {
// user crud controller
public function getUserRlatedPosts($user_id)
{
$crud = new CrudPanel();
$crud->addClause('where', 'user_id', '=', $user_id);
$crud->setModel("post");
$crud->setEntityNameStrings("post","posts");
$crud->enableAjaxTable();
$this->data['crud'] = $crud;
$this->data['title'] = ucfirst($this->crud->entity_name_plural);
if (! $this->data['crud']->ajaxTable()) {
$this->data['entries'] = $this->data['crud']->getEntries();
}
return view('crud::list', $this->data);
}
现在在适配器上,这样做。
int countryCode;
String countryName;
int countryResId;
}
现在在活动类中,只需准备好您的列表并使用列表设置适配器。