在ListView Android中使用子项

问题描述 投票:0回答:1

我制作资产管理系统信息项目,并将数据保存在SQLite数据库中。我想在列表视图中显示信息和时间,但它只是显示信息。我希望两者都显示在列表视图中。有什么想法怎么做?

此信息的字符串为“ 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>

这是我的数据库.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;
 }

}

android android-listview
1个回答
0
投票

我在您的RefreshList函数中进行了更改。只需删除您的SQLite数据并替换为本地数据)。查看图片以供参考enter image description here

 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一次只能处理一个字符串。

对于教程,请检查此链接Link 1Link 2


0
投票

您有两个选择,必须将信息和时间都发送到一个字符串中并发送到适配器,否则创建自定义布局并从当前布局中调用它

© www.soinside.com 2019 - 2024. All rights reserved.