実行時に画像を追加したいのですがどうすればいいですか?...名前とフラグのある国のリストgiven.Iのリストを作成して(そしてアダプタに設定された)が、私はその国にイメージをランタイムでアンドロイドで設定する方法
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;
}
}
常にR.drawable.spainをドロウアブルとして設定しています。それは問題かもしれない...? – FlanschiFox
うん、それは分かっていますが、望ましい出力に変更する方法はありますか? –