I have Question regarding to Listview and CheckBox
I m creating a custom ListView
using following JSON
data
In this example have use the string variable that store id of selected row
MainActivity
public class MainActivity extends Activity {
static final String TAG_NAME = "name";
static final String TAG_ID = "id";
static String selected = "";
ListView listView;
HashMap<String, String> map;
ArrayList<HashMap<String,String>> myList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList = new ArrayList<HashMap<String,String>>();
listView = (ListView) findViewById(R.id.listView1);
fillListView();
}
private void fillListView() {
myList.clear();
map = new HashMap<String, String>();
map.put(TAG_ID,"1");
map.put(TAG_NAME, "Name1");
myList.add(map);
map = new HashMap<String, String>();
map.put(TAG_ID,"2");
map.put(TAG_NAME, "Name2");
myList.add(map);
map = new HashMap<String, String>();
map.put(TAG_ID,"3");
map.put(TAG_NAME, "Name3");
myList.add(map);
map = new HashMap<String, String>();
map.put(TAG_ID,"4");
map.put(TAG_NAME, "Name4");
myList.add(map);
listView.setAdapter(new MyListAdapter(this, R.layout.row, myList));
}
private class MyListAdapter extends BaseAdapter
{
private List<HashMap<String,String>> List;
private LayoutInflater mInflater;
private int layoutresource;
public MyListAdapter(Context context,int resource,List<HashMap<String, String>> myList) {
List = myList;
mInflater = LayoutInflater.from(context);
layoutresource = resource;
}
@Override
public int getCount() {
return List.size();
}
@Override
public Object getItem(int position) {
return List.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(layoutresource,null);
}
final View rootView = convertView.findViewById(R.id.root);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
final TextView name = (TextView) convertView.findViewById(R.id.textView1);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.profile));
name.setTextColor(Color.parseColor("#ffffff"));
name.setText(List.get(position).get(TAG_NAME));
if ( List.get(position).get(TAG_ID).equals(selected))
name.setTextColor(Color.parseColor("#ff0000"));
else
name.setTextColor(Color.parseColor("#ffffff"));
rootView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selected = List.get(position).get(TAG_ID);
fillListView();
}
});
name.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rootView.performClick();
}
});
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rootView.performClick();
}
});
return convertView;
}
}
}
you can set Layout Visible and Invisibile Using view.setVisibility(View.VISIBLE) or view.setVisibility(View.INVISIBLE)