Android GridView 0 position issue












2















I know this question is already asked but I didn't get any proper solution to solve 0 position issue. I am working on Gridview and created one adaptor (ButtonAdapter) for this. so I have 4 buttons and If I click any one of them then selected button should change bg color and remaining all three buttons should have reduced opacity let say 0.3 but My problem is that my first button does not change the alpha. So what is the exact issue?



package adaptor;



    import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.appsee.Appsee;
import com.regiumconsulting.donationssumup.MainActivity;
import com.regiumconsulting.donationssumup.R;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class ButtonAdapter extends BaseAdapter {

private Context context;
private final String mobileValues;
private BigDecimal amnt;
private Button chosenone = new Button[1];
private Button tempBtn = null;
private BigDecimal amount;
private Map<Button, BigDecimal> buttons = new HashMap<>();

public ButtonAdapter(Context context, String mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
tempBtn = new Button[mobileValues.length];
}

public View getView(final int position, View convertView, ViewGroup parent) {

View gridView = null;

if (convertView == null) {

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

gridView = new View(context);
// get layout from item_adaptor.xmlor.xml
gridView = inflater.inflate(R.layout.item_adaptor, null);

} else {
gridView = convertView;
}

// set image based on selected text
final Button btnView = (Button) gridView
.findViewById(R.id.grid_item_image);

setBtnAmnt(position, btnView);

btnView.setBackgroundResource(R.drawable.button_bg_dashed_border);
tempBtn[position] = btnView;

btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

for (int i = 0; i < tempBtn.length; i++) {//set alpha to 0.5 for all buttons
tempBtn[i].setAlpha(0.3f);//updating only three buttons bg not for 1st one.
}
chosenone[0] = btnView;

amount = buttons.get(chosenone[0]);

chosenone[0].setBackgroundResource(R.drawable.button_bg_border);//set alpha 1 to selected one and set selected color
chosenone[0].setAlpha(1f);

}
}
});
return gridView;
}

@Override
public int getCount() {
return mobileValues.length;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

private void setBtnAmnt(int position, Button imageView) {
amnt = new BigDecimal(mobileValues[position]);
imageView.setText(String.format("£ %s", amnt.toString()));
buttons.put(imageView, amnt);

}
}

Also, I have created on method inside main activity let say screenClick.

public void screenClick() {
gridView.setAdapter(adaptor);
adaptor.notifyDataSetChanged();
}

So If I call this method and then perform the same operation as mentioned above then only it will work and it will set 0.3 alpha for a 1st button. So what is the issue I don't understand?

I have also attached a screenshot link where a 1st button doesn't have reduced opacity.
https://imgur.com/a/bofGMk4









share|improve this question























  • You said you have 4 buttons in grid_item_image . But i am seeing only one button here which is R.id.grid_item_image.

    – ADM
    Jan 2 at 8:00











  • @ADM Yes in Adaptor I have defined one button and it depends on String mobileValues length, which I am passing through mainactivity class as a constructor.

    – Ashish Jain
    Jan 2 at 8:23











  • So where is the 4 buttons ? Your question is quite unclear . Attach an screen shot of your grid view .

    – ADM
    Jan 2 at 8:28











  • @ADM please see this imgur.com/a/bofGMk4

    – Ashish Jain
    Jan 2 at 8:29













  • this is One button = one grid item . Then why are having this array of Button in your adapter . I can provide you solution but i can not understand why you have used those Button array.

    – ADM
    Jan 2 at 8:34
















2















I know this question is already asked but I didn't get any proper solution to solve 0 position issue. I am working on Gridview and created one adaptor (ButtonAdapter) for this. so I have 4 buttons and If I click any one of them then selected button should change bg color and remaining all three buttons should have reduced opacity let say 0.3 but My problem is that my first button does not change the alpha. So what is the exact issue?



package adaptor;



    import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.appsee.Appsee;
import com.regiumconsulting.donationssumup.MainActivity;
import com.regiumconsulting.donationssumup.R;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class ButtonAdapter extends BaseAdapter {

private Context context;
private final String mobileValues;
private BigDecimal amnt;
private Button chosenone = new Button[1];
private Button tempBtn = null;
private BigDecimal amount;
private Map<Button, BigDecimal> buttons = new HashMap<>();

public ButtonAdapter(Context context, String mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
tempBtn = new Button[mobileValues.length];
}

public View getView(final int position, View convertView, ViewGroup parent) {

View gridView = null;

if (convertView == null) {

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

gridView = new View(context);
// get layout from item_adaptor.xmlor.xml
gridView = inflater.inflate(R.layout.item_adaptor, null);

} else {
gridView = convertView;
}

// set image based on selected text
final Button btnView = (Button) gridView
.findViewById(R.id.grid_item_image);

setBtnAmnt(position, btnView);

btnView.setBackgroundResource(R.drawable.button_bg_dashed_border);
tempBtn[position] = btnView;

btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

for (int i = 0; i < tempBtn.length; i++) {//set alpha to 0.5 for all buttons
tempBtn[i].setAlpha(0.3f);//updating only three buttons bg not for 1st one.
}
chosenone[0] = btnView;

amount = buttons.get(chosenone[0]);

chosenone[0].setBackgroundResource(R.drawable.button_bg_border);//set alpha 1 to selected one and set selected color
chosenone[0].setAlpha(1f);

}
}
});
return gridView;
}

@Override
public int getCount() {
return mobileValues.length;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

private void setBtnAmnt(int position, Button imageView) {
amnt = new BigDecimal(mobileValues[position]);
imageView.setText(String.format("£ %s", amnt.toString()));
buttons.put(imageView, amnt);

}
}

Also, I have created on method inside main activity let say screenClick.

public void screenClick() {
gridView.setAdapter(adaptor);
adaptor.notifyDataSetChanged();
}

So If I call this method and then perform the same operation as mentioned above then only it will work and it will set 0.3 alpha for a 1st button. So what is the issue I don't understand?

I have also attached a screenshot link where a 1st button doesn't have reduced opacity.
https://imgur.com/a/bofGMk4









share|improve this question























  • You said you have 4 buttons in grid_item_image . But i am seeing only one button here which is R.id.grid_item_image.

    – ADM
    Jan 2 at 8:00











  • @ADM Yes in Adaptor I have defined one button and it depends on String mobileValues length, which I am passing through mainactivity class as a constructor.

    – Ashish Jain
    Jan 2 at 8:23











  • So where is the 4 buttons ? Your question is quite unclear . Attach an screen shot of your grid view .

    – ADM
    Jan 2 at 8:28











  • @ADM please see this imgur.com/a/bofGMk4

    – Ashish Jain
    Jan 2 at 8:29













  • this is One button = one grid item . Then why are having this array of Button in your adapter . I can provide you solution but i can not understand why you have used those Button array.

    – ADM
    Jan 2 at 8:34














2












2








2








I know this question is already asked but I didn't get any proper solution to solve 0 position issue. I am working on Gridview and created one adaptor (ButtonAdapter) for this. so I have 4 buttons and If I click any one of them then selected button should change bg color and remaining all three buttons should have reduced opacity let say 0.3 but My problem is that my first button does not change the alpha. So what is the exact issue?



package adaptor;



    import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.appsee.Appsee;
import com.regiumconsulting.donationssumup.MainActivity;
import com.regiumconsulting.donationssumup.R;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class ButtonAdapter extends BaseAdapter {

private Context context;
private final String mobileValues;
private BigDecimal amnt;
private Button chosenone = new Button[1];
private Button tempBtn = null;
private BigDecimal amount;
private Map<Button, BigDecimal> buttons = new HashMap<>();

public ButtonAdapter(Context context, String mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
tempBtn = new Button[mobileValues.length];
}

public View getView(final int position, View convertView, ViewGroup parent) {

View gridView = null;

if (convertView == null) {

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

gridView = new View(context);
// get layout from item_adaptor.xmlor.xml
gridView = inflater.inflate(R.layout.item_adaptor, null);

} else {
gridView = convertView;
}

// set image based on selected text
final Button btnView = (Button) gridView
.findViewById(R.id.grid_item_image);

setBtnAmnt(position, btnView);

btnView.setBackgroundResource(R.drawable.button_bg_dashed_border);
tempBtn[position] = btnView;

btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

for (int i = 0; i < tempBtn.length; i++) {//set alpha to 0.5 for all buttons
tempBtn[i].setAlpha(0.3f);//updating only three buttons bg not for 1st one.
}
chosenone[0] = btnView;

amount = buttons.get(chosenone[0]);

chosenone[0].setBackgroundResource(R.drawable.button_bg_border);//set alpha 1 to selected one and set selected color
chosenone[0].setAlpha(1f);

}
}
});
return gridView;
}

@Override
public int getCount() {
return mobileValues.length;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

private void setBtnAmnt(int position, Button imageView) {
amnt = new BigDecimal(mobileValues[position]);
imageView.setText(String.format("£ %s", amnt.toString()));
buttons.put(imageView, amnt);

}
}

Also, I have created on method inside main activity let say screenClick.

public void screenClick() {
gridView.setAdapter(adaptor);
adaptor.notifyDataSetChanged();
}

So If I call this method and then perform the same operation as mentioned above then only it will work and it will set 0.3 alpha for a 1st button. So what is the issue I don't understand?

I have also attached a screenshot link where a 1st button doesn't have reduced opacity.
https://imgur.com/a/bofGMk4









share|improve this question














I know this question is already asked but I didn't get any proper solution to solve 0 position issue. I am working on Gridview and created one adaptor (ButtonAdapter) for this. so I have 4 buttons and If I click any one of them then selected button should change bg color and remaining all three buttons should have reduced opacity let say 0.3 but My problem is that my first button does not change the alpha. So what is the exact issue?



package adaptor;



    import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.appsee.Appsee;
import com.regiumconsulting.donationssumup.MainActivity;
import com.regiumconsulting.donationssumup.R;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class ButtonAdapter extends BaseAdapter {

private Context context;
private final String mobileValues;
private BigDecimal amnt;
private Button chosenone = new Button[1];
private Button tempBtn = null;
private BigDecimal amount;
private Map<Button, BigDecimal> buttons = new HashMap<>();

public ButtonAdapter(Context context, String mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
tempBtn = new Button[mobileValues.length];
}

public View getView(final int position, View convertView, ViewGroup parent) {

View gridView = null;

if (convertView == null) {

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

gridView = new View(context);
// get layout from item_adaptor.xmlor.xml
gridView = inflater.inflate(R.layout.item_adaptor, null);

} else {
gridView = convertView;
}

// set image based on selected text
final Button btnView = (Button) gridView
.findViewById(R.id.grid_item_image);

setBtnAmnt(position, btnView);

btnView.setBackgroundResource(R.drawable.button_bg_dashed_border);
tempBtn[position] = btnView;

btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

for (int i = 0; i < tempBtn.length; i++) {//set alpha to 0.5 for all buttons
tempBtn[i].setAlpha(0.3f);//updating only three buttons bg not for 1st one.
}
chosenone[0] = btnView;

amount = buttons.get(chosenone[0]);

chosenone[0].setBackgroundResource(R.drawable.button_bg_border);//set alpha 1 to selected one and set selected color
chosenone[0].setAlpha(1f);

}
}
});
return gridView;
}

@Override
public int getCount() {
return mobileValues.length;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

private void setBtnAmnt(int position, Button imageView) {
amnt = new BigDecimal(mobileValues[position]);
imageView.setText(String.format("£ %s", amnt.toString()));
buttons.put(imageView, amnt);

}
}

Also, I have created on method inside main activity let say screenClick.

public void screenClick() {
gridView.setAdapter(adaptor);
adaptor.notifyDataSetChanged();
}

So If I call this method and then perform the same operation as mentioned above then only it will work and it will set 0.3 alpha for a 1st button. So what is the issue I don't understand?

I have also attached a screenshot link where a 1st button doesn't have reduced opacity.
https://imgur.com/a/bofGMk4






android gridview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 7:26









Ashish JainAshish Jain

114110




114110













  • You said you have 4 buttons in grid_item_image . But i am seeing only one button here which is R.id.grid_item_image.

    – ADM
    Jan 2 at 8:00











  • @ADM Yes in Adaptor I have defined one button and it depends on String mobileValues length, which I am passing through mainactivity class as a constructor.

    – Ashish Jain
    Jan 2 at 8:23











  • So where is the 4 buttons ? Your question is quite unclear . Attach an screen shot of your grid view .

    – ADM
    Jan 2 at 8:28











  • @ADM please see this imgur.com/a/bofGMk4

    – Ashish Jain
    Jan 2 at 8:29













  • this is One button = one grid item . Then why are having this array of Button in your adapter . I can provide you solution but i can not understand why you have used those Button array.

    – ADM
    Jan 2 at 8:34



















  • You said you have 4 buttons in grid_item_image . But i am seeing only one button here which is R.id.grid_item_image.

    – ADM
    Jan 2 at 8:00











  • @ADM Yes in Adaptor I have defined one button and it depends on String mobileValues length, which I am passing through mainactivity class as a constructor.

    – Ashish Jain
    Jan 2 at 8:23











  • So where is the 4 buttons ? Your question is quite unclear . Attach an screen shot of your grid view .

    – ADM
    Jan 2 at 8:28











  • @ADM please see this imgur.com/a/bofGMk4

    – Ashish Jain
    Jan 2 at 8:29













  • this is One button = one grid item . Then why are having this array of Button in your adapter . I can provide you solution but i can not understand why you have used those Button array.

    – ADM
    Jan 2 at 8:34

















You said you have 4 buttons in grid_item_image . But i am seeing only one button here which is R.id.grid_item_image.

– ADM
Jan 2 at 8:00





You said you have 4 buttons in grid_item_image . But i am seeing only one button here which is R.id.grid_item_image.

– ADM
Jan 2 at 8:00













@ADM Yes in Adaptor I have defined one button and it depends on String mobileValues length, which I am passing through mainactivity class as a constructor.

– Ashish Jain
Jan 2 at 8:23





@ADM Yes in Adaptor I have defined one button and it depends on String mobileValues length, which I am passing through mainactivity class as a constructor.

– Ashish Jain
Jan 2 at 8:23













So where is the 4 buttons ? Your question is quite unclear . Attach an screen shot of your grid view .

– ADM
Jan 2 at 8:28





So where is the 4 buttons ? Your question is quite unclear . Attach an screen shot of your grid view .

– ADM
Jan 2 at 8:28













@ADM please see this imgur.com/a/bofGMk4

– Ashish Jain
Jan 2 at 8:29







@ADM please see this imgur.com/a/bofGMk4

– Ashish Jain
Jan 2 at 8:29















this is One button = one grid item . Then why are having this array of Button in your adapter . I can provide you solution but i can not understand why you have used those Button array.

– ADM
Jan 2 at 8:34





this is One button = one grid item . Then why are having this array of Button in your adapter . I can provide you solution but i can not understand why you have used those Button array.

– ADM
Jan 2 at 8:34












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002681%2fandroid-gridview-0-position-issue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002681%2fandroid-gridview-0-position-issue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules