0

向所有伟大的开发者和帮助者致敬。我需要一些帮助。我有定义图标的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;
}

}
4

1 回答 1

0
  1. 要添加TextView,您可以创建自己的布局并getView在适配器的方法中动态扩展它。
  2. 要管理链接部分,您可以拥有一个字符串数组,并且在方法中您可以使用参数onItemClick获取特定的 url并根据需要使用它position

-编辑-

xml 你可以使用的TextView部分

my_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <ImageView
        android:id="@+id/appIcon"
        android:layout_height="60dp"
        android:layout_width="60dp"
        android:src="@drawable/albumstack1"
        android:layout_centerHorizontal="true"
        />
    <TextView
        android:id="@+id/appNameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/appIcon"
        android:layout_centerHorizontal="true" 
        android:gravity="center_horizontal"
        android:textIsSelectable="false"
        android:text="My Application name"
        />
</RelativeLayout>

您可以在 getView 方法中使用

LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public View getView(int position, View convertView, ViewGroup parent) {
     View v = convertView;
        if (v == null) {
            v = li.inflate(R.layout.my_view, null);
           ((TextView)v.findViewById(R.id.appNameTextView)).setText("put your text here");
            ((ImageView)v.findViewById(R.id.appIcon)).setImageResource(mThumbIds[position]);
    }
    return v;
}

对于您在问题中有 listHttp 的链接部分,您可以在

gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
        /**your code**/
        //This is the string you need for the url, do whatever you want to do with this string
        String urlForApp = listHttp.get(position);
    }
}
于 2013-03-07T08:12:36.750 回答