I am using a list view and an adapter for loading a list,each list item has a TextView,EditText and Image..I set the visibility of the arrow and the Edit text according to the position of the list row,everything works fine when I load the list for the first time...
But when I scroll through the list,visibility of the items keep changing...Kindly help me in this issue...The relevant codes has been attached...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:background="#FFFFFF">
<TextView android:layout_height="wrap_content" android:layout_width="0dip"
android:textSize="20dip" android:layout_weight="1"
android:id="#+id/textview_add_lot_list" android:textColor="#android:color/black"
android:paddingTop="10dip" android:paddingBottom="10dip"
android:paddingLeft="10dip"/>
<EditText android:layout_height="fill_parent" android:layout_width="0dip"
android:layout_weight="1" android:id="#+id/et_add_lot_list"
android:layout_gravity="center_vertical"/>
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content"
android:id="#+id/imageview_arrow_add_lot_list" android:layout_gravity="center_vertical"
android:visibility="invisible" android:src="#drawable/more_reviews_arrow"
android:paddingRight="10dip"/>
</LinearLayout>
Java code activity...
final ArrayList<String> listItems = new ArrayList<String>();
listItems.add("Parking name");
listItems.add("Address");
listItems.add("City");
listItems.add("State");
listItems.add("Zip");
listItems.add("Phone");
listItems.add("Web Address");
listItems.add(" ");
listItems.add("Parking Image");
listItems.add(" ");
listItems.add("Open Hours");
listItems.add(" ");
listItems.add("Web Reviews");
final AddParkingLotAdapter adapter = new AddParkingLotAdapter(mAppContext,0,listItems);
lv.setAdapter(adapter);
Java code...adapter
public class AddParkingLotAdapter extends ArrayAdapter<String> {
private ArrayList<String> mStrings;
private LayoutInflater mInflater;
private AppContext mContext;
private static int NON_EMPTY_ROW = 1;
private static int EMPTY_ROW = 0;
public AddParkingLotAdapter(Context context, int resId, List<String> strings) {
super(context, resId,strings);
mStrings = (ArrayList<String>) strings;
mContext = (AppContext) context;
mInflater = LayoutInflater.from(context);
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getCount() {
return mStrings.size();
}
#Override
public String getItem(int position) {
return mStrings.get(position);
}
#Override
public int getItemViewType(int position) {
if(position==7||position==9||position==11){
return EMPTY_ROW;
}else{
return NON_EMPTY_ROW;
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView itemTextView = null;
//different inflations for different type rows..
if(getItemViewType(position) == EMPTY_ROW){
if (convertView == null) {
convertView = mInflater.inflate(R.layout.review_empty_row, null);
}
}else if(getItemViewType(position) == NON_EMPTY_ROW){
if (convertView == null) {
convertView = mInflater.inflate(R.layout.add_parkinglist_item, null);
}
itemTextView = (TextView) convertView.findViewById(R.id.textview_add_lot_list);
itemTextView.setText(mStrings.get(position));
if (position==3||position==8||position==10||position==12){
ImageView itemImageView = (ImageView)convertView.findViewById(R.id.imageview_arrow_add_lot_list);
itemImageView.setVisibility(View.VISIBLE);
EditText editText = (EditText)convertView.findViewById(R.id.et_add_lot_list);
editText.setVisibility(View.INVISIBLE);
}
}
return convertView;
}
}
In this code:
if (position==3||position==8||position==10||position==12){
ImageView itemImageView = (ImageView)convertView.findViewById(R.id.imageview_arrow_add_lot_list);
itemImageView.setVisibility(View.VISIBLE);
EditText editText = (EditText)convertView.findViewById(R.id.et_add_lot_list);
editText.setVisibility(View.INVISIBLE);
}
you've got no else clause. That means that if position is 0,1,2,4,5 or 6 you don't explicitly set the visibility of the views and so the visibility will be whatever it was set to when the views were recycled. If convertView is non-null, you always need to reset the visibility of any items whose visibility may be been modified earlier.
Related
I am coding a listView that gets the data from mySQL Server.
I have created the followings classes.
Class ListView. It has two contractors all in Strings. And I sat getters and setters.
I believe I have a problem with the Adapter its self. I can click on more than one option. While I have created the custom adapter in the layout as the followings:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/RG_Adapter"
android:layoutDirection="rtl"
android:orientation="horizontal">
<RadioButton
android:id="#+id/TRIP_NAME"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:textAlignment="center"
android:textSize="25sp"
android:textColor="#000000"
/>
<TextView
android:id="#+id/SUM_TRIPS"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#000000"
android:textAlignment="center"
android:textSize="25sp" />
</RadioGroup>
I believe my problem is with the Adapter class.
I have created it like the followings:
public class TRIPS_LISTVIEW_ADAPTER extends ArrayAdapter<TRIPS_LISTVIEW> {
private Context mContext;
private ArrayList<TRIPS_LISTVIEW> mData;
private MyFunctionsClass myFunctionsClass = new MyFunctionsClass();
public TRIPS_LISTVIEW_ADAPTER (Context mContext, ArrayList<TRIPS_LISTVIEW> mData) {
super(mContext, R.layout.summary_shape_layout,mData);
this.mContext = mContext;
this.mData = mData;
}
public int getCount() {
return mData.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater)
mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.summary_shape_layout, null);
}
RadioGroup RG = (RadioGroup) convertView.findViewById(R.id.RG_Adapter);
TextView TRIP_NAME = (TextView) convertView.findViewById(R.id.TRIP_NAME);
TRIP_NAME.setTypeface(myFunctionsClass.FONT( TRIP_NAME.getContext().getAssets(),1));
TRIP_NAME.setText(myFunctionsClass.get_The_trip(mData.get(position).getTRIP_TITLE()));
TextView SUM_TRIPS = (TextView) convertView.findViewById(R.id.SUM_TRIPS);
SUM_TRIPS.setTypeface(myFunctionsClass.FONT( SUM_TRIPS.getContext().getAssets(),1));
SUM_TRIPS.setText(mData.get(position).getTRIP_COUNT());
return convertView;
}
}
The Data in my MainActivity Class are retrieved correctly. But as I mentioned I have multiple mode selection.
Try the following:
1) Demo2.class:-------------
public class Demo2 extends AppCompatActivity {
private ListView lv;
private CheckBox cb;
private Adapter adapter;
private List<Boolean> checkBoxState;
private List<String> checkBoxText;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo2);
checkBoxState = new ArrayList<>();
checkBoxText = new ArrayList<>();
for(int i = 0 ; i<10 ; i++){
if(i == 0) {
checkBoxState.add(i, true);
}else{
checkBoxState.add(i , false);
}
checkBoxText.add( i , "C" + (i+1));
}
lv = (ListView) findViewById(R.id.lv);
adapter = new Adapter(getApplicationContext() , checkBoxState , checkBoxText);
lv.setAdapter(adapter);
}
}
2) Adapter.class:------
public class Adapter extends BaseAdapter {
private Context context;
private LayoutInflater layoutInflater;
private List<Boolean> checkBoxState;
private List<String> checkBoxText;
public Adapter(Context context, List<Boolean> checkBoxState , List<String> checkBoxText) {
this.context = context;
layoutInflater = LayoutInflater.from(context);
this.checkBoxState = checkBoxState;
this.checkBoxText = checkBoxText;
}
public int getCount() {
return checkBoxState.size();
}
public Object getItem(int position) {
return checkBoxState.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final CheckBox cb_list_item;
if (convertView == null) {
if (layoutInflater != null) {
view = layoutInflater.inflate(R.layout.list_item, null);
}
}
cb_list_item = (CheckBox) view.findViewById(R.id.cb_list_item);
cb_list_item.setText(checkBoxText.get(position));
cb_list_item.setOnCheckedChangeListener(null); // mask onCheckedChangeListener()
cb_list_item.setChecked(checkBoxState.get(position));
cb_list_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (!b) { // already selected
compoundButton.setChecked(true);
} else { // is selected now
checkSelected(position);
}
}
});
return view;
}
private void checkSelected(int position){
try {
for (int i = 0; i < checkBoxState.size(); i++) {
checkBoxState.set(i, false);
}
checkBoxState.set(position, true);
this.notifyDataSetChanged();
}catch (Exception e){
e.printStackTrace();
}
}
}
3) demo2.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:weightSum="100"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lv"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
</ListView>
</LinearLayout>
4) list_item.xml:----------
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="C"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="#+id/cb_list_item"/>
</android.support.constraint.ConstraintLayout>
5) Output:--------
I am trying to implement a GridView in Android to display a list of Products as mentioned in below image:
With Custom Button and Grid List I am implemented this.
I want to know, how can I make this Product Button Red when I select it. Or in other words, I want to get the selected cell item object and change the background color to red, TextViews text color to white. Plus, at the same time, I want to make all remaining cell items to default white background and text color to purple.
I am new to android, any help would be great support. Thanks in advance. Here is my code:
GridView in Fragment
<GridView
android:id="#+id/grid_Products"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:horizontalSpacing="10dp"
android:gravity="center"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp />
In ProductFragment class in onCreateView() method, I am binding the productModels to gridView
List<ProductModel> productModels;
GridView gdGridView=(GridView)(view.findViewById(R.id.grid_Products));
adapter = new ProductButtonAdaptor(view.getContext(), productModels);
gdGridView.setAdapter(adapter);
product_button.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_product_red"
android:id="#+id/pnl_ProudctButton"
android:orientation="vertical">
<LinearLayout
android:paddingTop="10dp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="5"
android:gravity="center"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/lbl_ProductName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:text="5"
android:layout_marginRight="2dp"
android:textColor="#color/purple"
android:textAlignment="center"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/lbl_ProductCurrency"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:gravity="bottom"
android:text="QAR"
android:textAlignment="center"
android:textColor="#color/purple"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<View
android:id="#+id/lbl_ProductSeparator"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:layout_margin="4dp"
android:background="#color/purple" />
<TextView
android:id="#+id/lbl_ProductCategory"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="top"
android:text="International"
android:textAlignment="center"
android:textColor="#color/purple"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="16dp" />
</LinearLayout>
ProductButtonAdpater class:
public class ProductButtonAdaptor extends ArrayAdapter<ProductModel> implements AdapterView.OnItemClickListener
{
private Context context;
private final List<ProductModel> productModels;
private int selected = -1;
public ProductButtonAdaptor(Context context, List<ProductModel> productValues)
{
super(context, R.layout.button_product, productValues);
this.context = context;
this.productModels = productValues;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null)
{
gridView = new View(context);
// get layout from button_product.xml
gridView = inflater.inflate(R.layout.button_product, null);
// set value into textview
TextView lbl_ProductName = (TextView)gridView.findViewById(R.id.lbl_ProductName);
TextView lbl_ProductCurrency = (TextView)gridView.findViewById(R.id.lbl_ProductCurrency);
TextView lbl_ProductCategory = (TextView)gridView.findViewById(R.id.lbl_ProductCategory);
lbl_ProductName.setText(productModels.get(position).getCode());
lbl_ProductCurrency.setText(productModels.get(position).getCurrency());
lbl_ProductCategory.setText(productModels.get(position).getCategoryName());
}
else
{
gridView = (View)convertView;
}
if (selected == position)
{
TextView lbl_ProductName = (TextView)gridView.findViewById(R.id.lbl_ProductName);
TextView lbl_ProductCurrency = (TextView)gridView.findViewById(R.id.lbl_ProductCurrency);
TextView lbl_ProductCategory = (TextView)gridView.findViewById(R.id.lbl_ProductCategory);
View lbl_ProductSeperator = (View)gridView.findViewById(R.id.lbl_ProductSeparator);
LinearLayout pnl_ProductButton = (LinearLayout)gridView.findViewById(R.id.pnl_ProudctButton);
lbl_ProductName.setTextColor(ContextCompat.getColor(context, R.color.vodafone_white));
lbl_ProductCurrency.setTextColor(ContextCompat.getColor(context, R.color.vodafone_white));
lbl_ProductCategory.setTextColor(ContextCompat.getColor(context, R.color.vodafone_white));
lbl_ProductSeperator.setBackgroundColor(ContextCompat.getColor(context, R.color.vodafone_white));
pnl_ProductButton.setBackground(ResourcesCompat.getDrawable(context.getResources(), R.drawable.btn_product_red, null));
}
else
{
//setup the other cells
TextView lbl_ProductName = (TextView)gridView.findViewById(R.id.lbl_ProductName);
TextView lbl_ProductCurrency = (TextView)gridView.findViewById(R.id.lbl_ProductCurrency);
TextView lbl_ProductCategory = (TextView)gridView.findViewById(R.id.lbl_ProductCategory);
View lbl_ProductSeperator = (View)gridView.findViewById(R.id.lbl_ProductSeparator);
LinearLayout pnl_ProductButton = (LinearLayout)gridView.findViewById(R.id.pnl_ProudctButton);
lbl_ProductName.setTextColor(ContextCompat.getColor(context, R.color.vodafone_purple));
lbl_ProductCurrency.setTextColor(ContextCompat.getColor(context, R.color.vodafone_purple));
lbl_ProductCategory.setTextColor(ContextCompat.getColor(context, R.color.vodafone_purple));
lbl_ProductSeperator.setBackgroundColor(ContextCompat.getColor(context, R.color.vodafone_purple));
pnl_ProductButton.setBackground(ResourcesCompat.getDrawable(context.getResources(), R.drawable.btn_product_white, null));
}
return gridView;
}
#Override
public int getCount()
{
return productModels.size();
}
#Override
public ProductModel getItem(int position)
{
return productModels.get(position);
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
selected = position;
this.notifyDataSetChanged();
//adapter.notifyDataChanged();
//parent.invalidate();
//view.invalidate();
}
}
this is the updated code
I think you want to do it programatically (not styles).
So,
You have to declare a int selected; variable which stores index of a selected cell (or -1 if none is selected). Then, you have to implement onClickListener on each cell and change selected value when any element is tapped and redraw all data grid cells using notify... method.
After that, do not forget to change the setup block of colors and other parameters of each sell in the correspond method of the adapter.
inside onCreate() method:
gridview.setOnItemClickListener(adapter);
your class:
public class ProductButtonAdaptor extends BaseAdapter implemets onItemClickListener {
private Context context;
private final ProductModel[] productModels;
private int selected = -1;
public ProductButtonAdaptor(Context context, ProductModel[] productValues) {
this.context = context;
this.productModels = productValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from button_product.xml
gridView = inflater.inflate(R.layout.button_product, null);
// set value into textview
TextView lbl_ProductName = (TextView) gridView.findViewById(R.id.lbl_ProductName);
TextView lbl_ProductCurrency = (TextView) gridView.findViewById(R.id.lbl_ProductCurrency);
TextView lbl_ProductCategory = (TextView) gridView.findViewById(R.id.lbl_ProductCategory);
lbl_ProductName.setText(productModels[position].Name);
lbl_ProductCurrency.setText(productModels[position].Currency);
lbl_ProductCategory.setText(productModels[position].CategoryName);
} else {
gridView = (View) convertView;
}
if (selected == position) {
//setup selected cell
//for example
gridView.setBackgroundColor(Color.red);
} else {
//setup the other cells
gridView.setBackgroundColor(Color.white);
}
return gridView;
}
#Override
public int getCount() {
return productModels.length;
}
#Override
public Object getItem(int position) {
return productModels[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selected = position;
adapter.notifyDataChanged();
//you can pass the grid as a paramater of constructor if you need it
grid.invalidateViews();
}
Basically all I'm trying to do is have a button appear in a row, when the row is selected. I have tried many methods, and they all seem to work including the one below...I get the ImageView but calling setVisibility() on it does nothing:
ListFragment
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
setSellButton();
}
public View getViewForPosition(int position){
int relativePos = position - listview.getFirstVisiblePosition();
if( relativePos < 0 || relativePos > listview.getChildCount()){
return null;
}
return listview.getChildAt(relativePos);
}
public void setSellButton() {
View x = getViewForPosition(Constants.lastSelection);
ImageView y = (ImageView)x.findViewById(R.id.ivSell);
y.setVisibility(View.VISIBLE);
}
No errors, and debug shows that everything is right in setSellButton() but setVisibility() never works. Is this something I can only do in the adapter? Thanks!
Edit: Row Layout XML as requested
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="#drawable/thelist"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ivStar"
android:src="#drawable/liststar"
android:layout_weight="1" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_weight="10">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvValue"
android:textSize="13sp" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/coin"
android:id="#+id/ivSell"
android:visibility="invisible" />
</LinearLayout>
Edit: Adapter
public class InventoryAdapter extends ArrayAdapter<Integer> {
private final Context context;
private final ArrayList<String> names;
private final ArrayList<Integer> stock;
LayoutInflater inflater;
View rowView;
String stockText;
TextView textView;
TextView textView2;
ImageView iv;
public InventoryAdapter(Context context, ArrayList<String> names, ArrayList<Integer> stock) {
super(context, R.layout.inv_rowlayout, stock);
this.context = context;
this.names = names;
this.stock = stock;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.inv_rowlayout, parent, false);
String stockNumber = stock.get(position).toString();
final String name = names.get(position);
textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(names.get(position));
textView2 = (TextView) rowView.findViewById(R.id.tvValue);
textView2.setText(stockNumber);
iv = (ImageView)rowView.findViewById(R.id.ivSell);
return rowView;
}
}
Well. First of all allow me to adapt your row xml. It's better to use RelativeLayout instead of LinearLayout. It's more flexible.
row_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#drawable/thelist"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:minHeight="#dimen/listView_minHeight">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ivStar"
android:contentDescription="#string/list_item_image_desc"
android:layout_gravity="center_vertical"
android:layout_centerVertical="true"
android:src="#drawable/liststar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/ivStar"
android:layout_toEndOf="#+id/ivStar"
android:layout_alignParentTop="true"
android:id="#+id/label"
android:text="New Text"
android:textSize="15sp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textSize="13sp"
android:id="#+id/tvValue"
android:layout_below="#+id/label"
android:layout_toRightOf="#+id/ivStar"
android:layout_toEndOf="#+id/ivStar"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ivSell"
android:src="#drawable/coin"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:visibility="invisible" />
</RelativeLayout>
You're probably subclassing BaseAdapter. What you'll have to do is to create a method there to change the visibility of your ImageView. Then when you have your item clicked, you can get the adapter and call that method.
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private Context context_;
private ArrayList<YourDataHolder> items;
private YourDataHolder lastHolder = null;
private final String LOG_TAG = CustomAdapter.class.getSimpleName();
public CustomAdapter(Context context, ArrayList<YourDataHolder> items) {
this.context_ = context;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int i) {
return items.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context_.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row_layout, null);
}
final TextView tv_label = (TextView) convertView.findViewById(R.id.label);
final TextView tv_value = (TextView) convertView.findViewById(R.id.tvValue);
final ImageView iv_star = (ImageView) convertView.findViewById(R.id.ivSell);
YourDataHolder holder = items.get(position);
tv_label.setText(holder.getLabel());
tv_value.setText(holder.getValue());
if (holder.isImageVisible() && iv_star.getVisibility() == ImageView.INVISIBLE) {
iv_star.setVisibility(ImageView.VISIBLE);
}
else if (iv_star.getVisibility() == ImageView.VISIBLE) {
iv_star.setVisibility(ImageView.INVISIBLE);
}
return convertView;
}
public void onItemSelect(int position) {
if (position < items.size()) {
YourDataHolder holder = items.get(position);
holder.setVisibility(true);
if (lastHolder != null) {
lastHolder.setVisibility(false);
}
lastHolder = holder;
}
}
}
Assuming you're using a data holder like this one:
YourDataHolder.java
public class YourDataHolder {
private String label;
private String value;
private boolean isVisible;
public YourDataHolder(String label, String value) {
this.label = label;
this.value = value;
isVisible = false;
}
public String getLabel() {
return label;
}
public String getValue() {
return value;
}
public boolean getVisibility() {
return isVisible;
}
public void setVisibility(boolean isVisible) {
this.isVisible = isVisible;
}
}
And then, on your onItemClick(), do the magic.
YourFragment.java
private CustomAdapter adapter;
// Rest of the Fragment. Where you create the adapter, and assign it to the ListView.
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
adapter.onItemSelect(position);
adapter.notifyDataSetChanged();
}
My guess is, that after the click on the item you need to call the notifyDataSetChanged method on the adapter. And then render the button on the selected list view item.
It is something that I would do in the adapter, having direct access to the imageview would make the process easier. I previously attempted to do it from the listview level and was unable to do to the conditions I had setup.
I have custom ListView layout with a TextView and CheckBox. Everything works fine.
What I want is, when I click on the CheckBox or TextView (on the single View from ListView) both should behave like one object. (I can click on the CheckBox and it does not effect the TextView and TextView has no effect on CheckBox.) Code has no problem.
I have implemented all possible solutions but problem is still there. (One single click on every object of list should consider ONE COMPLETE CLICK for complete row.) I hope I explained very well.
MAIN ACTIVITY
package com.example.smsplanner;
public class SMSPlanner extends ListActivity {
ListView contactsListView;
private String TAG = "SMSPlanner"; CheckBox check;
int count;
List<ContactInfo> list = new ArrayList<ContactInfo>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ph = new String[3];
phType = new String[3];
LoadContactListFromPhone();
ContactsAdapter contactadAdapter = new ContactsAdapter(this, list);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setListAdapter(contactadAdapter);
}
#Override
public void onListItemClick(ListView parent, View v, int position, long id)
{
TextView tx =(TextView)v.findViewById(R.id.firstname);
TextView ph =(TextView)v.findViewById(R.id.phone);
Toast.makeText(this, tx.getText().toString() + " " + ph.getText().toString() + " " + Integer.toString(count), Toast.LENGTH_SHORT).show();
}
final class ContactHolder{
TextView txtviewfirstname;
CheckBox chkselected;
TextView txtviewphone;
}
void LoadContactListFromPhone()
{
loadlistandreturns();
}
void call()
{
Toast toast = Toast.makeText(this, "Called...",Toast.LENGTH_LONG);
toast.show();
}
}
CUSTOM ADAPTER
public class ContactsAdapter extends ArrayAdapter<ContactInfo>
{
private final Activity context;
int resourceid;
List<ContactInfo> list = null;
public ContactsAdapter(Activity context, List<ContactInfo> list) {
super(context, R.layout.contactrow, list);
this.context = context;
this.list = list;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
final class ContactHolder{
TextView txtviewfirstname;
CheckBox chkselected;
TextView txtviewphone;
}
}
LAYOUT
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<RadioGroup
android:id="#+id/rgStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="15"
android:orientation="vertical" >
<TextView
android:id="#+id/firstname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RadioGroup>
<RadioGroup
android:id="#+id/rgStyle2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="85"
android:orientation="vertical" >
<CheckBox
android:id="#+id/check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>
</RadioGroup>
</LinearLayout>
1. Bydefault all the Rows of the ListView are enabled to listen to click....
You must implement onItemClickListener() for the ListView....
See this example:
http://www.mkyong.com/android/android-listview-example/
you can use a CheckedTextView, or you can create a Checkable Layout, like this one :
http://tokudu.com/2010/android-checkable-linear-layout/
i think you should make adapter as
public class ContactsAdapter extends BaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public Object getItem(int postion) {
return mlist.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// to open the selected file in resp
// do your work here
}});
chkselected .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(context,// "checked is clicke="+pos, 12).show();
if (chkselected.isChecked())
{
// do your work here
} else {
// do your work here
}
}
});
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
}
My problem is that my listView repeat all items of my layout.
I need to have the edit text and the button at the bottom of this listView.
But here the edit text and button is repeated for each row of listView and i don't know why.
If someone could help me
The activity :
public class MessActivity extends ListActivity
{
public Channel chan;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(Network.getInstance().Messages);
Bundle b = getIntent().getExtras();
this.chan = (Channel) b.get("chanSelected");
//add all message of selected chan to the messAdapter
for (int i = 0 ; i < this.chan.getListMessage().size() ; i++) {
Message mess = this.chan.getListMessage().get(i);
Network.getInstance().addMessage(mess);
}
setContentView(R.layout.mess_list);
}
}
The adapter :
public class MessageAdapter extends ArrayAdapter<Message>
{
private Context context;
private int textViewResourceId;
public MessageAdapter(Context context, int textViewResourceId)
{
super(context, textViewResourceId);
this.context = context;
this.textViewResourceId = textViewResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
MessHolder holder = null;
//row null
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder = new MessHolder();
holder.texte = (TextView)row.findViewById(R.id.listMess);
row.setTag(holder);
}
else
{
holder = (MessHolder)row.getTag();
}
Message mess = getItem(position);
holder.texte.setText(mess.getText());
return row;
}
static class MessHolder
{
TextView texte;
}
}
The layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#android:id/list">
</ListView>
<TextView
android:id="#+id/listMess"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom" >
//edittext
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Envoyer" />
</LinearLayout>
</LinearLayout>
Thank you for your help.
Use ListView method addFooter(View v).
You can define xml layout for this view and inflate it:
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View footer = (View)inflater.inflate(R.layout.footer, lv, false);
lv.addFooterView(footer, null, false);
Firstly Reason of this issue is Listview Reuses its view when scrolls , So we should have a logic in our get view to manage that
if(convertview == null)
{
//code here
}else {
//code here
}
, but if you dont want to reuse then you can use following :
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return 500;
}