آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

تعریف صحیح آداپتر

سپهر  11 سال پیش  11 سال پیش
0 0

من در 3 اکتیویتی 3 تا لیست متفاوت دارم ، آیا برای هر لیست باید آداپتر جداگانه تعریف کنم یا این که میشه یک آداپتر داینامیک برای این 3 لیست استفاده کرد ؟

+1 0
اگه لیست هاتون از نظر ساختاری عین هم هستن نیازی به تعریف تنها یک Adapter هست ، در غیر اینصورت خیلی به نظرم جالب نیست یه Adapter پویا ( حالا نمیدونم منظورتون دقیقا از این عبارت چیه ) ایجاد کنید. (11 سال پیش)
0 0
از لحاظ آیتم لیست ها با هم متفاوت هستن که هر کدام از یک xml خاص ویو هاشون رو میگیرن ولی اطلاعات داخل این آیتم ها همه از یک struct گرفته میشن (11 سال پیش)
0 0
سپهر جان از اندک تجربه ای که من دارم گمون نکنم با روش استاد بشه که این کارو انجام داد. منم یکی دو روزی دنبال این راه حل بودم که به نتیجه ای نرسیدم. حتی توی انجمن هم مطرح کردم ولی به نتیجه خاصی نرسیدم. (11 سال پیش)
+2 0
اگه صرفا لایه xml متفاوتی دارن میتونی تو کاستوم ادپتورت شناسه لایه رو به عنوان پارامتر ورودی بگیری. ولی اگه استراکت متفاوتی داری نه (11 سال پیش)
0 0
@محمد حسین زاده ، اگر لطف کنی بیشتر کمک کنی ممنون میشم ، من کد آداپترم رو میذارم در بخش پاسخ ممنون میشم راهنمایی کنید (11 سال پیش)
 برای این سوال 2 پاسخ وجود دارد.
پاسخ به سوال 
سپهر  11 سال پیش
0 0

public class AdapterCategory extends ArrayAdapter<StructGame> {

    public AdapterCategory(ArrayList<StructGame> array) {
        super(G.context, R.layout.custom_list_category, array);
    }


    private static class ViewHolder {

        public ViewGroup layoutRoot;
        public TextView  txtgname;
        public ImageView imghand;
        public TextView  txtcount;


        public ViewHolder(View view) {
            layoutRoot = (ViewGroup) view.findViewById(R.id.layoutRoot_cat);
            txtgname = (TextView) view.findViewById(R.id.txtName_cat);
            imghand = (ImageView) view.findViewById(R.id.imgHand_cat);
            txtcount = (TextView) view.findViewById(R.id.txtCount_cat);
        }


        public void fill(final ArrayAdapter<StructGame> adapter, final StructGame item, final int position)
        {
            txtgname.setText("" + item.gname);
            txtcount.setText("" + item.count);
            layoutRoot.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(G.currentActivity, ActivityGameCategory.class);
                    G.currentActivity.startActivity(intent);
                }
            });
        }
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        StructGame item = getItem(position);
        if (convertView == null) {

            convertView = G.inflater.inflate(R.layout.custom_list_category, parent, false);
            holder = new ViewHolder(convertView);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position % 2 == 0) {
            convertView.setBackgroundResource(R.drawable.list_background_alternate);

        } else {
            convertView.setBackgroundResource(R.drawable.list_backgroundcolor);
        }
        holder.fill(this, item, position);
        return convertView;
    }
}

 

 

پاسخ به سوال 
محمد حسین زاده  11 سال پیش
+2 0

 public class AdapterCategory extends ArrayAdapter<StructGame> {

private int layoutId;

public AdapterCategory(ArrayList<StructGame> array, int layoutId) {
super(G.context, R.layout.custom_list_category, array);

this.layoutId=layoutId;
}


private static class ViewHolder {

public ViewGroup layoutRoot;
public TextView txtgname;
public ImageView imghand;
public TextView txtcount;


public ViewHolder(View view) {
layoutRoot = (ViewGroup) view.findViewById(R.id.layoutRoot_cat);
txtgname = (TextView) view.findViewById(R.id.txtName_cat);
imghand = (ImageView) view.findViewById(R.id.imgHand_cat);
txtcount = (TextView) view.findViewById(R.id.txtCount_cat);
}


public void fill(final ArrayAdapter<StructGame> adapter, final StructGame item, final int position)
{
txtgname.setText("" + item.gname);
txtcount.setText("" + item.count);
layoutRoot.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(G.currentActivity, ActivityGameCategory.class);
G.currentActivity.startActivity(intent);
}
});
}
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

StructGame item = getItem(position);
if (convertView == null) {

convertView = G.inflater.inflate(layoutId, parent, false);
holder = new ViewHolder(convertView);

convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}
if (position % 2 == 0) {
convertView.setBackgroundResource(R.drawable.list_background_alternate);

} else {
convertView.setBackgroundResource(R.drawable.list_backgroundcolor);
}
holder.fill(this, item, position);
return convertView;
}
}

البته تو ادیتور سایت تغییر دادم . تست کن

0 0
توی بخش super لازم نیست که R.layout.custom_list_category با layoutId تغییر کنه ؟ (11 سال پیش)

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .