无法在另一个活动中引用自定义布局的Imageview对象

时间:2014-03-06 06:16:09

标签: android android-listview android-imageview

我试图裁判一个imageview对象,它是listview(自定义布局)的一部分,另一个Activity根据用户选择的事件类型设置图像图标。但是我无法实现这一点,图标没有被改变

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:orientation="horizontal"
    android:padding="5dip">

    <!-- ListRow Left sied Thumbnail image -->

    <LinearLayout
        android:id="@+id/list_row"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:background="@drawable/image_bg"
        android:padding="3dip" >

        <!-- THUMBAIL IMAGE -->

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="85dip"
            android:layout_height="80dip"
            android:scaleType="fitXY"
            android:shadowColor="#585858"
            android:shadowDx="0.0"
            android:shadowDy="0.0"
            android:shadowRadius="20"
            android:src="@drawable/karthik" />
    </LinearLayout>

    <TextView
        android:id="@+id/sub_title_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dip"
        android:gravity="right"
        android:text="Facebook Friend"
        android:textColor="#10bcc9"
        android:textSize="12dip"
        android:textStyle="bold" />
    <!-- ARROW IMAGE -->


    <!-- NAME -->
    <!-- CONTACT NUMBER -->


    <!-- BIRTHDATE -->

    <TextView
        android:id="@+id/title_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/list_row"
        android:layout_toRightOf="@+id/list_row"
        android:text="KARTHIK.KOLANJI"
        android:textColor="#181818"
        android:textSize="16dip"
        android:textStyle="bold"
        android:typeface="sans" />

    <TextView
        android:id="@+id/txtview_zodiac"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TextView02"
        android:layout_alignBottom="@+id/TextView02"
        android:layout_toRightOf="@+id/TextView02"
        android:text="Sagittarius"
        android:textColor="#CC0000"
        android:textSize="15sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/txtview_turns"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/list_image_arrow"
        android:layout_below="@+id/list_image_arrow"
        android:text="24"
        android:textColor="#990000"
        android:textSize="27sp"
        android:textStyle="normal"
        android:layout_marginTop="7dip"/>

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/txtview_type_of_reminder"
        android:layout_alignBottom="@+id/txtview_type_of_reminder"
        android:layout_alignLeft="@+id/TextView02"
        android:text="Next birthday on : "
        android:textColor="#181818"
        android:textSize="15sp"
        android:textStyle="normal" 
        android:layout_marginTop="3dip"/>

    <TextView
        android:id="@+id/txtview_next_bd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TextView01"
        android:layout_alignBottom="@+id/TextView01"
        android:layout_toRightOf="@+id/TextView01"
        android:text="Wednesday"
        android:textColor="#CC0000"
        android:textSize="15sp"
        android:textStyle="bold" 
        android:layout_marginTop="2dip"/>

    <TextView
        android:id="@+id/sub_title_contact_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/title_name"
        android:layout_below="@+id/title_name"
        android:text="9594080469"
        android:textColor="#181818"
        android:textSize="15dip" 
       />

    <TextView
        android:id="@+id/list_image_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/sub_title_category"
        android:layout_alignTop="@+id/sub_title_contact_number"
        android:text="turns"
        android:textColor="#996600"
        android:textSize="9sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/txtview_days_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/txtview_next_bd"
        android:layout_alignBottom="@+id/txtview_next_bd"
        android:layout_alignParentRight="true"
        android:text="36"
        android:textColor="#990000"
        android:textSize="27sp"
        android:textStyle="normal" />

    <ImageView
        android:id="@+id/ImageView02"
        android:layout_width="15dip"
        android:layout_height="20dip"
        android:layout_alignLeft="@+id/list_row"
        android:layout_below="@+id/list_row"
        android:contentDescription="Zodiac icon"
        android:scaleType="fitXY"
        android:shadowColor="#8B4513"
        android:shadowDx="0.0"
        android:shadowDy="0.0"
        android:shadowRadius="20" 
        android:src="@drawable/rings_icon"/>

    <TextView
        android:id="@+id/txtview_type_of_reminder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/ImageView02"
        android:layout_toRightOf="@+id/ImageView02"
        android:text="(Anniversary)"
        android:textColor="#787878"
        android:textSize="13sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/txtview_type_of_reminder"
        android:layout_alignLeft="@+id/sub_title_birthdate"
        android:text="Zodiac :  "
        android:textColor="#181818"
        android:textSize="15sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/sub_title_birthdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/sub_title_contact_number"
        android:layout_centerVertical="true"
        android:text="7th, April 1990"
        android:textColor="#181818"
        android:textSize="15dip" />

    <TextView
        android:id="@+id/TextView04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/txtview_turns"
        android:layout_alignTop="@+id/ImageView01"
        android:text=" days left"
        android:textColor="#996600"
        android:textSize="9sp"
        android:textStyle="normal" />

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_above="@+id/txtview_next_bd"
        android:layout_alignRight="@+id/txtview_next_bd"
        android:layout_marginRight="16dp"
        android:contentDescription="Zodiac icon"
        android:scaleType="fitXY"
        android:shadowColor="#8B4513"
        android:shadowDx="0.0"
        android:shadowDy="0.0"
        android:shadowRadius="20" />

    </RelativeLayout>

ContactInfoMoreOption.java

protected Dialog onCreateDialog(int id){
LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=LayoutInflater.from(getApplication()).inflate(R.layout.list_row, null);
final ImageView imgView=(ImageView)v.findViewById(R.id.ImageView02);
switch(id){
case 2:  // Change event type alert dialog  
                try{                    
                    spnView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked,getResources().getStringArray(R.array.choose_type)));
                    alertBuilder=new AlertDialog.Builder(this);
                    alertBuilder=new AlertDialog.Builder(this);
                    alertBuilder.setTitle("Change event type");
                    alertBuilder.setIcon(R.drawable.change_event_type);             
                    alertBuilder.setView(spnView);
                    alertBuilder.setNegativeButton("cancel",new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                        }
                    });

                    alertBuilder.setPositiveButton("ok",new DialogInterface.OnClickListener() {

                        @SuppressWarnings("null")
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            if (spnView.getSelectedItem().toString().equals("User defined")) {
                                showDialog(USER_DEFINED_EVENT_NAME);
                            }
                            else{
                                ContentValues values=new ContentValues();
                                values.put(BirthdayProvider.EVENT_TYPE, spnView.getSelectedItem().toString());                      
                                int count=getContentResolver().update(BirthdayProvider.CONTENT_URI, values, BirthdayProvider.NUMBER+"='"+SearchListActivity.longClickValue+"'", null);
                                if (count==1) {
                                    String iconType = null;
                                    if (spnView.getSelectedItem().toString().equals("Birthday")) {
                                        iconType="app_icon.png";
                                    }
                                    else if (spnView.getSelectedItem().toString().equals("Anniversary")){
                                        iconType="rings_icon.png";
                                    }
                                    InputStream is = null;

                                        try {
                                            is = getResources().getAssets().open("rings_icon.png");
                                        } catch (IOException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                                        Log.v("is......", is.toString());

                                    Bitmap bit =BitmapFactory.decodeStream(is); 
                                    Log.v("bit......", bit.toString());
                                    imgView.setImageBitmap(bit);

                                    imgView.setScaleType(ScaleType.FIT_XY);
                                    finish();
                                    Toast.makeText(getBaseContext(),"Updated Successfully",Toast.LENGTH_SHORT).show();
                                }
                                 else{
                                     Toast.makeText(getBaseContext(),"Updation Failed",Toast.LENGTH_SHORT).show();
                                 }
                            }                       
                        }
                    });     
                    alertDialog=alertBuilder.create();
                    alertDialog.show();
                    return alertDialog;
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
    }
}

2 个答案:

答案 0 :(得分:1)

不是直接从第二个Dialog活动访问/更改listview布局的imageview,而是需要访问listview的适配器,根据该适配器填充listview项目,所以基本上将所选项目INDEX发送到对话框活动,里面对话框活动使用adapter.getItem(index)并更改项目(即更改事件类型),并且还调用adapter.notifydatasetchanged以在listview中进行更改... 基本上访问对话框中的适配器项并修改该项

答案 1 :(得分:0)

两件事。确保您没有错误地导入Android R文件。还要清理项目并查看是否可以修复它。