我目前正在尝试创建一个应用程序,该应用程序在数据库中存储一些带有额外信息的产品,并在自定义列表视图中显示。从这个列表视图中,我希望能够通过点击其中一个产品并显示新活动来编辑数据库。我已经设法通过制作一个单独的listview布局并将其添加到列表视图来实现自定义列表视图布局,但我无法找到一种方法来进行点击。活动还必须将该产品的信息传递给另一个活动,因此必须以某种方式提取与被点击的行相关的信息以放入意图中。下面是代码:
显示列表视图的活动:
public class Products extends Activity implements OnClickListener{
ImageButton bProductsBanner, bAdd, bSearch, bAddScan, bAddConfirm;
EditText etSearch, etAddProduct, etProductName;
TextView tvName, tvAmount, tvDate;
DatabaseCustom ourDb;
ListView ourList;
SimpleCursorAdapter ourCursorAdapter;
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(R.layout.products);
openDb();
initiate();
populateListView();
}
public void onClick(View v) {
switch (v.getId()){
case R.id.ibProductsBanner:
finish();
break;
case R.id.ibAddScan:
Intent openScanner = new Intent(Products.this,
Scanner.class);
startActivity(openScanner);
finish();
break;
case R.id.ibAddConfirm:
String productName = etAddProduct.getText().toString();
Intent openProductsAdd = new Intent(Products.this,
ProductsAdd.class);
openProductsAdd.putExtra("productName", productName);
startActivity(openProductsAdd);
finish();
break;
case R.id.ibSearch:
etSearch.setVisibility(View.VISIBLE);
etAddProduct.setVisibility(View.GONE);
bAddConfirm.setVisibility(View.GONE);
bAddScan.setVisibility(View.GONE);
break;
case R.id.ibAdd:
etSearch.setVisibility(View.GONE);
etAddProduct.setVisibility(View.VISIBLE);
bAddConfirm.setVisibility(View.VISIBLE);
bAddScan.setVisibility(View.VISIBLE);
break;
case R.id.tvProductNameList:
openProductsEdit();
break;
case R.id.tvAmountList:
openProductsEdit();
break;
case R.id.tvDateList:
openProductsEdit();
break;
}
}
private void initiate(){
bProductsBanner = (ImageButton)findViewById(R.id.ibProductsBanner);
bAddScan = (ImageButton)findViewById(R.id.ibAddScan);
bAddConfirm = (ImageButton)findViewById(R.id.ibAddConfirm);
bSearch = (ImageButton)findViewById(R.id.ibSearch);
bAdd = (ImageButton)findViewById(R.id.ibAdd);
etAddProduct = (EditText)findViewById(R.id.etAddProduct);
etSearch = (EditText)findViewById(R.id.etSearch);
ourList = (ListView)findViewById(R.id.lvProducts);
bProductsBanner.setOnClickListener(this);
bAddScan.setOnClickListener(this);
bAddConfirm.setOnClickListener(this);
bSearch.setOnClickListener(this);
bAdd.setOnClickListener(this);
}
private void openDb(){
ourDb = new DatabaseCustom(this);
ourDb.open();
}
private void populateListView(){
Cursor cursor = ourDb.getData();
String[] fromFieldNames = new String[] {DatabaseCustom.KEY_NAME,DatabaseCustom.KEY_AMOUNT,DatabaseCustom.KEY_DATE}; //Get text from KEYS
int[] toViewIDs = new int[] {R.id.tvProductNameList, R.id.tvAmountList, R.id.tvDateList}; //References TVs in item_layout.xml
ourCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.item_layout, cursor, fromFieldNames, toViewIDs, 0);//Put text from KEYS into TVs
//ListView ourList = (ListView) findViewById(R.id.lvProducts); //Reference the ListView in products.xml (already done in initiate())
ourList.setAdapter(ourCursorAdapter); //Put ourCursorAdapter into ourList
}
public void openProductsEdit(){
String name = tvName.getText().toString();
String amount = tvAmount.getText().toString();
String date = tvDate.getText().toString();
Intent openProductsEdit = new Intent(Products.this,
ProductsEdit.class);
openProductsEdit.putExtra("name", name);
openProductsEdit.putExtra("amount", amount);
openProductsEdit.putExtra("date", date);
startActivity(openProductsEdit);
finish();
}
}
使用了两个布局文件,products.xml文件是此类的标准布局,item_layout.xml用于列表视图:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvProductNameList"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:inputType="textCapSentences"
android:text="Name"
android:textSize="20sp" />
<TextView
android:id="@+id/tvDateList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Date"
android:textSize="20sp" />
<TextView
android:id="@+id/tvAmountList"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/tvDateList"
android:text="Mnt"
android:textSize="20sp" />
"
</RelativeLayout>
向可以帮助我的人致敬!
答案 0 :(得分:0)
当您将几个基本问题合二为一时,以下是您想要的指南:
如果确实有理由让产品通过多个活动而不仅仅是ID,则可以在应用程序中定义对象的静态实例。即使这似乎是你应该更加轻松的方式。
答案 1 :(得分:0)
该列表可以通过onItemClickedListener通知项目点击次数。列表器通过类似的命名集函数设置。在回调中,该位置表示正在点击哪一行。