Go to the layout of one class in another?

I have a custom ListView in which there is a textview and an image. I have set onclicklistener for imageview in my customlistadapter class, so I need to make some changes in main layout when the image is clicked. See the below code for reference…

MainActivity.java

hmDataList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView parent, View view,
                int position, long arg1) {

            DataFields dataField = (DataFields) hmDataList
                    .getItemAtPosition(position);
            ImageView v = (ImageView) view.findViewById(R.id.hmFieldDeleteImage);
            RelativeLayout mainRL = (RelativeLayout)view.findViewById(R.id.hmFieldMainRL);
            }

}

CustomListAdapter.java

public View getView(int position, View convertView, ViewGroup parent) {
        holder = null;
        DataFields rowItems = (DataFields) getItem(position);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.home_field_row, null);
            holder = new ViewHolder();
            holder.mName = (TextView) convertView.findViewById(R.id.hmFieldName);
            holder.mDeleteImage = (ImageView)convertView.findViewById(R.id.hmFieldDeleteImage);

            convertView.setTag(holder);

            holder.mDeleteImage.setTag(position);
            final View clickView = convertView;
            holder.mDeleteImage.setOnClickListener(new ImageView.OnClickListener() {

                @Override
                public void onClick(final View view) {
                    count++;
                    clickView.setBackgroundColor(color.list_row_bg);
                    //
                    //Some changes has to be made for the main activity's layout
                    //
                }
            });
        }

        else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mName.setText(rowItems.getName());

        return convertView;
    }

list_row.xml


activity_main.xmlhas some widgets which are hidden, so I need to unhide them when the Image in the custom ListView is clicked.

To put it simple, Is there any way to update the gui of main activity from other class? Any kind of help or example or reference is much appreciated. Thanks !

Here is a good way to do it:

In your MainActivity.java
add :

public onClickListener mListener = new OnClickListener(){
        @Override
            public void onClick(final View view) {
                count++;
                view.setBackgroundColor(color.list_row_bg);
                //
                //Some changes has to be made for the main activity's layout
                //

               // Here you can delete add or do anything to your views
            }

  };

and when you create a new CustomListAdapter object:

CustomListAdapter adapter = new .....;
adapter.setActivity(this);

and In your CustomListAdapter.java
add :

private MainActivity mActivity;

 public void setActivity(MainActivity activity){
          mActivity = activity;
 }

and In your getView method:

public View getView(int position, View convertView, ViewGroup parent) { holder = null; DataFields rowItems = (DataFields) getItem(position); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {
        convertView = inflater.inflate(R.layout.home_field_row, null);

             ....  Same Code

        holder.mDeleteImage.setTag(position);
        final View clickView = convertView;
// use this code
        holder.mDeleteImage.setOnClickListener(mActivity.mListener);
    }

    else {
        holder = (ViewHolder) convertView.getTag();
    }

}
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Go to the layout of one class in another?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录