質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

1158閲覧

RecyclerViewでの行選択によるクリックリスナの設定

Aies

総合スコア21

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2019/01/31 06:37

いまのところカテゴリ、メモ、金額といった形で横に並びそれがデータ件数分表示される。といったような処理なのですが、そのデータをクリックしたとき、そのポジションを手に入れるリスナを作りたいです。調べ型が悪いのか、この形に応用できるようなものが見つからなかったので、よろしければご助言お願いいたします。

Java

1package com.example.a162105.kakeibo; 2 3import android.content.Intent; 4import android.database.Cursor; 5import android.database.sqlite.SQLiteDatabase; 6import android.os.Bundle; 7import android.support.annotation.NonNull; 8import android.support.design.widget.BottomNavigationView; 9import android.support.v7.app.AppCompatActivity; 10import android.support.v7.widget.DividerItemDecoration; 11import android.support.v7.widget.LinearLayoutManager; 12import android.support.v7.widget.RecyclerView; 13import android.view.Menu; 14import android.view.MenuItem; 15import android.view.View; 16import android.widget.ListView; 17import android.widget.TextView; 18 19import java.util.ArrayList; 20 21public class DailyData extends AppCompatActivity { 22 23 private TextView mTextMessage; 24 25 private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener 26 = new BottomNavigationView.OnNavigationItemSelectedListener() { 27 28 @Override 29 public boolean onNavigationItemSelected(@NonNull MenuItem item) { 30 switch (item.getItemId()) { 31 case R.id.navigation_input: 32 mTextMessage.setText(R.string.title_home); 33 return true; 34 case R.id.navigation_calendar: 35 mTextMessage.setText(R.string.title_dashboard); 36 return true; 37 case R.id.navigation_config: 38 mTextMessage.setText(R.string.title_notifications); 39 return true; 40 } 41 return false; 42 } 43 }; 44 45 private KakeiboDatabase kDB; 46 private RecyclerView mRecyclerView; 47 private RecyclerView.Adapter mAdapter; 48 String date; 49 50 @Override 51 protected void onCreate(Bundle savedInstanceState) { 52 super.onCreate(savedInstanceState); 53 setContentView(R.layout.activity_daily_data); 54 55 kDB = new KakeiboDatabase(getApplicationContext()); 56 57 mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView); 58 DailyAdapter dailyAdapter = new DailyAdapter(this.createDataset()); 59 60 LinearLayoutManager llm = new LinearLayoutManager(this); 61 62 mRecyclerView.setHasFixedSize(true); 63 mRecyclerView.setLayoutManager(llm); 64 mRecyclerView.setAdapter(dailyAdapter); 65 66 DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(), 67 new LinearLayoutManager(getApplication()).getOrientation()); 68 mRecyclerView.addItemDecoration(dividerItemDecoration); 69 70 71 } 72 73 74 75 private ArrayList<Daily> createDataset() { 76 77 Intent intent = getIntent(); 78 date = intent.getStringExtra("CalendarID"); 79 80 ArrayList<Daily> dataset = new ArrayList<>(); 81 82 SQLiteDatabase db = kDB.getReadableDatabase(); 83 84 Cursor cursor = db.query( 85 "kakeibodb", 86 new String[] {"category", "memo", "price"}, 87 "date=?", 88 new String[] {date}, 89 null, 90 null, 91 null); 92 boolean next = cursor.moveToFirst(); 93 94 if(next) { 95 Daily title = new Daily(); 96 title.setCategory("カテゴリ"); 97 title.setMemo("メモ"); 98 title.setPrice("金額"); 99 dataset.add(title); 100 } 101 102 103 while (next) { 104 Daily daily = new Daily(); 105 // 取得したカラムの順番(0から始まる)と型を指定してデータを取得する 106 daily.setCategory(cursor.getString(0));// categoryを取得 107 daily.setMemo(cursor.getString(1));// memoを取得 108 daily.setPrice(String.valueOf(cursor.getInt(2)));// priceを取得 109 110 // 次の行が存在するか確認 111 next = cursor.moveToNext(); 112 dataset.add(daily); 113 } 114 cursor.close(); 115 116 return dataset; 117 } 118}

Java

1package com.example.a162105.kakeibo; 2 3import android.content.Context; 4import android.support.v7.widget.RecyclerView; 5import android.view.LayoutInflater; 6import android.view.View; 7import android.view.ViewGroup; 8import android.widget.BaseAdapter; 9import android.widget.TextView; 10import java.util.ArrayList; 11import java.util.List; 12 13public class DailyAdapter extends RecyclerView.Adapter<ViewHolder> { 14 15 private ArrayList<Daily> dailyList; 16 17 public DailyAdapter(ArrayList<Daily> list) { 18 this.dailyList = list; 19 } 20 21 @Override 22 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 23 View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell, parent, false); 24 ViewHolder vh = new ViewHolder(inflate); 25 return vh; 26 } 27 28 @Override 29 public void onBindViewHolder(ViewHolder holder, int position) { 30 holder.categoryView.setText(dailyList.get(position).getCategory()); 31 holder.memoView.setText(dailyList.get(position).getMemo()); 32 holder.priceView.setText(String.valueOf(dailyList.get(position).getPrice())); 33 } 34 35 @Override 36 public int getItemCount() { 37 return dailyList.size(); 38 } 39}

layout

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="wrap_content"> 5 6 <LinearLayout 7 android:orientation="horizontal" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content"> 10 11 <TextView 12 android:layout_width="0dp" 13 android:layout_height="48dp" 14 android:text="New Text" 15 android:id="@+id/dCategory" 16 android:textSize="30dp" 17 android:layout_weight="2" /> 18 19 <TextView 20 android:layout_width="0dp" 21 android:layout_height="48dp" 22 android:text="New Text" 23 android:id="@+id/dMemo" 24 android:textSize="30dp" 25 android:layout_weight="2" /> 26 27 <TextView 28 android:layout_width="0dp" 29 android:layout_height="48dp" 30 android:text="New Text" 31 android:id="@+id/dPrice" 32 android:textSize="30dp" 33 android:layout_weight="1" /> 34 </LinearLayout> 35 36</LinearLayout>

layout

1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/container" 6 android:layout_width="match_parent" 7 android:layout_height="wrap_content" 8 tools:context=".DailyData"> 9 10 <android.support.v7.widget.RecyclerView 11 android:id="@+id/recyclerView" 12 android:layout_width="394dp" 13 android:layout_height="662dp" 14 android:layout_marginStart="8dp" 15 android:layout_marginLeft="8dp" 16 android:layout_marginTop="8dp" 17 android:layout_marginEnd="8dp" 18 android:layout_marginRight="8dp" 19 android:layout_marginBottom="8dp" 20 app:layout_constraintBottom_toBottomOf="parent" 21 app:layout_constraintEnd_toEndOf="parent" 22 app:layout_constraintHorizontal_bias="0.0" 23 app:layout_constraintStart_toStartOf="parent" 24 app:layout_constraintTop_toTopOf="parent" 25 app:layout_constraintVertical_bias="0.0" /> 26 27 <android.support.design.widget.BottomNavigationView 28 android:id="@+id/navigation" 29 android:layout_width="0dp" 30 android:layout_height="wrap_content" 31 android:layout_marginStart="0dp" 32 android:layout_marginEnd="0dp" 33 android:background="?android:attr/windowBackground" 34 app:layout_constraintBottom_toBottomOf="parent" 35 app:layout_constraintLeft_toLeftOf="parent" 36 app:layout_constraintRight_toRightOf="parent" 37 app:menu="@menu/navigation" /> 38</android.support.constraint.ConstraintLayout>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

https://calculus-app.com/blog/develop_android/android_ui/303
こちらのサイトを参考にしたらリスナ搭載できました。

投稿2019/02/02 04:36

Aies

総合スコア21

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問