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
<TextView
android:id="@+id/hmFieldName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:gravity="left"
android:shadowColor="#000000"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="2"
android:text="@string/no_data"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#F2F2F2" />
<ImageView
android:id="@+id/hmFieldDeleteImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:contentDescription="@string/right_arrow"
android:src="@drawable/delete" />
activity_main.xml has 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 :
and when you create a new CustomListAdapter object:
and In your CustomListAdapter.java add :
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);