コード ```androidstudio, javaで簡単な家計簿アプリを作成しています。 実現したいこと リストビューで一番右に表示した金額の合計を下に表示したい リストビューの要素を配列のように扱えれば簡単に実現できそうと思い方法を探しましたが見つかりませんでした。 他にいい方法があれば教えて頂きたいです。 actyvity_main.xml ```ここに言語を入力 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/mainList" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="2"> </ListView> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" android:baselineAligned="false" android:layout_weight="1" android:gravity="bottom" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1" android:gravity="bottom" > <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="합계" /> <TextView android:id="@+id/textHap" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="0" /> </LinearLayout> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="match_parent" android:baselineAligned="false" android:gravity="center" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="bottom" android:layout_weight="1" android:orientation="horizontal" android:baselineAligned="false" > <Button android:id="@+id/fab_reg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginRight="1dp" android:onClick="fab_reg_onClick" android:text="지출입력" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="1dp" android:layout_weight="1" android:text="수입입력" /> </LinearLayout> </TableRow> </TableLayout> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.databasetest; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.AdapterView; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import static com.example.databasetest.DBContract.DBEntry; public class MainActivity extends AppCompatActivity { private SampDatabaseHelper helper = null; MainListAdapter sc_adapter; ListView mainList; TextView contents, textHap; // アクティビティの初期化処理 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // アクティビティの再開処理 @Override protected void onResume() { super.onResume(); // データを一覧表示 onShow(); } // データを一覧表示 protected void onShow() { // データベースヘルパーを準備 helper = new SampDatabaseHelper(this); // データベースを検索する項目を定義 String[] cols = {DBEntry._ID, DBEntry.COLUMN_NAME_TITLE, DBEntry.COLUMN_NAME_DATES,DBEntry.COLUMN_NAME_CONTENTS}; // 読み込みモードでデータベースをオープン try (SQLiteDatabase db = helper.getReadableDatabase()){ // データベースを検索 Cursor cursor = db.query(DBEntry.TABLE_NAME, cols, null, null, null, null, null, null); // 検索結果から取得する項目を定義 String[] from = {DBEntry.COLUMN_NAME_TITLE, DBEntry.COLUMN_NAME_DATES,DBEntry.COLUMN_NAME_CONTENTS}; // データを設定するレイアウトのフィールドを定義 int[] to = {R.id.title, R.id.date, R.id.contents}; // ListViewの1行分のレイアウト(row_main.xml)と検索結果を関連付け sc_adapter = new MainListAdapter( this, R.layout.row_main, cursor, from, to,0); // activity_main.xmlに定義したListViewオブジェクトを取得 ListView list = findViewById(R.id.mainList); // ListViewにアダプターを設定 list.setAdapter(sc_adapter); // リストの項目をクリックしたときの処理 list.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView av, View view, int position, long id) { // クリックされた行のデータを取得 Cursor cursor = (Cursor)av.getItemAtPosition(position); // テキスト登録画面 Activity へのインテントを作成 Intent intent = new Intent(MainActivity.this, com.example.databasetest.TextActivity.class); intent.putExtra(DBEntry._ID, cursor.getInt(0)); intent.putExtra(DBEntry.COLUMN_NAME_TITLE, cursor.getString(1)); intent.putExtra(DBEntry.COLUMN_NAME_CONTENTS, cursor.getString(2)); intent.putExtra(DBEntry.COLUMN_NAME_DATES, cursor.getString(3)); // アクティビティを起動 startActivity(intent); //====================== //アイテム数に合わせて配列作成String //intに変換、intの配列にいれる //forで回しながら合計int hapにいれる //hapをsettext // mainList = (ListView) findViewById(R.id.mainList); // //int first = mainList.getFirstVisiblePosition(); int last = mainList.getLastVisiblePosition(); // contents = (TextView)findViewById(R.id.contents); // String[] hapS; // int[] hapI; // // for (int i=0; i<= last; i++){ // hapS[i] = contents[i].getText().toString(); // } int hap = 0; contents = (TextView)findViewById(R.id.contents); String[] hapS = {contents.getText().toString()}; int[] hapI = new int[last]; for (int i=0; i>=last; i++){ hapI[i] = Integer.parseInt(hapS[i]); hap += hapI[i]; } textHap = (TextView) findViewById(R.id.textHap); textHap.setText(hap); // } }); } } // 削除ボタン タップ時に呼び出されるメソッド public void btnDel_onClick(View view){ // MainListAdapterで設定されたリスト内の位置を取得 int pos = (Integer)view.getTag(); // アダプターから、_idの値を取得 int id = ((Cursor) sc_adapter.getItem(pos)).getInt(0); // データを削除 try (SQLiteDatabase db = helper.getWritableDatabase()) { db.delete(DBEntry.TABLE_NAME, DBEntry._ID+" = ?", new String[] {String.valueOf(id)}); } // データを一覧表示 onShow(); } // 「+」フローティング操作ボタン タップ時に呼び出されるメソッド public void fab_reg_onClick(View view) { // テキスト登録画面 Activity へのインテントを作成 Intent intent = new Intent(MainActivity.this, com.example.databasetest.TextActivity.class); // アクティビティを起動 startActivity(intent); } }
MainListAdapter.java
package com.example.databasetest; import android.content.Context; import android.database.Cursor; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageButton; import android.widget.SimpleCursorAdapter; public class MainListAdapter extends SimpleCursorAdapter { // コンストラクタ public MainListAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) { super(context, layout, c, from, to, flags); } // 指定データのビューを取得 @Override public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); // 削除ボタン オブジェクトを取得 Button btnDel = (Button) view.findViewById(R.id.button_delete); // ボタンにリスト内の位置を設定 btnDel.setTag(position); return view; } }
回答1件
あなたの回答
tips
プレビュー