Listview shows previous item as checked android - java

I have alertdialog with listview and listview header with "check all" and "uncheck all" button and listview footer with "Done" button. Listview is CHOICE_MODE_MULTIPLE with arrayList value EavesList. EavesList will be cross checked with another arrayList selectedItems to show those items as checked. the issue i am facing is Listview shows correct value but shows previous item as checked.
Here's my code:
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setTitle("Select Category List");
final ListView modeList = new ListView(getActivity());
LayoutInflater inflater = LayoutInflater.from(getActivity());
View rootView = inflater.inflate(
R.layout.listview_header_eavesdrop, null);
modeList.addHeaderView(rootView);
Button btnDone = new Button(getActivity());
btnDone.setText("Done");
btnDone.setTextColor(getActivity().getResources().getColor(
android.R.color.white));
final ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_multiple_choice,
android.R.id.text1, EavesList);
modeList.addFooterView(btnDone);
modeList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
modeList.setAdapter(modeAdapter);
if (selectedItems.size() != 0) {
for (int i = 0; i < EavesList.size(); i++) {
if (selectedItems.size() != 0) {
// modeList.setItemChecked(2, true);
for (int j = 0; j < selectedItems.size(); j++) {
if (EavesList.get(i).contains(
selectedItems.get(j))) {
modeList.setItemChecked(i, true);
Log.e("Selected Item ",
"there Values true "
+ selectedItems.get(j));
j = selectedItems.size() + 2;
}
}
} else {
Log.e("Selected Item ", "No Values");
}
}
} else {
for (int i = 0; i < EavesList.size(); i++) {
if (category.size() != 0) {
for (int j = 0; j < category.size(); j++) {
if (EavesList_id.get(i).contains(category.get(j))) {
modeList.setItemChecked(i, true);
Log.e("List " + EavesList_id.get(i), ""+ category.get(j));
Log.e("Selected category Item ",
"there Values true "
+ category.get(j));
} else {
if (j == category.size()) {
modeList.setItemChecked(i, false);
Log.e("Selected category Item ",
"there Values false "
+ category.get(j));
}
}
}
} else {
Log.e("Selected Item ", "No Values");
}
}
}
builder.setView(modeList);
final Dialog dialog = builder.create();
btnDone.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
selectedItems.clear();
eavesdropping.setText("");
SparseBooleanArray checked = modeList
.getCheckedItemPositions();
selected_community_id = new String[checked.size()];
String[] evas_id;
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i))
selectedItems.add(EavesList.get(position));
selected_community_id[i] = EavesList_id
.get(position);
}
for (int i = 0; i < selected_community_id.length; i++) {
eavesdropping_hide.append(selected_community_id[i]);
if (i != selected_community_id.length ) {
eavesdropping_hide.append(",");
}
}
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
eavesdropping.append(outputStrArr[i]);
eavesdropping.append(" | ");
}
dialog.dismiss();
}
});
dialog.show();
P.S: Listview shows proper content but the checked item of listview is always one previous item is always checked and the item to be checked is unchecked.

Why don't try this method on your submit button click
SparseBooleanArray sparseBooleanArray = list.getCheckedItemPositions();
List<String> lstCheck= new ArrayList<String>();
for (int i = 0; i < list.getCount(); i++) {
if (sparseBooleanArray.get(i) == true) {
lstCheck.add(list.getItemAtPosition(i).toString());
}
}
Now as lstCheck will result in only the checked values compare it with your EavesList and find those items that do not exist and you will also have you non-selected values

Use the following: (it is pseudo code):
ArrayList<String> data;
ArrayList<String> checkboxes;
.....
listview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
if(chockBoxes.containds(data.get(position))){
chockBoxes.remove(data.get(position));
}else{
chockBoxes.add(data.get(position))
}
}
});
...
now in your adapter getView():
if(checkBoxes.contains(data.get(position))){
checkBox.setChecked(true);
}else{
checkBox.setChecked(false);
}

Related

when i clicked on two imageviews which are overlapped, one of them gives me feedback the other does not

Here is codes, and i am trying to receive a feedback from imageviews which are overlapped. One of the imageviews return isPressed method true, but the other one return me false. They are overlapped, so when i clicked one of them, both of their isPressed method should be true, i guess. Where am i wrong
public class MainActivity extends AppCompatActivity {
private final String TAG = this.getClass().getSimpleName();
ImageView[] imageViews;
Button button;
int a = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridLayout gridLayout = findViewById(R.id.grid_layout);
GridLayout.LayoutParams[] layoutParams = new GridLayout.LayoutParams[9];
imageViews = new ImageView[18];
button = findViewById(R.id.button);
for (int i = 0, j = 0, k = -1, m = 0; j < 3 & k < 3 & m < imageViews.length || i < layoutParams.length; m++) {
if (m == 18) {
break;
}
if (m == 0) {
k++;
}
layoutParams[i] = new GridLayout.LayoutParams();
layoutParams[i].setGravity(Gravity.CENTER);
layoutParams[i].setMargins(25, 25, 25, 25);
layoutParams[i].columnSpec = GridLayout.spec(k, GridLayout.CENTER);
layoutParams[i].rowSpec = GridLayout.spec(j, GridLayout.CENTER);
imageViews[m] = new ImageView(MainActivity.this);
imageViews[m].setLayoutParams(layoutParams[i]);
gridLayout.addView(imageViews[m]); //---
if (m % 2 == 0) {
imageViews[m].setImageResource(R.drawable.first_image);
imageViews[m].setAlpha(1f);
} else {
imageViews[m].setImageResource(R.drawable.second_image);
imageViews[m].setAlpha(0.5f);
}
if (m % 2 == 1 & m != 0) {
j++;
i++;
}
if (m % 6 == 5 & m != 0) {
k++;
}
if (j == 3) {
j = 0;
}
}
actionView();
}
public void actionView() {
for (int m = 0; m < imageViews.length; m++) {
imageViews[m].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, " " + a + " " + "\n", Toast.LENGTH_SHORT).show();
Log.e(TAG, imageViews[1].isPressed() + "");
Log.e(TAG, imageViews[0].isPressed() + "");
Log.e(TAG, imageViews[1].isEnabled() + "");
Log.e(TAG, imageViews[0].isEnabled() + "");
Log.e(TAG, "--------------------");
a++;
for (int x = 0; x < imageViews.length; x++) {
if (imageViews[x].isPressed()) {
Log.e(TAG, x + " Image is clicked");
Log.e(TAG, " *****************");
}
}
}
});
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a = 0;
}
});
}
}
If the views are overlapped and click is enabled for both only the view on top will receive the click event.
If you want to call on click event of both, call performClick of the view in background form the onclick listener of the view in front.
Like imageviewInBack.performClick()

RecyclerView snapshot

I am having problem in snapshot of recyclerview.
How do i take snapshot of the few elements of recyclerview. like from 1 to 20, then 20 to 40 and so on.
kindly help me
i tried alot i can't take pictures dynamically of the recyclerview
public void onClick(View view)
{
//datamodel.setName(etname.getText().toString());
//datamodel.setAge((Integer.parseInt(etage.getText().toString())));
for (int i = 1; i <= 500; i++)
{
DataModel datamodel = new DataModel("umair", i);
data.add(datamodel);
}
adapter.notifyDataSetChanged();
}
private void slice()
{
slice = new ArrayList<>();
int strtnum = Integer.parseInt(start.getText().toString());
int endnum = Integer.parseInt(end.getText().toString());
for (int i = strtnum; i < endnum; i++)
{
Log.d("test", "" + start + "" + end);
DataModel item = (DataModel)data.get(i);
DataModel item2 = new DataModel(item.getName(), item.getAge());
slice.add(item2);
final Adapter adapter = new Adapter(slice);
recyclerView.setAdapter(adapter);
Toast.makeText(MainActivity.this, "start" + start + "end" + end, Toast.LENGTH_LONG);
}
}

there is no text in radio buttons on screen

in this program, I want to show on screen radio buttons with rooms name
buttonList = new RadioButton[roomNames.length];
btGroup = (RadioGroup) findViewById(R.id.roomList);
RadioGroup.LayoutParams rprms;
for(int i = 0; i < roomNames.length; i++)
{
//buttonList[i] = new RadioButton(this);
RadioButton tempBT = new RadioButton(this);
System.out.println("room name number :"+ i + roomNames[i]);
rprms= new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
//tempBT.setText(roomNames[i].subSequence(0,roomNames[i].length()));
//tempBT.setTextColor(0);
tempBT.setId(i);
buttonList[i] = tempBT;
btGroup.addView(tempBT, rprms);
//buttonList[i].setText(roomNames[i].subSequence(0,roomNames[i].length()));
//buttonList[i].setTextColor(0);
//btGroup.addView(buttonList[i], rprms);
}
for (int f = 0; f < btGroup.getChildCount(); f++) {
((RadioButton) btGroup.getChildAt(f)).setText(roomNames[f].subSequence(0,roomNames[f].length()));
((RadioButton) btGroup.getChildAt(f)).setTextColor(0);
}
//saving the chosen room name
btGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(RadioGroup arg0, int selectedId) {
for(int j = 0; j < buttonList.length; j++)
{
if(btGroup.getCheckedRadioButtonId() == buttonList[j].getId())
{
roomNameSelected = roomNames[j];
}
}
}
});
but when I run this program I see the radio buttons but there is no text inside
Instead of
setTextColor(0);
Try
setTextColor(Color.BLACK);

how to count filled edit text from multiple edittext

In my app have twenty edit text,but I want to count filled edit text and that data goes in anther activity through an array. Like when I filled 3 edit text from twenty, that 3 edit text data goes next page and that 3 count goes to next page as an int.
this is my 1st java class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
mspin=(Spinner) findViewById(R.id.spinner1);
submit = (Button) findViewById(R.id.btn1);
layout = (LinearLayout) findViewById(R.id.linear);
lay = (LinearLayout) findViewById(R.id.li);
edt=(EditText) findViewById(R.id.ed2);
sc = (ScrollView) findViewById(R.id.sc1);
int no = 20;
allEds = new ArrayList<EditText>();
for (int i=1;i<=no;i++){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
edtAdd = new EditText(SelectActivity.this);
layout.setLayoutParams(params);
allEds.add(edtAdd);
edtAdd.setHint("Enter Name" + i);
edtAdd.setId(i);
layout.addView(edtAdd);
}
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (allEds.size()<=9) {
Intent data = new Intent(SelectActivity.this, HalfPieChartActivity.class);
String[] items = new String[allEds.size()];
String str = String.valueOf(allEds.size());
data.putExtra("size", str); //you don't need to keep this in loop as its same.
data.putExtra("edt", edt.getText().toString());
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
data.putExtra("edData" + j, items[j]);
}
startActivity(data);
}
else {
Intent data = new Intent(SelectActivity.this, FullPieChartActivity.class);
String[] items = new String[allEds.size()];
String str = String.valueOf(allEds.size());
data.putExtra("size", str);// this is the line where I sent that count
data.putExtra("edt", edt.getText().toString());
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
data.putExtra("edData" + j, items[j]);//here is filled data send line
}
startActivity(data);
}
}
});
}
}
When I click submit 20 show in next page as an int.I want to sent that 3data and 3 as an int.
Please help me
As you have asked for sample in comment, i am posting my answer with sample.
In onclicklistener of your submit button you can add following code.
myNoneEmptyEdittextCounter = 0;
for (int i=1;i<=no;i++)
{
myEt = (EditText) findViewById(i);
if(!TextUtils.isEmpty(myEt.getText().toString()))
{
myNoneEmptyEdittextCounter +=1;
}
}
//myNoneEmptyEdittextCounter is count of your filled edittexts
This method will return list of data and its size will give you the int you are looking for which is number of filled items.
In submit button click
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(edittextValues().isEmpty()) {
//nothing has been filled
} else {
//items in edittextValues() are your data.
edittextValues().size();//this is your int which is number of edit text filled.
}
}
}
I have mentioned in comments what each step does. Hope this helps.
Method edittextValues()
ArrayList<String> edittextValues() {
ArrayList<String> filledData = new ArrayList<>(); //initialise empty string array.
for (int i=1; i<=20; i++){ //loop through ids from 1 to 20
for (int j = 0; j< layout.getChildCount(); j++) { //loop through all the children of root layout
if(layout.getChildAt(j) instanceof EditText) { //filter EditText
EditText editText = (EditText) layout.getChildAt(j);
if(editText.getId() == i) { // filter the one which u programmatically added
if(!editText.getText().toString().isEmpty()) { // check if its not empty, that's the one you are looking
filledData.add(editText.getText().toString()); //add it to the list
}
}
}
}
}
return filledData; //return string list
}

android: adding dynamically with dynamically items

i have list item in each row like :
item1 item2 item3 item4 item5 item6
I need
item 1 item 2 item3
item 4 item5 item 6
...
I don't need to use GridView. My getView():
LinearLayout dynamicallyLayout = null;
boolean needCreated = true;
if (json.length() > 1) {
for (int j = 0; j < json.length(); j++) {
JSONObject classesJson = json.getJSONObject(j);
String name = classesJson.getString("name");
classChbx = new CheckBox(ctx);
classChbx.setText(name);
if (value == 1) classChbx.setChecked(true);
if (needCreated) {
dynamicallyLayout = new LinearLayout(ctx);
dynamicallyLayout.setOrientation(LinearLayout.HORIZONTAL);
holder.llChbTypes.addView(dynamicallyLayout);
needCreated = false;
}else {
if (j % 3 == 0) {
needCreated = true;
dynamicallyLayout.addView(classChbx);
dynamicallyLayout = null;
break;
}
}
if (dynamicallyLayout != null)
dynamicallyLayout.addView(classChbx);
}
}else {
classChbx = new CheckBox(ctx);
classChbx.setText(json.getJSONObject(0).getString("name"));
holder.llChbTypes.addView(classChbx);
}
but it look for low resolution like: item1, item2, item3.. that's all!
I have used this approach in my code.
for ( int i = 0 ; i < 4 ; i ++ )
{
LinearLayout layoutInWhichToAdd= (LinearLayout) rootView
.findViewById(R.id.layoutInWhichToAdd);
View toadd= orderDetails.getLayoutInflater().inflate(
R.layout.include_items_invieworder, layoutInWhichToAdd,
false);
layoutInWhichToAdd.addView(toadd);
}

Categories