デモのアンドロイドアプリを開発中です。現在、私は表示している製品のリストを持っていますが、それらはすべて同じイメージを持ち、生成されたテキストもJavaメソッドで生成されたランダムな文字です。言うまでもなく、それはとてもよく見えません。Android:画像とテキストが異なるランダムなデータを追加
アダプターを使用してデータを表示しています。 efficiently
ランダムな画像(URLまたは選択したフォルダのいずれか)を使用して、一部のXMLファイルの名前を使用してUIをより見やすくするにはどうすればよいですか。
コード:
public class Products extends Activity{
ProductAdapter productAdapter;
List<Product> productList = new ArrayList<>();
private SecureRandom random = new SecureRandom();
public static final String productName = "productName";
public static final String productComments = "productComments";
public static final String productLikes = "productLikes";
public static final String productDescription = "productDescription";
public static final String userName = "userName";
ListView productsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
for(int i=1;i<100;i++){
Product product = createProduct();
productList.add(product);
}
ArrayList<HashMap<String, String>> productsArrayHashList = new ArrayList<>();
for (Product product : productList){
HashMap<String,String> productDisplay = new HashMap<>();
productDisplay.put(productName,product.getName());
productDisplay.put(productComments,String.valueOf(product.getCommentCount()));
productDisplay.put(productLikes,String.valueOf(product.getLikes()));
productDisplay.put(productDescription,product.getDescription());
productDisplay.put(userName,new BigInteger(130, random).toString(32));
productsArrayHashList.add(productDisplay);
}
productsList = (ListView) findViewById(R.id.productList);
productAdapter = new ProductAdapter(this,productsArrayHashList);
productsList.setAdapter(productAdapter);
}
public Product createProduct(){
int minimum = 1;
int maxValue = 20;
Product product = new Product();
product.setCommentCount(minimum + random.nextInt(maxValue - minimum + 1));
product.setLikes(minimum + random.nextInt(maxValue - minimum + 1));
product.setSaveDate(new Timestamp(System.currentTimeMillis()));
product.setDescription(new BigInteger(130, random).toString(32));
product.setName(new BigInteger(130, random).toString(32));
product.setSize(new BigInteger(130, random).toString(32));
if(getRandomBoolean()) {
product.setSold(true);
}else {
product.setSold(false);
}
return product;
}
アダプターコード:
public class ProductAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
private SecureRandom random = new SecureRandom();
public ProductAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null) {
view = inflater.inflate(R.layout.product_row, null);
TextView productName = (TextView) view.findViewById(R.id.productName);
TextView userUploader = (TextView)view.findViewById(R.id.uploadingUser);
RatingBar ratingBar = (RatingBar)view.findViewById(R.id.productStars);
TextView comments = (TextView) view.findViewById(R.id.comments);
TextView productDetails = (TextView) view.findViewById(R.id.description);
ImageView prodImage = (ImageView) view.findViewById(R.id.productImage);
prodImage.setImageResource(R.drawable.productwatch);
HashMap<String, String> productList;
productList = data.get(position);
productName.setText(productList.get(Products.productName));
comments.setText("Comments("+productList.get(Products.productComments)+")");
productDetails.setText(productList.get(Products.productDescription));
userUploader.setText(productList.get(Products.userName));
int minimum = 1;
int maxValue = 5;
ratingBar.setRating((float) (minimum + random.nextInt(maxValue - minimum + 1)));
}
return view;
}
}
ありがとうございました。
私はPicasoを使用するとき、私はいくつかのツリーイメージを取得します。私はスレッドを使用し、ランダムな画像を取得しました。ありがとうございました。タグとテキストの提案はありますか? –
そのWebサイトに移動し、そこにカテゴリがあり、そのURLを使用します。タグの場合は、ランダム化関数を使うことができます。テキストの場合は、単語をコンマで区切ってコピーし、文字列配列に入れます。索引をランダム化して文字列を取得します。 – dustblue