0

我有一个带有 textview 和 edittext 的自定义列表视图。当列表视图有 6 个或更少但超过 6 个项目的列表视图时没有问题,就像这个视频一样。我该如何解决这个问题?

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

}

4

3 回答 3

1

您需要实现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;
 }
于 2013-10-08T02:33:55.353 回答
1

嗨,这是我的解决方案手工列表视图

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>

爪哇代码:

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

}

于 2013-10-09T15:46:08.660 回答
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));
...

看看这个类似的问题。答案推荐完全相同的东西。

于 2013-10-08T02:51:27.563 回答