2016-03-18 140 views
0

我是新來的android。當我向列表視圖中的編輯文本插入值時,它會在滾動時重複。我到處搜索,但我找不到確切的解決方案。我的列表視圖非常大,因爲它從數據庫中提取值。 這裏是我的android代碼滾動時在列表視圖中編輯文本值重複

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.orders); 
    list = (ListView)findViewById(R.id.list1); 
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

    SelectItems(); 
    ViewItems();   
    cases = (EditText)findViewById(R.id.cases); 
     pcs = (EditText)findViewById(R.id.pcs); 
    clear1=(Button)findViewById(R.id.clear2); 
    cout1=(Button)findViewById(R.id.cout2); 

    clear1.setOnClickListener(this); 
    cout1.setOnClickListener(this); 

} 

private void SelectItems() { 
    // TODO Auto-generated method stub 
    try { 

      datas = new ArrayList<Map<String, String>>(); 
     DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext()); 
     newDB = dbHelper.getWritableDatabase(); 
     Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp " 
       + " from itemmaster", null); 

     Log.v("item detailss", c.toString()); 

      while (c.moveToNext()){ 

      HashMap<String, String> datanums = new HashMap<String, String>(); 
      String im_code = c.getString(c.getColumnIndex("im_code")); 

      String desc = c.getString(c.getColumnIndex("im_desc")); 

      datanums.put("code", im_code); 
      datanums.put("name", desc); 
         String price = c.getString(c.getColumnIndex("im_srp")); 
      datanums.put("ims", price); 

      datas.add(datanums); 

      } 
     } catch (SQLiteException se) { 
      Log.e(getClass().getSimpleName(), "Could not create or Open the database"); 
      } 

} 

private void ViewItems() { 
    // TODO Auto-generated method stub 
    list.setAdapter(null); 
    String[] fromwhere = { "code","name","ims" }; 
    int[] viewswhere = { R.id.code,R.id.name,R.id.srp}; 

      ADAhere = new SimpleAdapter(Orders.this, datas, 
      R.layout.order_simple_row, fromwhere, viewswhere); 
    list.setAdapter(ADAhere); 
    } 



@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
     switch (v.getId()) { 

     case R.id.clear2: 
      Intent i= new Intent(getApplicationContext(),Orders.class); 
      startActivity(i); 
      break; 

     case R.id.cout2: 
       Checkout(v); 

       break; 

     default: 
      break; 
     } 

} 
private void Checkout(View view) { 
// TODO Auto-generated method stub 
    AlertDialog.Builder alertDialou = new AlertDialog.Builder(Orders.this); 
    alertDialou.setMessage("DO YOU WANT TO CHECKOUT ?"); 


    alertDialou.setPositiveButton("YES", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int id) { 
      getcases(); 
      invoiceAlert(); 


     } 


     private void invoiceAlert() { 
      // TODO Auto-generated method stub 
      AlertDialog.Builder alertDialo = new AlertDialog.Builder(Orders.this); 
      alertDialo.setMessage("ORDER SUCCESS.."); 
      alertDialo.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        //Orderheader(); 
        printAlert(); 
       } 



       private void printAlert() { 
        // TODO Auto-generated method stub 
        AlertDialog.Builder alertDial = new AlertDialog.Builder(Orders.this); 
        alertDial.setMessage("DO YOU WANT TO PRINT NOW ?"); 
        alertDial.setPositiveButton("YES", new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int id) { 
          // TODO Auto-generated method stub 
          Intent in = new Intent(Orders.this,Menu_iem.class); 

          startActivity(in); 
         } 
        }); 
        alertDial.setNegativeButton("LATER", new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int id) { 
          // TODO Auto-generated method stub 

         dialog.cancel(); 
         } 
        }); 
        AlertDialog alertDialog = alertDial.create(); 

         // show alert 

         alertDialog.show(); 
       } 
      }); 
      AlertDialog alertDialog = alertDialo.create(); 

       // show alert 

       alertDialog.show(); 
     } 

    }); 

alertDialou.setNegativeButton("No", new DialogInterface.OnClickListener() { 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
    // TODO Auto-generated method stub 
    Intent in= new Intent(Orders.this,Orders.class); 
    startActivity(in); 
    } 
});   
AlertDialog alertDialog = alertDialou.create(); 

// show alert 

alertDialog.show();  
} 

    protected void getcases() { 
// TODO Auto-generated method stub 
DatabaseHelper databasecontroller = new DatabaseHelper(Orders.this); 
List<Map<String, String>> data = null; 
data = new ArrayList<Map<String, String>>(); 
float srps = (float) 0.0; 
int qtys = 0; 
databas.put("imuom", "CASES"); 


if(list != null){ 

     for(int i = 0; i< list.getChildCount();i++){ 

      View vie = list.getChildAt(i); 

      EditText ed1= (EditText) vie.findViewById(R.id.cases); 
       String cases1 = ed1.getText().toString(); 

       databas.put("A",ed1.getText().toString()); 

     EditText ed2= (EditText) vie.findViewById(R.id.piece); 
       String cases2 = ed2.getText().toString(); 

       databas.put("B",ed1.getText().toString());  
data.add(databas); 
databasecontroller.inserttable(databas); 
    } 
    } 

請幫我這個。

+0

在EDITTEXT你在哪裏顯示的數據顯示某些代碼 –

+0

列表視圖是女傭與三個文本視圖和2個編輯文本。使用上面的代碼,我只是將數據添加到文本視圖。並在編輯文本中,我想輸入並保存數據到數據庫上按鈕單擊 – ellie

+0

@ellie,當你說editText值得到重複時,你是否想說say值出現在列表視圖中的另一個項目的不同editText? –

回答

1

嘗試設置標籤適配器類的你getview方法,併發布你的適配器類的代碼,所以我們可以幫助更好。

if (convertView == null) { 
      convertView = inflater.inflate(R.layout.simple_store_item, parent, false); 
      mViewHolder = new CustomViewHolder(convertView); 
      convertView.setTag(mViewHolder); 
     } else { 
      mViewHolder = (CustomViewHolder) convertView.getTag(); 
     } 
+0

如果您想自定義UI,您將不得不製作自己的自定義適配器,而不是使用SimpleAdapter。這將是一個更好的方法。 – Nidhi

0

這兩種方法添加到您的列表視圖適配器類

@Override 
     public int getItemViewType(int position) { 
      return position; 
     } 

     @Override 
     public int getViewTypeCount() { 
      return getCount(); 
     } 
0

列表視圖中的Android重用的項目,因此,如果用戶滾動查看,一些人認爲重用舊觀點,它應該顯示重複的值,如果你不小心管理。 如果您的行視圖有很多項目,您應該檢查條件以正確顯示視圖。例如:

if(true){ 
    edt.setText("abc"); 
}else{ 
    edt.setText("cde"); 
} 

如果在每種條件下顯示的數據,則必須設置值或復位的情況下,該視圖UNUSE

相關問題