小さな Financecontrol-App を作成しています。今、私は自分の電話にぶつからないと対処できないと思う問題に直面しています = D
Android-Template から作成された ViewPager を使用して Activity を取得しました。私は3つのタブを持っています。2 番目と 3 番目のタブは同じ Fragment-Class を使用します。タブのスニペットは次のとおりです。
アカウント アクティビティで:
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (position) {
case POSITION_OVERVIEW:
fragment = new OverviewSectionFragment();
break;
case POSITION_INCOME:
args.putString("flag", TransactionSectionFragment.INCOME);
fragment = new TransactionSectionFragment();
fragment.setArguments(args);
break;
case POSITION_OUTGO:
args.putString("flag", TransactionSectionFragment.OUTGO);
fragment = new TransactionSectionFragment();
fragment.setArguments(args);
break;
}
return fragment;
}
フラグメント自体は次のようになります。
public class TransactionSectionFragment extends Fragment {
OnDataChangedListener mCallback;
public interface OnDataChangedListener {
public void onDataChanged(int rnd);
}
// Private variables for IncomeSectionFragment
private FinanceDataSource datasource;
private ListView transactionList;
private CustomFinanceListAdapter adapter;
private String flag;
private Button newTran;
// public constants
public static final String INCOME = "i";
public static final String OUTGO = "o";
// Variables for Contextmenu
private static final int DETAILS_OPTION = Menu.FIRST;
private static final int DELETE_OPTION = DETAILS_OPTION + 1;
public TransactionSectionFragment() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnDataChangedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnDataChangedListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_account_transactions, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
newTran = (Button) getView().findViewById(R.id.new_entry_btn);
transactionList = (ListView) this.getView().findViewById(
R.id.transactionlist);
flag = getArguments().getString("flag");
datasource = new FinanceDataSource(getActivity());
datasource.open();
updateList();
registerForContextMenu(transactionList);
transactionList
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Transaction tran = (Transaction) transactionList
.getItemAtPosition(position);
showTransactionDetails(tran);
}
});
newTran.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
datasource.insertSomeTransactions();
updateList();
}
});
}
@Override
public void onResume() {
datasource.open();
super.onResume();
}
@Override
public void onPause() {
datasource.close();
super.onPause();
}
private void showTransactionDetails(Transaction tran) {
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.transactionlist) {
menu.setHeaderTitle("Optionen ");
menu.add(Menu.NONE, DETAILS_OPTION, 0, "Details");
menu.add(Menu.NONE, DELETE_OPTION, 1, "Eintrag löschen");
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
Transaction tran = (Transaction) transactionList
.getItemAtPosition(info.position);
switch (item.getItemId()) {
case DETAILS_OPTION:
showMsg(tran.getID() + "");
break;
case DELETE_OPTION:
try {
datasource.deleteTransaction(tran);
updateList();
showMsg("Entry deleted!");
} catch (Exception e) {
Log.i("Exception", e.getMessage());
}
break;
}
return true;
}
// Show Toast
private void showMsg(String message) {
Toast msg = Toast.makeText(getActivity(), message, Toast.LENGTH_LONG);
msg.show();
}
private void updateList() {
adapter = new CustomFinanceListAdapter(
datasource.getAllTransactions(flag), getActivity());
transactionList.setAdapter(adapter);
mCallback.onDataChanged(new Random().nextInt());
}
}
そして今問題:収入を表す最初のフラグメントは、「i」でフラグが立てられたsqliteデータベースからのデータで満たされています。2 番目の Fragment は、「o」のフラグが付いた outgo です。これはかなりうまくいきます。リストビューのすべてのエントリはトランザクションを表します ---> ここを参照してください:
public class Transaction {
// private variables
int id;
String date;
double value;
String desc;
String flag;
// Empty constructor
public Transaction() {
}
// constructor
public Transaction(String date, double value, String desc, String flag) {
this.date = date;
this.value = value;
this.desc = desc;
this.flag = flag;
}
// constructor
public Transaction(int id, String date, double value, String desc,
String flag) {
this.id = id;
this.date = date;
this.value = value;
this.desc = desc;
this.flag = flag;
}
// get name
public int getID() {
return this.id;
}
// get name
public String getDate() {
return this.date;
}
// set name
public void setDate(String date) {
this.date = date;
}
// get value
public double getValue() {
return this.value;
}
// set value
public void setValue(double value) {
this.value = value;
}
// get description
public String getDesc() {
return this.desc;
}
// set description
public void setDesc(String desc) {
this.desc = desc;
}
// get flag
public String getFlag() {
return this.flag;
}
// set flag
public void setFlag(String flag) {
this.flag = flag;
}
@Override
public String toString() {
return this.getDesc();
}
}
リスト項目を長押しすると、データベースにあるものと同じトランザクションの ID を取得します (自動インクリメント主キーなど)。したがって、一意である必要があります。しかし、outgo-list の最初のアイテムは、income-list の最初のアイテムと同じ ID を持っています。収入リストから項目を削除すると、結果リストの最後の項目が outofboundexception になります。ただし、結果リストは収入のように更新されず、異なる値を持つ収入の「コピー」に似ています。
あなたが私の問題を解決してくれることを願っています....さらにコードが必要な場合は教えてください =)
編集 1: テーブルを作成する SQL ステートメントは次のとおりです。
USERS_TABLE_CREATE = "CREATE TABLE "
+ TABLE_FINANCES + " (" + COLUMN_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + COLUMN_DATE
+ " DATETIME NOT NULL," + COLUMN_VALUE + " REAL NOT NULL," + COLUMN_DESC
+ " NVARCHAR(255)," + COLUMN_FLAG + " NVARCHAR(1) NOT NULL);";
データセット自体は次のようになります。
(id, date, value, description, flag) ==> ex1: (2, 23.08.2013, 33.33, "tip", "i") 収入データセットと ex2: (3, 23.08.2013, 12.12, "結果データセットのチケット"、"o")
ここでいくつかの写真: 最初のエントリの getID() を使用した収入画面 --> http://i.imgur.com/kfCkjGr.png最初のエントリ の getID() を使用した出国画面 --> http://i.imgur.com /tOdoSqb.png
EDIT 2: 私は次の助けを借りて問題を解決しました: ViewPager の間違ったフラグメントが onContextItemSelected 呼び出しを受け取ります
onContextItemSelected Call をこれでラップする必要があります。これは、fragmentmanager が、実際に使用しているフラグメントではなく、contextmenu に対して true を返す最初の利用可能なフラグメントを取得するためです。
if (getUserVisibleHint()) {
// Handle menu events and return true
} else
return false; // Pass the event to the next fragment
}