android edittext自定义列表视图

时间:2013-10-08 02:15:27

标签: android android-listview

我有一个带有textview和edittext的自定义列表视图。当listview有6项或更少但超过6项列表视图时,没有问题,例如video。我怎样才能解决这个问题?

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);
}

我的自定义适配器是

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;
        }

}

3 个答案:

答案 0 :(得分:1)

这是我的解决方案handmade listview

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: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代码:

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;
    }
}

}

答案 1 :(得分:0)

您需要实施 ViewHolder 模式。

有关详细信息,请参阅此博客文章: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;
 }

答案 2 :(得分:0)

问题是,当调用getView()时,您需要设置EditText的值,就像设置TextView

的值一样

因此,每次用户键入内容时,都会将值放在相应的Siparis对象中。在getView()时,您从对象中选择值并在视图中进行设置。

这就是你在使用TextView(Item1,Item2 ......等)时所做的事情。它们永远不会被错误地显示出来。但是使用编辑文本,由于值更改,您必须更新相应miktar对象的Siparis变量。并将其绑定在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));
...

看看这个类似的question。答案建议完全相同。