0

I've tried many ways to prevent repeating items from appearing in each row, but can't figure it out.

I have 2 XML layouts. The first has 4 EditTexts and 1 TextView inside a row. The second one has a Button, which will add more items to the first when clicked.

Now There are 2 main problems:

Firstly, when I press the Button four times, and fill in the EditText fields, the ListView doesn't move to the last row I added. It just hangs in the 5th row, forcing me to touch the ListView and scroll down.

Secondly, when I scroll the ListView, the 5 first rows repeat in the rest of the rows, remove the old information, and replaces it with the wrong information.

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:shrinkColumns="*"
android:stretchColumns="*"
>

<TableRow
    android:id="@+id/tv_Row"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="1dp"
    android:background="@drawable/row_shap"
    android:padding="1dip"
    android:weightSum="5">

    <EditText
        android:id="@+id/nameTool"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1.8"
        android:gravity="center"
        android:maxLength="14"
        android:hint="Item's Name"
        android:textColor="@color/black"
        android:textSize="9sp"
        android:textStyle="bold"
        android:typeface="serif"
        android:imeOptions="actionNext">

        <requestFocus />
    </EditText>


    <EditText
        android:id="@+id/qntTool"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.80"
        android:gravity="center"
        android:hint="Qty"
        android:maxLength="2"
        android:numeric="integer"
        android:textColor="@color/black"
        android:textSize="9sp"
        android:textStyle="bold"
        android:typeface="serif"
        android:nextFocusRight="@+id/nameTool"
        android:imeOptions="actionNext"/>

    <EditText
        android:id="@+id/xTool"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.80"
        android:gravity="center"
        android:hint="X"
        android:maxLength="4"
        android:numeric="integer"
        android:textColor="@color/black"
        android:textSize="9sp"
        android:textStyle="bold"
        android:typeface="serif"
        android:nextFocusRight="@+id/qntTool"
        android:imeOptions="actionNext"/>


    <EditText
        android:id="@+id/yTool"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.80"
        android:gravity="center"
        android:hint="Y"
        android:maxLength="2"
        android:numeric="integer"
        android:textColor="@color/black"
        android:textSize="9sp"
        android:textStyle="bold"
        android:typeface="serif"
        android:nextFocusRight="@+id/xTool"
        android:imeOptions="actionNext"/>

    <TextView
        android:id="@+id/rsltTool"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.80"
        android:gravity="center"
        android:hint="result"
        android:maxLength="7"
        android:textColor="@color/black"
        android:textSize="9sp"
        android:textStyle="bold"
        android:typeface="serif"/>
   </TableRow>
</RelativeLayout>

Here's An Activity

public class CalculatDetails extends Activity {

    ListView listView;
    RowAdapter rowAdapterObj;
    ArrayList<MyRow> objsArrayList

    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.list_appliance_layout1);

      listView = (ListView) findViewById(R.id.lst);
      initRows();
  }

public void initRows() {

    objsArrayList = new ArrayList<>();
    objsArrayList.add(new MyRow());
    rowAdapterObj = new RowAdapter(getBaseContext(),android.R.layout.simple_list_item_1, objsArrayList);
    listView.setAdapter(rowAdapterObj);
  }

public void btnAdd(View view) {
    objsArrayList.add(new MyRow());
    rowAdapterObj.notifyDataSetChanged();
  }

}



class RowAdapter extends ArrayAdapter<MyRow> {

  Context context;
  MyRow rowPosition;
  private ArrayList<MyRow> objects;

public RowAdapter(Context context, int textViewResourceId, ArrayList<MyRow> objects) {
    super(context, textViewResourceId, objects);
    this.objects = objects; //objects;
    this.context = context;
  }

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return this.objects.size();
  }

@Override
public MyRow getItem(int position) {
    // TODO Auto-generated method stub
     return this.objects.get(position);

  }

@SuppressLint("NewApi")
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = new ViewHolder();

    rowPosition = this.objects.get(position);


    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        convertView = mInflater.inflate(R.layout.details_list_row, parent, false);

        holder.edtxtNameItem = (EditText) convertView.findViewById(R.id.nameTool);
        holder.edtxtQuantityItem = (EditText) convertView.findViewById(R.id.qntTool);
        holder.edtxtXItem = (EditText) convertView.findViewById(R.id.xTool);
        holder.edtxtYItem = (EditText) convertView.findViewById(R.id.yTool);
        holder.txtvResult = (TextView) convertView.findViewById(R.id.rsltTool);


        convertView.setTag(holder);

     } else {

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

    rowPosition.setNameItem(holder.edtxtNameItem.getText().toString());
    rowPosition.setQntItem(holder.edtxtQuantityItem.getText().toString());
    rowPosition.setXItem(holder.edtxtXItem.getText().toString());
    rowPosition.setYItem(holder.edtxtYItem.getText().toString());
    try {
        holder.edtxtNameItem.setText(rowPosition.getNameItem());
        holder.edtxtQuantityItem.setText(rowPosition.getQntItem());
        holder.edtxtXItem.setText(rowPosition.getXItem());
        holder.edtxtYItem.setText(rowPosition.getYItem());
      } catch (Exception e) {
          e.printStackTrace();
        }
  return convertView;
 }


class ViewHolder {
    EditText edtxtNameItem;
    EditText edtxtQuantityItem;
    EditText edtxtXItem;
    EditText edtxtYItem;
    TextView txtvResult;
 }
}
Drenmi
  • 8,492
  • 4
  • 42
  • 51
MNN
  • 3
  • 1
  • 1
  • 4

1 Answers1

0

Hi your second problem comes from here:

...
rowPosition.setNameItem(holder.edtxtNameItem.getText().toString());

...
try{
    holder.edtxtNameItem.setText(rowPosition.getNameItem());
}...

The problem here is you are overriding what ever is in rowPosition with what was showing in your views when getView() is called. You should delete rowPosition.set... rowPosition should only be updated when you create your objects. So remove all lines where you use setters of rowPosition in the getView() method.

You should probably have a new Constructor for your rows or just add your setters in this method:

public void btnAdd(View view) {
    objsArrayList.add(new MyRow());
    // Set your row params here
    rowAdapterObj.notifyDataSetChanged();
}

Finally it is a bad idea to have EditText inside your rows. You should probably use TextViews and move your EditTexts elsewhere.