向所有伟大的开发者和帮助者致敬。我需要一些帮助。我有定义图标的gridview。(它们存储在应用程序中,我不需要从互联网上下载它们)。
第一个问题 - 如何将 textview(即图标的名称)添加到现有的 gridview 适配器?
第二个问题(最重要,首先要解决) - 我如何为每个项目添加指定的 url 并单击它,用户将下载指定的文件?(例如 - 我的 gridview 显示皮肤,单击它用户将能够下载他选择的皮肤。)我尝试了很多方法,找不到解决方案。像这样的东西:
public void prepareList()
{
listHttp = new ArrayList();
listHttp.add("http://yoursite/file1.apk");
listHttp.add("http://yoursite/file2.apk");
listHttp.add("http://yoursite/file3.apk");
SkinsActivity.class
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import java.util.ArrayList;
public class SkinsActivity extends Activity {
com.htc.widget.HtcAlertDialog.Builder builder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
HtcAlertDialog.Builder alertDialog = new HtcAlertDialog.Builder(SkinsActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Confirm Installation");
// Setting Dialog Message
alertDialog.setMessage("Do you want to install this skin?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.ic_about);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to invoke YES event
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
});
}
ImageAdapter.class (Gridviewadapter)
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.sense5, R.drawable.sense5red,
R.drawable.concrete, R.drawable.graphite,
R.drawable.sense5_full,
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(250, 180));
return imageView;
}
}