私は最近同様の質問をしています。それから、カスタムリストアイテムで本当に良いチュートリアルを見つけました。
まず、データベースのレコードをobject
に保存してください。
最初に、行ビューを作成する必要があります。したがって、例えば次のコードのような簡単なXMLファイルの挿入を作成し、res/layout
フォルダにmyobject_list_item
という名前で保存します。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/column1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/column2"
android:padding="10dp"
android:paddingLeft="5dp"
android:text="Column1"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textSize="20sp" />
<TextView
android:id="@+id/column2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/gewichtung"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:padding="10dp"
android:text="Column2"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textSize="20sp" />
</RelativeLayout>
その後、カスタムリストアダプターを作成する必要があります。だから、名前MyObject_ListAdapter
で新しいJavaファイルを作成し、次のコードを挿入します。
package net.example.app;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import net.example.app.R;
import java.util.ArrayList;
public class MyObject_ListAdapter extends ArrayAdapter<MyObject> {
// Source:
// http://hmkcode.com/android-custom-listview-items-row/
private ArrayList<MyObject> objects;
private Context context;
public MyObject_ListAdapter(Context context, ArrayList<MyObject> objects) {
super(context, R.layout.myobject_list_item, objects);
this.objects = objects;
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.fach_list_item, parent, false);
// 3. Get the two text view from the rowView
TextView column1 = (TextView) rowView.findViewById(R.id.column1);
TextView column2 = (TextView) rowView.findViewById(R.id.column2);
RelativeLayout item = (RelativeLayout) rowView.findViewById(R.id.item);
// 4. Set the text for textView
column1.setText(objects.get(position).getName());
column2.setText(objects.get(position).getSecondName());
// 5. return rowView
return rowView;
}
}
は今、あなたの活動に簡単なListView
を追加し、このためにlv
ようなIDを追加します。私はこのチュートリアルでは、あなたを助けることを願ってい
package net.example.app;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import net.example.app.MyObject;
import net.example.app.MyObject_ListAdapter;
import java.util.ArrayList;
public class MyActivity extends AppCompatActivity {
private ArrayAdapter arrayAdapter;
private ArrayList<MyObject> myObjects = new ArrayList<>();
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
db = openOrCreateDatabase("database.db", MODE_PRIVATE, null);
ListView lv = (ListView) findViewById(R.id.lv);
arrayAdapter = new MyObject_ListAdapter(this, myObjects); //Define the custom list adapter with the activity and arraylist
lv.setAdapter(arrayAdapter); //Connect your listview with the adapter
displayData();
}
private void displayData() {
Cursor c = db.rawQuery("SELECT * FROM my_table", null);
while (c.moveToNext()) { //Loop through all the records
//Now on the variable 'c' there is one record.
int column_a_name = c.getColumnIndex("my_column1"); //Get the index of the column from your table.
String column_a_value = c.getString(column_a_name); //Get the value from the column from the current record.
int column_b_name = c.getColumnIndex("my_column2");
String column_b_value = c.getString(column_b_name);
//Now you can do with the value what you want.
myObjects.add(new MyObject(column_a_value, column_b_value));
}
arrayAdapter.notifyDataSetChanged(); //Notify, that you have changed some data in the array list.
}
}
: は、その後、あなたのJavaのアクティビティでは、次のコードを挿入することができます。
ありがとうございました。私はデータを取得して到達することができますが、問題はテーブルに応じて変更するアクティビティを持たせたいということです。そのアクティビティには未加工のデータも表示されます。 –
@ M.Kaan私は新しい答えを追加しました。 –