将onclick侦听器和对话框侦听器设置为表布局

时间:2015-07-16 13:01:48

标签: android

我想在tablelayout中为动态创建的表行添加一个onClick Listener。并点击一个警告对话框,其中有两个项目'编辑','删除',当我点击编辑时,它必须采取行中的所有项目,即日期,描述,金额,并启动另一个具有Edittexts的活动,该活动应包含这三个项目他们分别。当点击删除时,该行应该从那里删除..... 提前致谢。 请考虑评论中的代码.. 我不知道怎么做,所以我尝试干杯知道这些物品是否被重新检验,但无论在哪一行我点击只有最后一行被重新检索

                                                                                                              MyDBHandler dbHandler = new MyDBHandler(this);
              TableLayout tableLayout=(TableLayout)findViewById(R.id.table);
              SQLiteDatabase db = dbHandler.getReadableDatabase();
                    // Start the transaction.
                   db.beginTransaction();
    String selectQuery = "SELECT date,description,amount FROM expdetails E `enter code here`WHERE E.date == date('now')";
         Cursor cursor = db.rawQuery(selectQuery, null);

        //lpw.setAdapter(adapter);


    if(cursor.getCount() >0)
    {
        while (cursor.moveToNext()) {
            // Read columns data
            _date = cursor.getString(cursor.getColumnIndex("date"));
            _desc = cursor.getString(cursor.getColumnIndex("description"));
            _amt = cursor.getString(cursor.getColumnIndex("amount"));

            // dara rows
            row = new TableRow(this);
            row.setLayoutParams(new   `enter code here` `enter code here`TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.MATCH_PARENT));

            String[] colText = {_date , _desc, _amt+""};
            //colText[0]=_date;
            //colText[1]=_desc;
            //colText[2]=_amt;
            for (String text : colText) {

                TextView tv = new TextView(this);
                tv.setLayoutParams(new `enter code here`TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                        TableRow.LayoutParams.WRAP_CONTENT));
                tv.setGravity(Gravity.CENTER);
                tv.setTextSize(18);
                tv.setPadding(30, 10, 30, 10);
                tv.setText(text);
                row.setGravity(Gravity.CENTER);
                row.addView(tv);


               // row.setOnClickListener(rowOnClick);
                //row.getChildAt(0)
               /* row.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TextView tv1 =(TextView) row.getChildAt(0);
                        TextView tv2 = (TextView)row.getChildAt(1);
                         `enter code    here`Toast.makeText(getApplicationContext(),tv2.getText().toString(), `enter code here`Toast.LENGTH_LONG).show();
                        //showDialog(DIALOG_ID);


                    }
                });*/
            }
            tableLayout.addView(row);

    }

                }
   cursor.close();

    db.endTransaction();
}
 /*private View.OnClickListener rowOnClick = new       `enter code here`View.OnClickListener()   {
    public void onClick(View v) {
        //Highlight selected row
        `enter code here`       `enter code here`   `enter code here`//v.setBackgroundColor(getResources().getColor(android.R.color.holo_red_lig`enter code here`ht));
        final int k = v.getId();
        Toast.makeText(ExpenseDetails.this, colText[k],
                Toast.LENGTH_SHORT).show();
    }
};*/
@Override
public Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_ID:

        ArrayList<String> ops = new ArrayList<>();
        //lpw=new ListPopupWindow(this);
        ArrayAdapter adapter = new ArrayAdapter<String>(this, `enter code  here`        android.R.layout.simple_spinner_dropdown_item, ops);
        ops.add("Edit");
        ops.add("Delete");
        adapter.notifyDataSetChanged();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);


        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // The 'which' argument contains the index position
                // of the selected item
                TextView tv1 =(TextView) row.getChildAt(0);
                TextView tv2 = (TextView)row.getChildAt(1);

                switch (which) {
                    case 0:

                        Toast.makeText(getApplicationContext(), `enter code         here`tv1.getText().toString(), Toast.LENGTH_LONG).show();
                        dialog.dismiss();
                        break;
                    case 1:
                        Toast.makeText(getApplicationContext(), `enter code       here`tv2.getText().toString(), Toast.LENGTH_LONG).show();
                        dialog.dismiss();
                        break;

                }
            }
        });
        return builder.create();
    }
    return null;
}

2 个答案:

答案 0 :(得分:0)

  1. 在表格行

  2. 中关联点击监听器
  3. 传递包中的表行id并使用相应的editText视图启动新活动,获取用户输入并使用新值通知表的row元素

答案 1 :(得分:0)

While you create a table Row dynamically associate an Id with it and in onclick listener distinguish the row pressed with it's id

eg 
TableLayout tl=new TableLayout(context);
for(int i=0;i<20;i++){
TableRow tr = new TableRow(context);
tr.setId(i);
tr.setOnClickListener(this);
}

public void onClick(View view)
{

switch(view.getId())
{
case 1: 
doSomething();
break;
case 2: 
doSomething();
}

}

public void doSomething()
{

}


Similarly do it for a dialog