本周学习了android的fragment
package net.hnjdzy.tinyaccount.fragment;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import net.hnjdzy.tinyaccount.R;
import net.hnjdzy.tinyaccount.activity.AccountEditActivity;
import net.hnjdzy.tinyaccount.adapter.AccountItemAdapter;
import net.hnjdzy.tinyaccount.db.AccountDao;
import net.hnjdzy.tinyaccount.entity.AccountItem;
import android.widget.AdapterView.OnItemLongClickListener;
import java.util.ArrayList;
import java.util.List;
/**
* 收入列表
* @author androiddev@163.com,hnjdzy
*/
public class IncomeFragment extends Fragment {
View mRootView;
public IncomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mRootView = inflater.inflate(R.layout.fragment_income, container, false);
initView();
return mRootView;
}
// 初始化
private void initView() {
Button buttonAdd = (Button)mRootView.findViewById(R.id.buttonAdd);
buttonAdd.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
buttonAddOnClick();
}
});
ListView listView = (ListView)mRootView.findViewById(R.id.listView1);
listView.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
deleteItem(id);
return true;
}
});
refreshData();
}
protected void buttonAddOnClick() {
Intent intent =new Intent(this.getActivity(), AccountEditActivity.class);
intent.putExtra("isIncome", true);
this.startActivityForResult(intent, 1);
//startActivity(intent);
}
//刷新界面
private void refreshData() {
AccountDao dbManager = new AccountDao(getContext());
// List<AccountItem> incomeAccountList = getTestData();
List<AccountItem> incomeAccountList = dbManager.getIncomeList();
AccountItemAdapter adapter = new AccountItemAdapter(incomeAccountList,getActivity());
ListView listView = (ListView) mRootView.findViewById(R.id.listView1);
listView.setAdapter(adapter);
TextView textViewIncomeSummary = (TextView) mRootView.findViewById(R.id.textViewIncomeSummary);
textViewIncomeSummary.setText("10000");
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("tinyaccount","onActivityResult");
refreshData();
}
protected void deleteItem(final long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setTitle(R.string.delete_confirm_title);
builder.setMessage(R.string.delete_confirm_msg);
builder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AccountDao dbManager = new AccountDao(getContext());
dbManager.deleteIncome(id);
refreshData();
}
});
builder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
private List<AccountItem> getTestData() {
List<AccountItem> result = new ArrayList<>();
for(int i=0;i<5;i++) {
AccountItem item = new AccountItem();
item.setId(i);
item.setCategory("兼职收入");
item.setMoney(100*i);
item.setDate("2019-01-0"+i);
result.add(item);
}
return result;
}
}