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

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

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

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

Android Studio

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

Q&A

1回答

2294閲覧

Android StudioのListViewとSQLiteについて

hatch_1564

総合スコア8

Java

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

Android Studio

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

0グッド

0クリップ

投稿2017/02/09 02:33

ListViewを使った実験的なアプリを作っているのですが、ListViewの横に画像を表示させたいと思っているのですが、どうすれば表示できるようになりますか?
また、ListViewで表示する項目をデータベースで管理して表示したいのですが、やり方がわからずどうすればいいかわかりません。
なので、やり方を教えていただきたいです!
コードがごちゃごちゃで読みづらいと思いますが、回答していただけると幸いです...。

MainActivity.java

java

1public class MainActivity extends AppCompatActivity implements View.OnClickListener { 2 3 //private ArrayAdapter<String> adapter; 4 5 private RowModelAdapter adapter; 6 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 13 14 /* 15 final ArrayAdapter<String> adapter 16 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); 17 18 adapter.add("まぐろ"); 19 adapter.add("いか"); 20 adapter.add("うに"); 21 */ 22 23 24 adapter = new RowModelAdapter(this); 25 adapter.add(new RowModel("うに", 360, R.id.icon1)); 26 adapter.add(new RowModel("おおとろ", 520, R.id.icon1)); 27 adapter.add(new RowModel("いくら", 240, R.id.icon1)); 28 29 30 ListView list = (ListView) findViewById(R.id.listView1); 31 list.setAdapter(adapter); 32 33 34 list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 35 @Override 36 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 37 ListView listView = (ListView) parent; 38 RowModel item = (RowModel) listView.getItemAtPosition(position); 39 Toast.makeText(MainActivity.this, item.getName() + ":" + item.getPrice() + "円", Toast.LENGTH_SHORT).show(); 40 41 // adapter.remove(item); 42 // adapter.notifyDataSetChanged(); 43 } 44 }); 45 46 47 list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 48 @Override 49 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 50 ListView listView = (ListView) parent; 51 RowModel item = (RowModel) listView.getItemAtPosition(position); 52 adapter.remove(item); 53 adapter.notifyDataSetChanged(); 54 Toast.makeText(MainActivity.this, item.getName() + ":" + item.getPrice() + "円を消去しました", Toast.LENGTH_SHORT).show(); 55 return false; 56 } 57 }); 58 59 60 Button btnEntry = (Button) findViewById(R.id.btnEntry); 61 btnEntry.setOnClickListener(this); 62 } 63 64 65 @Override 66 public void onClick(View v) { 67 EditText edt = (EditText) findViewById(R.id.edtEntry); 68 69 EditText edtEntryPrice = (EditText) findViewById(R.id.edtEntryPrice); 70 71 //adapter.insert(new RowModel(edt.getText().toString(), Integer.parseInt(edtEntryPrice.getText().toString())), 0); 72 73 adapter.notifyDataSetChanged(); 74 75 edt.setText(""); 76 edtEntryPrice.setText(""); 77 } 78 79 80 81 82 class RowModelAdapter extends ArrayAdapter<RowModel> { 83 84 85 86 public RowModelAdapter(Context context) { 87 super(context, R.layout.row_item); 88 } 89 90 91 @Override 92 public View getView(int position, View convertView, ViewGroup parent) { 93 94 //アダプターにセットされている該当のデータを取り出す 95 RowModel item = getItem(position); 96 97 98 //ListViewが初めて表示される場合は引数のconvertView(行のView)は 99 //nullなので行のレイアウトをinflate(適用)させる必要がある。 100 101 if (convertView == null) { 102 LayoutInflater inflater = getLayoutInflater(); 103 convertView = inflater.inflate(R.layout.row_item, null); 104 } 105 106 if (item != null) { 107 108 //convertViewが持っているレイアウトリソース(row_item.xml)からTextViewを取り出す 109 //寿司の名前のTextViewを取得 110 TextView txt1 = (TextView) convertView.findViewById(R.id.txtName); 111 112 if (txt1 != null) { 113 114 //寿司の名前をセット 115 txt1.setText(item.getName()); 116 } 117 118 //寿司の値段のTextViewを取得 119 TextView txt2 = (TextView) convertView.findViewById(R.id.txtPrice); 120 if (txt2 != null) { 121 122 //寿司の値段をセット 123 txt2.setText(String.valueOf(item.getPrice())); 124 } 125 126 } 127 128 return convertView; 129 } 130 } 131} 132

RowModel.java

java

1public class RowModel { 2 3 private String name; 4 private int price; 5 private int icon; 6 7 8 9 public RowModel(String name, int price, int icon) { 10 this.name = name; 11 this.price = price; 12 this.icon = icon; 13 } 14 15 16 17 18 public String getName() { 19 return name; 20 } 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 27 public int getPrice() { 28 29 return price; 30 } 31 32 33 public void setPrice(int price) { 34 35 this.price = price; 36 } 37 38 39 public int getIcon() { 40 return icon; 41 } 42 43 public void setIcon(int icon) { 44 this.icon = icon; 45 } 46}

activity_main.xml

xml

1 <LinearLayout 2 android:layout_width="match_parent" 3 android:layout_height="wrap_content" 4 android:orientation="horizontal"> 5 6 <EditText 7 android:layout_width="0dp" 8 android:layout_height="wrap_content" 9 android:layout_weight="75.67" 10 android:id="@+id/edtEntry" 11 android:onClick="onClick" 12 android:hint="寿司の名前" /> 13 14 <EditText 15 android:layout_width="0dp" 16 android:layout_height="wrap_content" 17 android:id="@+id/edtEntryPrice" 18 android:layout_weight="41.66" 19 android:onClick="onClick" 20 android:hint="金額" /> 21 22 <Button 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:id="@+id/btnEntry" 26 android:text="entry"/> 27 28 </LinearLayout> 29 30 <LinearLayout 31 android:orientation="horizontal" 32 android:layout_width="match_parent" 33 android:layout_height="match_parent"> 34 35 <ImageView 36 android:layout_width="65dp" 37 android:layout_height="40dp" 38 android:id="@+id/icon1" 39 android:src="@drawable/osusi" 40 android:layout_marginRight="16dp"/> 41 42 <ListView 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:id="@+id/listView1" 46 android:entries="@array/items"> 47 </ListView> 48 </LinearLayout> 49 50</LinearLayout> 51

row_item.xml

xml

1<ImageView 2 android:layout_width="100dp" 3 android:layout_height="61dp" 4 android:id="@+id/icon1" 5 android:src="@drawable/osusi" /> 6 7 <LinearLayout 8 android:orientation="vertical" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent"> 11 12 <TextView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="寿司の名前をここに表示" 16 android:id="@+id/txtName" 17 android:textSize="25dp"/> 18 19 <TextView 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="108円" 23 android:id="@+id/txtPrice" 24 android:textSize="20dp"/> 25 </LinearLayout> 26</LinearLayout> 27

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

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

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

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

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

guest

回答1

0

現状のレイアウトファイルでListViewの横に画像は表示されると思いますよ、それとも1行ごとに画像を表示したいのでしょうか?

また、SQLiteについてはListViewとの連携を考えずにデータベースの作成と検索について調べて、ある程度実装してから質問をしたほうがいいですよ。

投稿2017/02/09 05:20

yona

総合スコア18155

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問