I make asset management system information project, and save the data in SQLite database
. I want to show the information and time in the listview
, but it just show the information. I want both to show up in the listview
. Any ideas how to do it ?
The string for the information is daftar
and the time is 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>
Here's my 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;
}
}
I finally get the answer, thanks for helping me. I know the code is still in messed up, but it works. Thanks for everyone who helping me. I make the CRUD project based SQLite Database and the listview show the subitem from the database. You can download the project 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();
}
}
For everyone who helping me, Thanks.