گرفتن id در یک custom control
باسلام
من یک custom control ایجاد کردم که داخلش یک عکس ودو textview گذاشتم
وداخل اون عکس محصولم و وداخل textview ها id محصول ونام محصول رو از سرور میگیرم
منتها الان میخوام وقتی روی این custom control ها کلیک کردم id رو به من نمایش بده منتها این اتفاق نمیفته وtext تکس ویو رو که داخل xml این custom control گذاشتم رو نشون میده
به عنوان مثال من 10 تا محصول اولمو گرفتم وروی هر کدوم ازاینها کلیک میکنم همشون یک چیزو نشون میده
طبق اموزش من یه کلاس برای custom control ایجاد کردم و دو تکس ویو ویک image view ویک linear layout براش تعریف کردم واومدم برای خاصیت onClick همین linear layout مقدار textview ی که ایدی داخلشه رو توست کردم
مشکل کجاست؟لطفا راهنمایی بفرمایید باتشکر از دوستان
این هم کدها دوست عزیز
کد فایل 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>
- این هم کدهای کلاس کاستوم کنترل:
-
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);
}
توی متد 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 رو به صورت فیلد دربیارید که اونو بشناسه.
مشکل برطرف شد
به ساده ترین شکل ممکن
من به جای اینکه id رو داخل textview بریزم داخل یه متغیر ریختم وجواب داد!
به همین سادگی
ممنون ازهمه دوستان که وقت گذاشتن
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .