在 ListView 中使用子项 Android
Using sub item in ListView Android
我做资产管理系统信息化项目,数据保存在SQLite database
。我想在listview
中显示信息和时间,但它只是显示信息。我希望两者都出现在 listview
中。有什么想法吗?
信息的字符串是daftar
,时间是timer
。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
</ListView>
</LinearLayout>
这是我的 database.java
public class Database extends Activity {
String[] daftar,timer;
ListView ListView01;
Menu menu;
protected Cursor cursor,cursor2;
DataCenter dbcenter;
public static Database ma;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.database);
ma = this;
dbcenter = new DataCenter(this);
RefreshList();
}
public void RefreshList(){
SQLiteDatabase db = dbcenter.getReadableDatabase();
cursor = db.rawQuery("SELECT * FROM data",null);
cursor2 = db.rawQuery("SELECT * FROM data",null);
timer = new String[cursor2.getCount()];
daftar = new String[cursor.getCount()];
cursor.moveToFirst();
for (int cc=0; cc < cursor.getCount(); cc++){
cursor.moveToPosition(cc);
daftar[cc] = cursor.getString(1).toString();
}
cursor2.moveToNext();
for (int bb=0; bb < cursor2.getCount(); bb++){
cursor2.moveToPosition(bb);
timer[bb] = cursor2.getString(3).toString();
}
ListView01 = (ListView)findViewById(R.id.listView1);
ListView01.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, daftar));
ListView01.setSelected(true);
ListView01.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
final String selection = daftar[arg2]; //.getItemAtPosition(arg2).toString();
final CharSequence[] dialogitem = {"View", "Edit", "Delete"};
AlertDialog.Builder builder = new AlertDialog.Builder(Database.this);
builder.setTitle("Pilih Menu");
builder.setItems(dialogitem, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item){
case 0 :
Intent i = new Intent(getApplicationContext(), DetailDatabase.class);
i.putExtra("informasi", selection);
startActivity(i);
break;
case 1 :
Intent in = new Intent(getApplicationContext(), UbahDatabase.class);
in.putExtra("informasi", selection);
startActivity(in);
break;
case 2 :
SQLiteDatabase db = dbcenter.getWritableDatabase();
db.execSQL("delete from data where informasi = '"+selection+"'");
RefreshList();
break;
}
}
});
builder.create().show();
}});
((ArrayAdapter)ListView01.getAdapter()).notifyDataSetInvalidated();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我对您的 RefreshList 函数进行了更改。只是删除了您的 SQLite 数据并替换为本地数据)。检查图像以供参考
public void RefreshList(){
timer = new String[6];
daftar = new String[6];
for (int cc=0; cc < daftar.length; cc++)
daftar[cc] = "daftar " + cc;
for (int bb=0; bb < timer.length; bb++)
timer[bb] = "timer "+bb;
ListView01 = findViewById(R.id.listView1);
ArrayList<HashMap<String, Object>> listArg = new ArrayList<>();
HashMap<String, Object> map;
for(int i=0; i<timer.length; i++){
map = new HashMap<>();
map.put("timer", timer[i]);
map.put("daftar", daftar[i]);
listArg.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, listArg, R.layout.row, new String[]{"timer", "daftar"}, new int[]{R.id.tvTimer, R.id.tvDaftar});
ListView01.setAdapter(adapter);
ListView01.setSelected(true);
}
您需要使用简单的或自定义的适配器,因为您当前的适配器 ArrayAdapter 一次只能处理一个字符串。
我终于得到答案了,谢谢你的帮助。我知道代码仍然一团糟,但它可以工作。感谢所有帮助过我的人。我使基于 SQLite 数据库的 CRUD 项目和列表视图显示数据库中的子项。您可以下载该项目
HERE
public void RefreshList(){
SQLiteDatabase db = dbcenter.getReadableDatabase();
cursor = db.rawQuery("SELECT * FROM biodata",null);
daftar = new String[cursor.getCount()];
timer = new String[cursor.getCount()];
ArrayList<Map<String,Object>> itemDataList = new ArrayList<Map<String,Object>>();;
cursor.moveToFirst();
for (int cc=0; cc < cursor.getCount(); cc++){
cursor.moveToPosition(cc);
daftar[cc] = cursor.getString(1).toString();
timer[cc] = cursor.getString(3).toString();
String[] daftarr = daftar;
String[] timerr = timer;
Map<String,Object> listItemMap = new HashMap<String,Object>();
listItemMap.put("title", daftarr[cc]);
listItemMap.put("description", timerr[cc]);
itemDataList.add(listItemMap);
ListView01 = (ListView)findViewById(R.id.listView1);
SimpleAdapter simpleAdapter = new SimpleAdapter(this,itemDataList,android.R.layout.simple_list_item_2,
new String[]{"title","description"},new int[]{android.R.id.text1,android.R.id.text2});
ListView01.setAdapter(simpleAdapter);
ListView01.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
final String selection = daftar[arg2]; //.getItemAtPosition(arg2).toString();
final CharSequence[] dialogitem = {"View", "Edit", "Delete"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Pilih Menu");
builder.setItems(dialogitem, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item){
case 0 :
Intent i = new Intent(getApplicationContext(), LihatActivity.class);
i.putExtra("nama", selection);
startActivity(i);
break;
case 1 :
Intent in = new Intent(getApplicationContext(), UbahActivity.class);
in.putExtra("nama", selection);
startActivity(in);
break;
case 2 :
SQLiteDatabase db = dbcenter.getWritableDatabase();
db.execSQL("delete from biodata where nama = '"+selection+"'");
RefreshList();
break;
}
}
});
builder.create().show();
}});
((SimpleAdapter)ListView01.getAdapter()).notifyDataSetInvalidated();
}
}
感谢所有帮助过我的人。
我做资产管理系统信息化项目,数据保存在SQLite database
。我想在listview
中显示信息和时间,但它只是显示信息。我希望两者都出现在 listview
中。有什么想法吗?
信息的字符串是daftar
,时间是timer
。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
</ListView>
</LinearLayout>
这是我的 database.java
public class Database extends Activity {
String[] daftar,timer;
ListView ListView01;
Menu menu;
protected Cursor cursor,cursor2;
DataCenter dbcenter;
public static Database ma;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.database);
ma = this;
dbcenter = new DataCenter(this);
RefreshList();
}
public void RefreshList(){
SQLiteDatabase db = dbcenter.getReadableDatabase();
cursor = db.rawQuery("SELECT * FROM data",null);
cursor2 = db.rawQuery("SELECT * FROM data",null);
timer = new String[cursor2.getCount()];
daftar = new String[cursor.getCount()];
cursor.moveToFirst();
for (int cc=0; cc < cursor.getCount(); cc++){
cursor.moveToPosition(cc);
daftar[cc] = cursor.getString(1).toString();
}
cursor2.moveToNext();
for (int bb=0; bb < cursor2.getCount(); bb++){
cursor2.moveToPosition(bb);
timer[bb] = cursor2.getString(3).toString();
}
ListView01 = (ListView)findViewById(R.id.listView1);
ListView01.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, daftar));
ListView01.setSelected(true);
ListView01.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
final String selection = daftar[arg2]; //.getItemAtPosition(arg2).toString();
final CharSequence[] dialogitem = {"View", "Edit", "Delete"};
AlertDialog.Builder builder = new AlertDialog.Builder(Database.this);
builder.setTitle("Pilih Menu");
builder.setItems(dialogitem, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item){
case 0 :
Intent i = new Intent(getApplicationContext(), DetailDatabase.class);
i.putExtra("informasi", selection);
startActivity(i);
break;
case 1 :
Intent in = new Intent(getApplicationContext(), UbahDatabase.class);
in.putExtra("informasi", selection);
startActivity(in);
break;
case 2 :
SQLiteDatabase db = dbcenter.getWritableDatabase();
db.execSQL("delete from data where informasi = '"+selection+"'");
RefreshList();
break;
}
}
});
builder.create().show();
}});
((ArrayAdapter)ListView01.getAdapter()).notifyDataSetInvalidated();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我对您的 RefreshList 函数进行了更改。只是删除了您的 SQLite 数据并替换为本地数据)。检查图像以供参考
public void RefreshList(){
timer = new String[6];
daftar = new String[6];
for (int cc=0; cc < daftar.length; cc++)
daftar[cc] = "daftar " + cc;
for (int bb=0; bb < timer.length; bb++)
timer[bb] = "timer "+bb;
ListView01 = findViewById(R.id.listView1);
ArrayList<HashMap<String, Object>> listArg = new ArrayList<>();
HashMap<String, Object> map;
for(int i=0; i<timer.length; i++){
map = new HashMap<>();
map.put("timer", timer[i]);
map.put("daftar", daftar[i]);
listArg.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, listArg, R.layout.row, new String[]{"timer", "daftar"}, new int[]{R.id.tvTimer, R.id.tvDaftar});
ListView01.setAdapter(adapter);
ListView01.setSelected(true);
}
您需要使用简单的或自定义的适配器,因为您当前的适配器 ArrayAdapter 一次只能处理一个字符串。
我终于得到答案了,谢谢你的帮助。我知道代码仍然一团糟,但它可以工作。感谢所有帮助过我的人。我使基于 SQLite 数据库的 CRUD 项目和列表视图显示数据库中的子项。您可以下载该项目 HERE
public void RefreshList(){
SQLiteDatabase db = dbcenter.getReadableDatabase();
cursor = db.rawQuery("SELECT * FROM biodata",null);
daftar = new String[cursor.getCount()];
timer = new String[cursor.getCount()];
ArrayList<Map<String,Object>> itemDataList = new ArrayList<Map<String,Object>>();;
cursor.moveToFirst();
for (int cc=0; cc < cursor.getCount(); cc++){
cursor.moveToPosition(cc);
daftar[cc] = cursor.getString(1).toString();
timer[cc] = cursor.getString(3).toString();
String[] daftarr = daftar;
String[] timerr = timer;
Map<String,Object> listItemMap = new HashMap<String,Object>();
listItemMap.put("title", daftarr[cc]);
listItemMap.put("description", timerr[cc]);
itemDataList.add(listItemMap);
ListView01 = (ListView)findViewById(R.id.listView1);
SimpleAdapter simpleAdapter = new SimpleAdapter(this,itemDataList,android.R.layout.simple_list_item_2,
new String[]{"title","description"},new int[]{android.R.id.text1,android.R.id.text2});
ListView01.setAdapter(simpleAdapter);
ListView01.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
final String selection = daftar[arg2]; //.getItemAtPosition(arg2).toString();
final CharSequence[] dialogitem = {"View", "Edit", "Delete"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Pilih Menu");
builder.setItems(dialogitem, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item){
case 0 :
Intent i = new Intent(getApplicationContext(), LihatActivity.class);
i.putExtra("nama", selection);
startActivity(i);
break;
case 1 :
Intent in = new Intent(getApplicationContext(), UbahActivity.class);
in.putExtra("nama", selection);
startActivity(in);
break;
case 2 :
SQLiteDatabase db = dbcenter.getWritableDatabase();
db.execSQL("delete from biodata where nama = '"+selection+"'");
RefreshList();
break;
}
}
});
builder.create().show();
}});
((SimpleAdapter)ListView01.getAdapter()).notifyDataSetInvalidated();
}
}
感谢所有帮助过我的人。