0

I have a custom listview with textview and edittext.there is no problem when listview has 6 item or less but more than 6 item listview like this video .How can I solve this problem?

public class Form2 extends ListActivity {

CustomListAdapter cla;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_form2);

    cla = new CustomListAdapter(this);
    setListAdapter(cla);
}

my custom adapter is

public class CustomListAdapter  extends BaseAdapter{

private LayoutInflater mInflater;
private ArrayList<Siparis> notlar;

public CustomListAdapter(Context context) {

            mInflater = LayoutInflater.from(context);
            notlar = new ArrayList<Siparis>();


            DBHandler db = new DBHandler(context);
            db.getWritableDatabase();
            notlar=db.getSiparis();
            db.close();

        }

@Override
public int getCount() {
    return notlar.size();
}

@Override
public Siparis getItem(int position) {
    return notlar.get(position);
}

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

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

            ViewHolder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.activity_kap, null);

                holder = new ViewHolder();
                holder.mKonu = (TextView) convertView.findViewById(R.id.malzeme);
                holder.mTarih2 = (EditText) convertView.findViewById(R.id.miktar);
            } 
            else {
                holder = (ViewHolder) convertView.getTag();
            }

            //holder.mTarih2.setText(String.valueOf(notlar.get(position).miktar));
            holder.mKonu.setText(notlar.get(position).malzeme_adi);

            convertView.setTag(holder);

            return convertView;
    }


        public class ViewHolder {
            TextView mKonu;
            EditText mTarih2;
        }

}

essp
  • 13
  • 1
  • 5
  • This is because android list view recycles views. Can you post your code so we can help you with a solution.. by then maybe you could read this http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works – Amulya Khare Oct 08 '13 at 02:18
  • Is there a reason why you have commented out line `//holder.mTarih2.setText(String.valueOf(notlar.get(position).miktar));` I think it should work if you keep it.. – Amulya Khare Oct 08 '13 at 02:40
  • unfortunately,it is same – essp Oct 08 '13 at 02:47
  • @essp see my updated post/comment – d.moncada Oct 08 '13 at 03:08

3 Answers3

1

You need to implement the ViewHolder pattern.

See this blog post for more details: http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html

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

    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.activity_kap, null);

        holder = new ViewHolder();
        holder.mKonu = (TextView) convertView.findViewById(R.id.malzeme);
        holder.mTarih2 = (EditText) convertView.findViewById(R.id.miktar);

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

    holder.mTarih2.setText(String.valueOf(notlar.get(position).miktar));
    holder.mKonu.setText(notlar.get(position).malzeme_adi);

    return convertView;
}

public static class ViewHolder {
     TextView mKonu;
     EditText mTarih2;
 }
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • It has nothing much to do with ViewHolder pattern. The reason is he is not setting the amount value at the time the getView() happens – Amulya Khare Oct 08 '13 at 02:45
1

hi this is my solution handmade listview

xml file:

  <?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:layout_weight="1"
    android:orientation="vertical" >

<ScrollView 
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="fill_parent">

      <LinearLayout 
        android:id="@+id/mylinear"
         android:layout_width="match_parent"
         android:layout_weight="1"
        android:layout_height="fill_parent"
        android:orientation="vertical"

        >

    </LinearLayout>

 </ScrollView>


     <LinearLayout
         android:id="@+id/group4ff"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" >

         <Button
             android:id="@+id/vazgecff"
             android:layout_width="0dp"
             android:layout_weight=".30"
             android:layout_height="wrap_content"
             android:onClick="cagir"
             android:text="vazgec" />

         <Button
             android:id="@+id/kaydetff"
             android:layout_width="0dp"
             android:layout_weight=".70"
             android:layout_height="wrap_content"
             android:onClick="cagir"
             android:text="kaydet" />
     </LinearLayout>

</LinearLayout>

java code:

public class Form3 extends Activity {

LinearLayout[] llx ;
TextView[] tx ;
EditText[] ex ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_form3);

    DBHandler db = new DBHandler(this);
    ArrayList<Siparis> listem = db.getSiparis();
    db.close();

    LinearLayout ll = (LinearLayout) findViewById(R.id.mylinear);
    llx = new LinearLayout[listem.size()];
    tx = new TextView[listem.size()];
    ex = new EditText[listem.size()];

    for (int i = 0; i < listem.size(); i++) {
        llx[i] = new LinearLayout(this);
        tx[i] = new TextView(this);
        ex[i] =new EditText(this);
        tx[i].setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT,0.8f));
        ex[i].setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT,0.2f));
        tx[i].setText(listem.get(i).getMalzeme_adi());
        ex[i].setInputType(InputType.TYPE_CLASS_NUMBER);
        llx[i].setId(i);
        llx[i].setClickable(true);
        final int j = i;
        llx[i].setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                msg(tx[j].getText().toString());
            }
        });

        llx[i].addView(tx[i]);
        llx[i].addView(ex[i]);

        ll.addView(llx[i]);
    }

}


private void msg(String x){
    Toast.makeText(this, x, Toast.LENGTH_LONG).show();
}

public void cagir(View view){
    switch (view.getId()) {
    case R.id.kaydetff:
        for (int i = 0; i < ex.length; i++) {
            if(!ex[i].getText().toString().equals("")){
                Log.e(tx[i].getText().toString(),ex[i].getText().toString());
            }
        }
        break;

    default:
        break;
    }
}

}

essp
  • 13
  • 1
  • 5
0

So the problem is when getView() is called you need to set the value of the EditText just like you set the value of the TextView

So each time the user types something, you put the value in the corresponding Siparis object. At the time of getView() you pick out the value from the object and set it in the view.

This is what you are doing with your TextView (Item1, Item2.. etc) They are never displayed wrongly. But with the edit text, since the value changes, you must update the miktar variable of the corresponding Siparis object. And bind it on getView()

...
...    
    holder.mKonu.setText(notlar.get(position).malzeme_adi);    
    //every time the user changes the edit text, save 
        //the current value of the edit text to retrieve later
        holder.mTarih2.addTextChangedListener(new TextWatcher(){
            @Override
            public void afterTextChanged(Editable editable) {
                notlar.get(position).miktar = Integer.parseInt(editable.toString());
            }
            ....
        };
    holder.mTarih2.setText(String.valueOf(notlar.get(position).miktar));
...

Take a look at this similar question. The answer recommends exactly the same thing.

Community
  • 1
  • 1
Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
  • i change my ViewHolder to static and i set value edittext . but it is same http://www.youtube.com/watch?v=NnhWkvdjSYo&feature=youtu.be – essp Oct 08 '13 at 03:04
  • Now everything appears to be 0. So this means means you are not saving the value to the object. – Amulya Khare Oct 08 '13 at 03:09