android中的自定义事件

时间:2016-02-01 08:10:27

标签: android listview

我有一个有效的Android应用程序。在这个应用程序中,我有一个活动,它在ListView中显示来自SqlServer的事务,我使用ListViewAdapter(扩展BaseAdapter)和net.sourceforge.jtds.jdbc.Driver来显示这些事务,现在我已经为每一行添加了一个Button ListView和我想在用户点击按钮时从服务器删除事务。我不知道从哪里开始。

TransactionActivity.java

public class TransactionActivity extends AppCompatActivity implements   OnClickListener {

private EditText tranDate;
private DatePickerDialog dtpDialog;
private SimpleDateFormat dateFormat;
private ListView lvTransactions;
private Button btnAddNewTransaction;
private TransactionsListViewAdapter adapter;
private ArrayList<HashMap<String, String>> transactionsList;
private Date selectedDate;
private SqlDatabaseHelper sqldb = new SqlDatabaseHelper(this);
private DecimalFormat df = new DecimalFormat("#.00");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_transaction);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);

    tranDate = (EditText) findViewById(R.id.tranDate);
    tranDate.setInputType(InputType.TYPE_NULL);
    tranDate.requestFocus();

    setDateTimeField();

    lvTransactions = (ListView) findViewById(R.id.lvTransactions);
    transactionsList = new ArrayList<HashMap<String, String>>();

    tranDate.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            refreshTransactions();
        }
    });
}

private void refreshTransactions() {
    transactionsList.clear();
    HashMap<String, String> headerRow = new HashMap<String, String>();
    headerRow.put(FIRST_COLUMN, "Party");
    headerRow.put(SECOND_COLUMN, "Type");
    headerRow.put(THIRD_COLUMN, "Amount");
    headerRow.put(FOURTH_COLUMN, "Status");
    transactionsList.add(headerRow);
    List<Transaction> tranList = sqldb.getTransactionsByDate(selectedDate);
    for (Transaction t : tranList) {
        HashMap<String, String> tranRow = new HashMap<String, String>();
        tranRow.put(FIRST_COLUMN, t.get_party().getName());
        String tTypeStr;
        int tType = t.get_transactionType();
        switch (tType) {
            case 0:
                tTypeStr = "आया";
                break;
            case 1:
                tTypeStr = "दिया";
                break;
            case 2:
                tTypeStr = "जमा";
                break;
            case 3:
                tTypeStr = "नामे";
                break;
            default:
                tTypeStr = "";
        }
        int tStat = t.get_transactionStatus();
        String tStatStr = "";
        switch (tStat) {
            case 0:
                tStatStr = "Fresh";
                break;
            case 1:
                tStatStr = "Synced";
                break;
            case 2:
                tStatStr = "Deleted";
                break;
        }
        tranRow.put(SECOND_COLUMN, tTypeStr);
        tranRow.put(THIRD_COLUMN, df.format(t.get_amount()));
        tranRow.put(FOURTH_COLUMN, tStatStr);
        transactionsList.add(tranRow);
    }
    adapter = new TransactionsListViewAdapter(this, transactionsList);
    lvTransactions.setAdapter(adapter);
    lvTransactions.invalidateViews();
}

private void setDateTimeField() {
    tranDate.setOnClickListener(this);

    Calendar calendar = Calendar.getInstance();
    dtpDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            selectedDate = newDate.getTime();
            tranDate.setText(dateFormat.format(newDate.getTime()));
        }
    }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
}


@Override
public void onClick(View view) {
    if (view == tranDate) {
        dtpDialog.show();
    }
}

}

TransactionListViewAdapter.java

public class TransactionsListViewAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
Button btnDeleteTran;


public TransactionsListViewAdapter(Activity activity, ArrayList<HashMap<String, String>> list) {
    super();
    this.activity = activity;
    this.list = list;
}

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

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub


    LayoutInflater inflater = activity.getLayoutInflater();
    View view = null;
    convertView = null;
    if (convertView == null) {

        convertView = inflater.inflate(R.layout.transaction_list_layout, null);

        txtFirst = (TextView) convertView.findViewById(R.id.txtTranParty);
        txtSecond = (TextView) convertView.findViewById(R.id.txtTranType);
        txtThird = (TextView) convertView.findViewById(R.id.txtTranAmount);
        txtFourth = (TextView) convertView.findViewById(R.id.txtTranStatus);
        btnDeleteTran = (Button) convertView.findViewById(R.id.btnTranDelete);


        convertView.setTag(R.id.txtTranParty, txtFirst);
        convertView.setTag(R.id.txtTranType, txtSecond);
        convertView.setTag(R.id.txtTranAmount, txtThird);
        convertView.setTag(R.id.txtTranStatus, txtFourth);
        convertView.setTag(R.id.btnTranDelete, btnDeleteTran);

    }/* else {
        txtFirst = (TextView) convertView.getTag(R.id.Party);
        txtSecond = (TextView) convertView.getTag(R.id.Opponent1);
        txtThird = (TextView) convertView.getTag(R.id.Opponent2);
        txtFourth = (TextView) convertView.getTag(R.id.Draw);
    }*/

    HashMap<String, String> map = list.get(position);
    txtFirst.setText(map.get(FIRST_COLUMN));
    txtSecond.setText(map.get(SECOND_COLUMN));
    txtThird.setText(map.get(THIRD_COLUMN));
    txtFourth.setText(map.get(FOURTH_COLUMN));

    if (txtFirst.getText().equals("Party")) {
        btnDeleteTran.setVisibility(View.INVISIBLE);
    }

    btnDeleteTran.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    return convertView;
}

}

1 个答案:

答案 0 :(得分:0)

在你的适配器中,只需为按钮添加一个onclick监听器,并放置代码以删除监听器内的条目。它可能看起来像:

yourButton.setOnClickListener(new OnClickListener(){
      public void onClick(View v){
         //deletion code here
      }
)};

其中yourButton是要点击的按钮。 您应该在适配器的getView方法中添加此代码