0
votes

I am working on an app in android studio. My app has a listview, whiche has Edittext , textview and checkbox in its row. my proplem is when I have more than five items in my listvew, the listview lost focus, for example: when I press on checkbox on the 6th item , the textview shows me the name from the first item. I wish I could explain my proplem very well.

This is my adapter:

public AdapterListView(Context context, int resource, ArrayList<ObjectPeople> arraypeople) {

    super(context, resource);

    this.mContext = context;
    this.arrayPeople = arraypeople;
    this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override

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

    final ViewHolder holder;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item_listview, null);
        holder = new ViewHolder();
        holder.mission_name = (TextView) convertView.findViewById(R.id.mission_name);
        holder.mission_time = (TextView) convertView.findViewById(R.id.mission_time);
        holder.edit_time = (EditText) convertView.findViewById(R.id.edit_time);
        holder.mission_image= (ImageView) convertView.findViewById(R.id.mission_image);
        holder.cbm = (CheckBox) convertView.findViewById(R.id.checkbo);


        convertView.setTag(holder);
        holder.cbm.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v ) {
                CheckBox cb = (CheckBox) v ;
                final ObjectPeople person = arrayPeople.get(position);
              if(cb.isChecked()) {
                  arrayPeople.get(position).checkbox= true;

                  int mis_tm=0;
                  try{    mis_tm= Integer.parseInt( holder.edit_time.getText().toString());}
                  catch (Exception e){
                      cb.setChecked(false);
                      Toast.makeText(mContext, "يرجى إدخال قيمة معينة", Toast.LENGTH_SHORT).show();}
                  person.time= mis_tm;
                  if(mis_tm>0&&mis_tm<1200){
                      holder.mission_name.setText("اسم المهمة: "+person.name);
                      holder.mission_time.setText( "مدة المهمة: "+ person.time );
                      holder.edit_time.setVisibility(View.GONE);}
                  else{
                      cb.setChecked(false);
                      Toast.makeText(mContext, "يرجى اختيار رقم حقيقي", Toast.LENGTH_SHORT).show();
                  }

                }
                else {
                  arrayPeople.get(position).checkbox= false;
                  holder.edit_time.setVisibility(View.VISIBLE);

                }
            }
        });

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final ObjectPeople person = arrayPeople.get(position);
    holder.mission_name.setText("اسم المهمة: "+person.name);
    holder.mission_time.setText( "مدة المهمة: ");
    holder.mission_image.setImageResource(arrayPeople.get(position).image);






    holder.edit_time.setId(position);

        holder.edit_time.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                try {

                if (!hasFocus) {
                    final int position = v.getId();
                    final EditText Caption = (EditText) v;
                    arrayPeople.get(position).time = Integer.parseInt(Caption.getText().toString());

                }
                }catch (Exception e){}

            }

        });

    holder.cbm.setTag(arrayPeople);

    return convertView;

}

@Override

public int getCount() {

    return arrayPeople.size();

}

static class ViewHolder {
    TextView mission_name;
    TextView mission_time;
    EditText edit_time;
    ImageView mission_image;
    CheckBox cbm;

}

}

and this select.java which contains the arraylist and list view:

public class select extends AppCompatActivity {
    ArrayList<ObjectPeople> arrPeople;
    ListView lvPeople;
    AdapterListView adapter;
    ObjectPeople person;
    SharedPreferences settings;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select);
    settings = getApplicationContext().getSharedPreferences("shared", 0);
    lvPeople= (ListView) findViewById(R.id.lvPeopl);
    arrPeople=new ArrayList<>();

    person = new ObjectPeople("حفظ قرآن", 0,false, R.drawable.quran_hifz_r);
    arrPeople.add(person);

    person = new ObjectPeople("تلاوة قرآن", 0 ,false,R.drawable.img);
    arrPeople.add(person);

    person = new ObjectPeople("قراءة كتاب", 0 , false,R.drawable.books_r);
    arrPeople.add(person);

    person = new ObjectPeople("دورات تطوير مهارات", 0 , false,R.drawable.courses_r);
    arrPeople.add(person);


    person = new ObjectPeople("رياضة", 15 , false,R.drawable.sport_r);
    arrPeople.add(person);
    final String[] misson_name = new String[1];

    Button adf= (Button) findViewById(R.id.add_mission_out_btn);
    adf.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            View view = LayoutInflater.from(select.this).inflate(R.layout.add_mission_lyt,null);
            final EditText add_name = (EditText) view.findViewById(R.id.add_mission_name);
            AlertDialog.Builder builder = new AlertDialog.Builder(select.this);
            builder.setMessage("add your mission")

                    .setView(view)
                    .setPositiveButton("إضافة", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                             misson_name[0] = add_name.getText().toString();
                            arrPeople.add(new ObjectPeople(misson_name[0], 0, true,R.drawable.smile_small_r));

                        }
                    })
                    .setNegativeButton("إلغاء",null)
                    .setCancelable(false);
            AlertDialog alert =builder.create();
            alert.show();


        }
    });



    adapter=new AdapterListView(this,R.layout.item_listview,arrPeople);
    lvPeople.setAdapter(adapter);
    checkButtonClick();
}






private void checkButtonClick() {


    Button myButton = (Button) findViewById(R.id.btn_save);

    myButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            SharedPreferences settings = getApplicationContext().getSharedPreferences("shared", 0);
            int  hours2=settings.getInt("hours",0)*60;
            ArrayList<ObjectPeople> selected_missions = new ArrayList<>();
            int missoins_time_calc=0;
            for (int i = 0; i <arrPeople.size(); i++) {
                if(arrPeople.get(i).checkbox==true){
                    selected_missions.add(arrPeople.get(i));
                missoins_time_calc= missoins_time_calc + arrPeople.get(i).time;}
            }
            if (hours2 == missoins_time_calc){
                Gson gson = new Gson();
                String  jsonText = gson.toJson(selected_missions);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("selected_array", jsonText);
                editor.commit();

                Intent intent = new Intent(select.this, yourprogram.class);
                startActivity(intent);
                overridePendingTransition(R.anim.fade_in,R.anim.fade_out);

            }
            else if (hours2 > missoins_time_calc){
                Toast.makeText(select.this, "بقي"+(-missoins_time_calc+ hours2)+" دقيقة ", Toast.LENGTH_SHORT).show();}
            else if (hours2< missoins_time_calc){
                Toast.makeText(select.this,"يوجد "+ (-hours2+missoins_time_calc)+" دقيقة زيادة على الوقت المخصص", Toast.LENGTH_SHORT).show();
        }

           /* ArrayList<missions> missionsList = dataAdapter.missionsList;
            for(int i = 0; i< missionsList.size(); i++){
                missions missions = missionsList.get(i);
                if(missions.isSelected()){}}*/


        }
    });
}


}
1
Please add your source code.bnayagrawal
You may use RecyclerView instead of ListViewguisantogui
Your problem is somewhere in your adapter class, if you post the source code of your adapter we should be able to see what is going on :)Andre Breton
@AndreBreton I added my adapteraboahmad
@bnayagrawal i added my source codeaboahmad

1 Answers

1
votes

I think the error is the way your arrayPeople List is being populated, if you say the sixth item gets you the first you can try reversing the order with

Collections.reverse(arrayPeople);

you can place this line of code below your holder.cbm.setOnClickListener on your adapter

Hope it helps.