2 whynofly whynofly 于 2016.05.04 18:09 提问

关于BaseAdapter的一个小程序的问题

额,代码比较长...................
public class Main extends ListActivity
{
private static String[] applicationNames = new String[]
{ "多功能日历", "eoeMarket客户端", "耐玩的重力消砖块", "白社会", "程序终结者" };
private static String[] authors = new String[]
{ "李宁", "eoemobile", "wawa", "ApkHome", "lucyfa" };
private static int[] resIds = new int[]
{ R.drawable.calendar, R.drawable.eoemarket, R.drawable.brick,
R.drawable.whitesociety, R.drawable.terminater };
private static float[] applicationRating = new float[]
{ (float) 5.0, (float) 5.0, (float) 3.5, (float) 5.0, (float) 4.0 };

String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater;
private RatingAdapter raAdapter;

private class RatingAdapter extends BaseAdapter
{
private Context context;

    public RatingAdapter(Context context)
    {
        this.context = context;
        layoutInflater = (LayoutInflater) context
                .getSystemService(inflater);
    }

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

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

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

    public void setRating(int position, float rating)
    {
        applicationRating[position] = rating;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(
                R.layout.main, null);
        ImageView ivLogo = (ImageView) linearLayout
                .findViewById(R.id.ivLogo);
        TextView tvApplicationName = ((TextView) linearLayout
                .findViewById(R.id.tvApplicationName));
        TextView tvAuthor = (TextView) linearLayout
                .findViewById(R.id.tvAuthor);
        TextView tvRating = (TextView) linearLayout
                .findViewById(R.id.tvRating);
        RatingBar ratingBar = (RatingBar) linearLayout
                .findViewById(R.id.ratingbar);

        ivLogo.setImageResource(resIds[position]);
        tvApplicationName.setText(applicationNames[position]);
        tvAuthor.setText(authors[position]);
        tvRating.setText(String.valueOf(applicationRating[position]));
        ratingBar.setRating(applicationRating[position]);
        return linearLayout;
    }
}

@Override
protected void onListItemClick(ListView l, View view, final int position,
long id)
{
View myView = getLayoutInflater().inflate(R.layout.rating, null);
final RatingBar ratingBar = (RatingBar) myView
.findViewById(R.id.ratingbar);
ratingBar.setRating(applicationRating[position]);
new AlertDialog.Builder(this).setTitle(applicationNames[position])
.setMessage("给应用程序打分").setIcon(resIds[position])
.setView(myView).setPositiveButton("确定", new OnClickListener()
{

                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    raAdapter.setRating(position, ratingBar.getRating());

                }
            }).setNegativeButton("取消", null).show();
}

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    List<View> viewList = new ArrayList<View>();
    viewList.add(getLayoutInflater().inflate(R.layout.main, null));
    raAdapter = new RatingAdapter(this);
    setListAdapter(raAdapter);


}

}
有几个问题:
1.setView(myView).setPositiveButton("确定", new OnClickListener()
{

                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    raAdapter.setRating(position, ratingBar.getRating());

                    这里为什么setPositiveButton第二个参数不是new Dialog.OnClickListener(),还有为什么要用适配器的对象去设置星级评分

2.这里整个的数据列表的数据是在哪设置的,实在不是很懂,希望知道的朋友帮忙解答下,

3个回答

lbcab
lbcab   2016.05.04 18:20

什么问题??????

lbcab
lbcab 回复whynofly: 1. DialogInterface是AlertDialog中的静态接口, 供用户使用Dialog时去实现 2.数据列表不是定义了吗? applicationNames, 在getview中取数据
一年多之前 回复
whynofly
whynofly 不好意思,刚刚才更新了下
一年多之前 回复
CSDNXIAOD
CSDNXIAOD   2016.05.04 18:21

一个很简单的程序问题
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

q610098308
q610098308   2016.05.05 07:44

修改adapter
1.修改构造函数,传参,传过去
2,增加setList函数,传过去,通知数据改变。
具体可看我的博客,有adapter处理

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!