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

گرفتن id در یک custom control

General  9 سال پیش  9 سال پیش
0 0

باسلام

من یک custom control ایجاد کردم که داخلش یک عکس ودو textview گذاشتم

وداخل اون عکس محصولم و وداخل textview ها id محصول ونام محصول رو از سرور میگیرم

منتها الان میخوام وقتی روی این custom control ها کلیک کردم id رو به من نمایش بده منتها این اتفاق نمیفته وtext تکس ویو رو که داخل xml این custom control گذاشتم رو نشون میده

به عنوان مثال من 10 تا محصول اولمو گرفتم وروی هر کدوم ازاینها کلیک میکنم همشون یک چیزو نشون میده

طبق اموزش من یه کلاس برای custom control ایجاد کردم و دو تکس ویو ویک image view ویک linear layout براش تعریف کردم واومدم برای خاصیت onClick همین linear layout مقدار textview ی که ایدی داخلشه رو توست کردم

مشکل کجاست؟لطفا راهنمایی بفرمایید باتشکر از دوستان

+2 0
اگر کد رو میزاشتین بهتر می تونستیم کمکتون کنیم (9 سال پیش)
+1 0
یه کمی نامفهوم بود ... به قول دوستمون کاش کد میزاشتین ولی بهرحال شما inflate کردین و با id.textview تکست ویو رو گرفتین!؟ (9 سال پیش)
0 0
بله گرفتم کدها رو ملاحظه بفرمایید اصلا برای من عجیبه چطور برای هرکدوم ازاینها تفاوتی قائل نمیشه وبرای همه یه چیز چاپ میکنه (9 سال پیش)
0 0
ببخشید شما دیباک کردن رو بلدید یه دیباگ انجام بدید خیلی راحت می تونید مشکلتون رو پیدا کنید (9 سال پیش)
0 0
میشه واضح تر بگید چیکارکنم؟ببینید حتی توی نمایش داره id رو به درستی توی هرکدوم از کاستوم کنترل ها نشون میده ولی وقتی کلیک میکنم مقداری که تکس ویو در لایه xml داره رو بهم نشون میده لطفا راهنماییم کنید (9 سال پیش)
0 0
خوب حتما مقداری براش ست نکردین (9 سال پیش)
0 0
شما باید دیباک کنید ببینید ایا مقداری رو از سرور می گیرید منظورم اونیه که شما می خواید مثلا قیمت محصول رو شاید شما این مقدار رو دریافت نکرده باشید (9 سال پیش)
0 0
بله مشکلی نداره اطلاعات کاملا درست میاد وحتی اگه دقت کنید من اونا رو توی textview خودشون نشون میدم تنها مشکل اینه که وقتی کلیک میکنم مقداری که توی تکس ویو هرکدوم ازاون ها هست رو نشون نمیده (9 سال پیش)
 برای این سوال 3 پاسخ وجود دارد.
پاسخ به سوال 
General  9 سال پیش
0 0

این هم کدها دوست عزیز

کد فایل xml کاستوم کنترل :

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layoutDirection="rtl"
    android:id="@+id/amazingLinear"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_marginRight="4dp"
        android:background="#ffffff"
        android:layout_width="wrap_content"
        android:layout_marginLeft="4dp"
        android:layout_height="wrap_content">

        <LinearLayout
            android:background="#ffffff"
            android:layout_width="170dp"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_height="250dp">
            <ImageView
                android:layout_width="160dp"
                android:src="@drawable/mobile2"
                android:scaleType="centerCrop"
                android:layout_marginBottom="16dp"
                android:layout_height="150dp"
                android:id="@+id/imgPic" />
            <TextView
                android:layout_width="wrap_content"
                android:text="clicksite"
                android:textColor="#2f2f2f"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp"
                android:layout_height="20dp"
                android:id="@+id/txtid" />
            <TextView
                android:layout_width="wrap_content"
                android:text="ASUS ROG GL552VW"
                android:textColor="#2f2f2f"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp"
                android:layout_height="20dp"
                android:id="@+id/txttitle" />
            <ImageView
                android:layout_width="match_parent"
                android:background="#807f7f"
                android:layout_height="2dp" />
            <TextView
                android:layout_width="wrap_content"
                android:text="24000"
                android:textColor="#07960b"
                android:layout_height="wrap_content"
                android:id="@+id/txtPrice" />
            <TextView
                android:layout_width="wrap_content"
                android:gravity="center_vertical"
                android:text="28000"
                android:textColor="#ff0000"
                android:background="@drawable/redline"
                android:layout_height="wrap_content"
                android:id="@+id/txtPPrice" />
        </LinearLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

 

  1. این هم کدهای کلاس کاستوم کنترل:
  2.  
    public class ProductAmazing extends LinearLayout {




        LinearLayout linearLayout;
        public static ImageView pic;
        public static TextView title;
        public static TextView id;
        public static TextView pPrice;
        public static TextView price;

        public ProductAmazing(Context context) {
            super(context);
            init(context);
        }
        public ProductAmazing(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }
        public ProductAmazing(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init(context);
        }



        public void init(Context context){
            LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view=inflater.inflate(R.layout.product_amazing,this,true);


            pic=(ImageView)view.findViewById(R.id.imgPic);
            title=(TextView) view.findViewById(R.id.txttitle);
            pPrice=(TextView) view.findViewById(R.id.txtPPrice);
            price=(TextView)view.findViewById(R.id.txtPrice);
            id=(TextView)view.findViewById(R.id.txtid);

            linearLayout=(LinearLayout)view.findViewById(R.id.amazingLinear);


            linearLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(G.context,id.getText().toString(),Toast.LENGTH_SHORT).show();
                }
            });
     

این هم کدهایی که برای مقداردهی نوشتم:

    
    public void showAmazingProduct(){

        try {
            JSONArray jsonArray=new JSONArray(amazingProduct);
            for(int i=0;i<jsonArray.length();i++){

                JSONObject object=jsonArray.getJSONObject(i);
                int id=object.getInt("id");
                String title=object.getString("title");
                String pPrice=String.valueOf(object.getInt("pprice")+" ÊæãÇä");
                String price=String.valueOf(object.getInt("price")+" ÊæãÇä");
                String pic=object.getString("pic");

                String picUrl="http://192.168.1.101/digikala/img/"+pic;

                createProductAmazing(id,title,pPrice,price,picUrl);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }



 public void createProductAmazing(int id,String title, String pPrice, String price,String picUrl){


        String id2=String.valueOf(id);
        productAmazing=new ProductAmazing(context);
        productAmazing.id.setText(id2);
        productAmazing.title.setText(title);
        productAmazing.pPrice.setText(pPrice);
        productAmazing.price.setText(price);
        Picasso.with(G.context).load(picUrl).into(productAmazing.pic);
         layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        linearProductAmazing.addView(productAmazing);

    }
 
0 0
دوست عزیز من کاستوم کنترل شما رو تست کردم کار میکنه که!! شما یه لاگ بزن تو کلیک صددرصد کار میکنه! (9 سال پیش)
0 0
واقعا؟یعنی ایراد از امولیتور منه که درست نشون نمیده؟ (9 سال پیش)
0 0
شما با چه امولیتوری تست کردین؟ (9 سال پیش)
0 0
من الان با گوشی هم تست کردم حتی لاگ هم کردم ولی بازم درست نمیده (9 سال پیش)
0 0
نظر خاصی ندارم والا خیلی عجیبه!! .... (9 سال پیش)
0 0
ممنون از راهنماییتون لطف کردید (9 سال پیش)
پاسخ به سوال 
Amir  9 سال پیش
0 0

توی متد init کد linearLayout.setOnClick رو کلا پاک کنید و بجاش متد onTouchEvent رو برای کلاس ProductAmazing اورراید کنید. به شکل زیر:

@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);

    Toast.makeText(G.context, id.getText().toString(), Toast.LENGTH_SHORT).show();
    return true;
}

id رو به صورت فیلد دربیارید که اونو بشناسه.

0 0
به دستور توست دراین متد ارور unreachable statement میده. ایراد onClick چیه که نوشتم؟مشکلی داره یعنی؟ (9 سال پیش)
0 0
نه نه ببخشید onClick مشکلی نداره من اشتباه کردم. (9 سال پیش)
0 0
خب الان جواب نمیده چیزی به ذهنتون نمیرسه که کمکم بکنه؟ (9 سال پیش)
0 0
نه متاسفانه!. (9 سال پیش)
پاسخ به سوال 
General  9 سال پیش
0 0

مشکل برطرف شد

به ساده ترین شکل ممکن

من به جای اینکه id رو داخل textview بریزم داخل یه متغیر ریختم وجواب داد!

به همین سادگی

ممنون ازهمه دوستان که وقت گذاشتن

0 0
خداروشکر .... (9 سال پیش)

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