我正在尝试删除在dinammically创建的表行,但是我无法获得正确的表行id,我的所有表行都具有相同的id,更多我创建它们,我的计数器设置更高的id并且所有行都得到它...我会感激任何帮助,我坚持了几天。我的方法很大,但我认为只有开始很重要。
public void getRow(String klik, int brojkica)
{
//db=openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
db = INF.getReadableDatabase();
int count = 0;
Cursor cursor3 = db.rawQuery("select id,Artikl, CIJENA from Artikli where");
System.out.println(counterko);
counterko++;
tr = new TableRow(this);
tr.setClickable(true);
tr.setBackgroundColor(Color.GRAY);
tr.setId(500 + counterko);
cursor3.moveToFirst();
final TextView sifracolumn = new TextView(this);
sifracolumn.setId(1000 + counterko);
sifracolumn.setPadding(5, 5, 5, 5);
sifracolumn.setText(String.valueOf(cursor3.getInt(0)));
sifracolumn.setGravity(Gravity.CENTER | Gravity.BOTTOM);
sifracolumn.setTextColor(Color.BLACK);
tr.addView(sifracolumn);
allIDs.add(sifracolumn);//////////////////////////////////////
final TextView artiklcolumn = new TextView(this);
artiklcolumn.setPadding(5, 5, 5, 5);
artiklcolumn.setId(2000 + counterko);
artiklcolumn.setClickable(true);
artiklcolumn.setWidth(400);
artiklcolumn.setText(cursor3.getString(1));
artiklcolumn.setGravity(Gravity.CENTER | Gravity.BOTTOM);
artiklcolumn.setTextColor(Color.BLACK);
tr.addView(artiklcolumn);
artiklcolumn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
//alert(tr.getId());
alert(artiklcolumn.getId());
}
});
final TextView cijenacolumn = new TextView(this);
cijenacolumn.setId(3000 + count);
cijenacolumn.setText(cursor3.getString(2));
cijenacolumn.setWidth(120);
cijenacolumn.setGravity(Gravity.CENTER | Gravity.BOTTOM);
cijenacolumn.setTextColor(Color.BLACK);
tr.addView(cijenacolumn);
final EditText kolicinacolumn = new EditText(this);
kolicinacolumn.setId(4000 + counterko);
kolicinacolumn.setText("1");
kolicinacolumn.setGravity(Gravity.CENTER | Gravity.BOTTOM);
kolicinacolumn.setTextColor(Color.BLACK);
allEds.add(kolicinacolumn);
tr.addView(kolicinacolumn);
final TextView iznoscolumn = new TextView(this);
iznoscolumn.setId(5000 + counterko);
iznoscolumn.setWidth(120);
iznoscolumn.setText("");
iznoscolumn.setTextColor(Color.BLACK);
iznoscolumn.setGravity(Gravity.CENTER | Gravity.BOTTOM);
tr.addView(iznoscolumn);
allTexts.add(iznoscolumn);
if (kolicinacolumn.getText().toString() == "1")
{
System.out.println("kme");
iznoscolumn.setText(cijenacolumn.getText().toString());
}
zaplatiti();
tl = (TableLayout) findViewById(R.id.tableLayout1);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
count++;
kolicinacolumn.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count)
{
String kol = kolicinacolumn.getText().toString();
if (TextUtils.isEmpty(kol))
{
try
{
System.out.print("Stop it");
} catch (NumberFormatException e)
{
System.out.println("This is not a number");
System.out.println(e.getMessage());
}
} else
{
Float racunam = Float.valueOf(kol);
Float racunam2 = Float.valueOf(cijenacolumn.getText().toString());
Float reza = racunam * racunam2;
iznoscolumn.setText(String.valueOf(reza));
}
zaplatiti();
}
public void afterTextChanged(Editable s)
{
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
//TODO
}
});
}
答案 0 :(得分:0)
使用.removeView()
方法,并使用您要删除的表格行的索引或向表格行添加唯一标记,稍后根据此标记将其删除。
答案 1 :(得分:0)
在每一行中也将id设置为标记。删除唯一行时,请使用.removeView(view.getTag());
答案 2 :(得分:0)
for(int i = 0, j < table.getChildCount(); i < j; i++){
TableRow row = (TableRow) getChildAt(i);
if(you can check something here) {
removeViewAt(i);
// or...
removeView(row);
}
}
答案 3 :(得分:0)
tr = new TableRow(this);
我无法将id分配给我的表行,因为id没有在我的方法中实例化它,所以首先要做的是
final TableRow tr = new TableRow(this);
然后我把onclicklistener放在我想要响应的列上并将其放入..
artiklcolumn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
TableRow km= (TableRow)findViewById(tr.getId());
alert(km);
}
我获取我的对象,将其发送到警告对话框,然后从那里删除它。
public void delete(TableRow tr2){
tl.removeView(tr2);
}