I have a ListView
which is generated using a SimpleAdapter
.I have a textview and an ImageView in the xml file which is used with the SimpleAdapter. The imageview is set to invisible at first. When a row in the Listview is clicked the Imageview becomes visible. What I want to do is the following:
If I have 4 rows in the Listview and if I click row 1 then the ImageView should be visible on row 1 then if I click row 2 the imageview should become invisible from row 1 and should be visible only on row 2.
I have done the following but it works in the following way:
If I click on row 1 the ImageView is visible on row1. Then if I click row 2 the ImageView gets visible on row 2 and is also visible on row1. I want to make it invisible fro m row1 and display only on the clicked row.
Can anyone guide me step by step how to do it. My codes and xml are as below:
myactivity.class
final ListAdapter k=new SimpleAdapter(getActivity(),val,R.layout.senttaskdata2,new String[]{"rname"},new int[]{R.id.textView1})
{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final View v = super.getView(position, convertView, parent);
ImageView im=(ImageView)v.findViewById(R.id.ImageView3);
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
ImageView im=(ImageView)arg1.findViewById(R.id.ImageView3);
setVisibility(arg1.VISIBLE);
});
return v;
}
};
sent.setAdapter(k);
senttaskdata2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@android:color/transparent" >
<ImageView
android:id="@+id/imageView3"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:src="@drawable/test2" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:text="TextView"
android:textColor="@android:color/darker_gray"
android:textSize="11sp" />
</RelativeLayout>
</LinearLayout>
I have the same problem and I found a simple way to do it.
First, in your custom adapter
public class MyAdapter extends BaseAdapter {
private int selectedIndex;
private boolean visible;
// omitted code
}
// omitted code
public View getView(int index, View view, ViewGroup parent) {
TextView textView= (TextView) view.findViewById(R.id.text_view);
ImageView imgView = (ImageView) view.findViewById(R.id.image_view);
textView.setText("some text");
if(index == selectedIndex) {
imgView .setVisibility(View.VISIBLE);
} else {
imgView .setVisibility(View.INVISIBLE);
}
return view;
}
Second, in your activity
public void onCreate(Bundle bundle) {
// omitted code
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
// change index of selected item
fontAdapter.setSelectedIndex(position);
fontAdapter.notifyDataSetChanged();
}
});
// omitted code
}
Try this i have implemented my own adapter instead of using Simple Adapter
ListAdapter.java
package com.stackoverflow.listviewanswer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter
{
private boolean[] mVisibilityList = null;
private String[] mNameList = null;
private Context mContext = null;
private LayoutInflater mInflater = null;
public ListAdapter(Context iContext, boolean[] iVisibilityList, String[] iNameList)
{
mVisibilityList = iVisibilityList;
mContext = iContext;
mInflater = LayoutInflater.from(mContext);
mNameList = iNameList;
}
public void setVisibilityList(boolean[] iVisibilityList)
{
mVisibilityList = iVisibilityList;
}
@Override
public int getCount()
{
return mVisibilityList.length;
}
@Override
public Object getItem(int iPosition)
{
return mVisibilityList[iPosition];
}
@Override
public long getItemId(int iPosition)
{
return iPosition;
}
@Override
public View getView(int iPosition, View iConvertView, ViewGroup iViewGroup)
{
Holder holder = new Holder();
if (iConvertView == null)
{
iConvertView = mInflater.inflate(R.layout.list, null);
holder.mImageView = (ImageView) iConvertView.findViewById(R.id.imageView3);
holder.mTextView = (TextView) iConvertView.findViewById(R.id.textView1);
iConvertView.setTag(holder);
}
else
{
holder = (Holder) iConvertView.getTag();
}
// SET DATA
if (mVisibilityList[iPosition])
{
holder.mImageView.setVisibility(View.VISIBLE);
}
else
{
holder.mImageView.setVisibility(View.INVISIBLE);
// OR
// holder.mImageView.setVisibility(View.GONE);
// Use as per your needs
}
holder.mTextView.setText(mNameList[iPosition]);
return iConvertView;
}
private class Holder
{
TextView mTextView = null;
ImageView mImageView = null;
}
}
MainActivity.java
package com.stackoverflow.listviewanswer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity implements OnItemClickListener
{
ListView mListView = null;
boolean[] mVisisbilityList = { false, false, false, false };
String[] mNameList = { "TextView 1", "TextView 2", "TextView 3", "TextView 4" };
ListAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.list_view);
mListView.setOnItemClickListener(this);
adapter = new ListAdapter(this, mVisisbilityList, mNameList);
mListView.setAdapter(adapter);
}
@Override
public void onItemClick(AdapterView<?> iAdapterView, View iView, int iPosition, long iId)
{
for (int i = 0; i < 4; i++)
{
mVisisbilityList[i] = false;
}
mVisisbilityList[iPosition] = true;
adapter.setVisibilityList(mVisisbilityList);
adapter.notifyDataSetChanged();
}
}
list.xml is same as your senttaskdata2.xml
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.