我只需要对我的简单项目进行小改动,在我的类中通过单击btnDisplay
从ListView中使用ListView检索数据,
我只需要点击,当我正在运行我的课程时,只需显示"id","Nama_Gedung"
,这个逻辑与旧代码相同,但只需要点击btnDisplay
那我该怎么处理我的代码呢?非常感谢你。
public class MainParkirDalam extends ListActivity implements android.view.View.OnClickListener{
Button btnAdd,btnGetAll, btnClose;
TextView parkirdalam_Id;
@Override
public void onClick(View view) {
if (view== findViewById(R.id.btnAdd)){
Intent intent = new Intent(this,ParkirDalamDetail.class);
intent.putExtra("parkirdalam_Id",0);
startActivity(intent);
}else {
ParkirDalamRepo repo = new ParkirDalamRepo(this);
ArrayList<HashMap<String, String>> parkirdalamList = repo.getParkirDalamList();
if(parkirdalamList.size()!=0) {
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
parkirdalam_Id = (TextView) view.findViewById(R.id.parkirdalam_Id);
String parkirdalamId = parkirdalam_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),ParkirDalamDetail.class);
objIndent.putExtra("parkirdalam_Id", Integer.parseInt( parkirdalamId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( MainParkirDalam.this,parkirdalamList, R.layout.view_parkirdalam_entry, new String[] { "id","Nama_Gedung"}, new int[] {R.id.parkirdalam_Id, R.id.nama_gedung});
setListAdapter(adapter);
}else{
Toast.makeText(this,"Tidak Ada Area Parkir Dalam",Toast.LENGTH_SHORT).show();
}
if (view== findViewById(R.id.btnClose)){
Intent intent = new Intent(this,MainMenuActivity.class);
startActivity(intent);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parkir_dalam);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
btnGetAll = (Button) findViewById(R.id.btnGetAll);
btnGetAll.setOnClickListener(this);
btnClose = (Button) findViewById(R.id.btnClose);
btnClose.setOnClickListener(this);
}
Parkir_dalam_actvity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tambah"
android:id="@+id/btnAdd"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/btnGetAll"
android:layout_toStartOf="@+id/btnGetAll" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_above="@+id/btnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tampilkan"
android:id="@+id/btnGetAll"
android:layout_below="@android:id/list"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Keluar"
android:id="@+id/btnClose"
android:layout_alignTop="@+id/btnGetAll"
android:layout_toRightOf="@+id/btnGetAll"
android:layout_toEndOf="@+id/btnGetAll" />
</RelativeLayout>
}
View_ParkirDalam_Entry
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/parkirdalam_Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/nama_gedung"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textSize="22sp"
android:textStyle="bold" />
</LinearLayout>
答案 0 :(得分:0)
这段代码可以放在onCreate
的底部,但是,您可能仍然需要一个按钮事件,因为如果您想在以后更新ListView,这将是有意义的。在这种情况下,例如,最后几行应该是loadListViewFromDatabase()
方法。
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
parkirdalam_Id = (TextView) view.findViewById(R.id.parkirdalam_Id);
String parkirdalamId = parkirdalam_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),ParkirDalamDetail.class);
objIndent.putExtra("parkirdalam_Id", Integer.parseInt( parkirdalamId));
startActivity(objIndent);
}
});
// loadListViewFromDatabase()
ParkirDalamRepo repo = new ParkirDalamRepo(this);
ArrayList<HashMap<String, String>> parkirdalamList = repo.getParkirDalamList();
ListAdapter adapter = new SimpleAdapter( MainParkirDalam.this,parkirdalamList, R.layout.view_parkirdalam_entry,
new String[] { "id","Nama_Gedung"},
new int[] {R.id.parkirdalam_Id, R.id.nama_gedung});
setListAdapter(adapter);