当前位置: 首页 > 知识库问答 >
问题:

Android使用GridView尝试调用虚拟方法集适配器

华建同
2023-03-14
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.nusecond.suredeal.app.R;
import com.nusecond.suredeal.app.suredeal.adapter.CustomAdapter;
import com.nusecond.suredeal.app.suredeal.adapter.CustomPromoAdapter;

import java.util.ArrayList;

public class Promo extends AppCompatActivity {
    GridView gv;
    Context context;
    ArrayList programName;
    public static String[] programNameList={"AppleMacBook","HP_note_Book","LG_NEXUS","NokiaLumia","SamsungRT","SONY_BRAVIA","Sansui"};
    public static int [] programImages={R.drawable.apple,R.drawable.hp,R.drawable.nexus,R.drawable.lumia,R.drawable.fridge,R.drawable.tv,R.drawable.tv1};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gv=(GridView)findViewById(R.id.gridView1);
        //gv.setAdapter(new CustomPromoAdapter(this,programNameList,programImages));
        gv.setAdapter(new CustomPromoAdapter(this,programNameList,programImages));

    }
}
  1. 这是我在GridView中显示图像和文本的活动。
  2. 我将在此的其他活动中使用Intent。
  3. 使用setAdapter方法出错。
  4. 下面是我的适配器。
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 android.widget.Toast;

import com.nusecond.suredeal.app.R;
import com.nusecond.suredeal.app.suredeal.activity.MainActivity;
import com.nusecond.suredeal.app.suredeal.activity.Promo;

/**
 * Created by ns2 on 2/4/16.
 */
public class CustomPromoAdapter extends BaseAdapter{
    ImageView imageView;
    TextView textView;
    String [] result;
    Context context;
    int [] imageId;
    private static LayoutInflater inflater=null;
    public CustomPromoAdapter(Promo promo, String[] programNameList, int[] programImages) {
        // TODO Auto-generated constructor stub
        result=programNameList;
        context=promo;
        imageId=programImages;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        view=inflater.inflate(R.layout.promo_gridlist,null);
        textView=(TextView)view.findViewById(R.id.textView1);
        imageView=(ImageView)view.findViewById(R.id.imageView1);
        imageView.setImageResource(imageId[position]);
        textView.setText(imageId[position]);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }
}
  1. 这是我的适配器我不知道哪里出了问题。
  2. 启动此活动时,我的应用程序崩溃。

共有1个答案

司空俊悟
2023-03-14

试试这个,

 CustomPromoAdapter adapter = new CustomPromoAdapter(this,programNameList,programImages)
    gv.setAdapter(adapter);
 类似资料: