添加 OnClickListener 并从 ListView 中提取信息

Adding OnClickListener and extracting info from ListView

我目前正在尝试制作一个应用程序,该应用程序在数据库中存储一些带有额外信息的产品,并将其显示在自定义列表视图中。从这个列表视图中,我希望能够通过点击其中一个产品并显示新的 activity 来编辑数据库信息。我已经设法通过制作单独的列表视图布局并将其添加到列表视图来实现自定义列表视图布局,但我只是想不出一种方法来实现点击。 activity 还必须将信息从该产品传递到另一个 activity,因此必须以某种方式提取属于点击行的信息以放入意图中。代码如下:

Activity 显示列表视图:

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 文件是此 class 的标准布局,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>

向能帮我解决这个问题的人致敬!

由于您要将几个基本问​​题合二为一,因此这里有一个指南可以满足您的需求:

  • 在您的列表视图上设置 OnItemClickListener。当用户单击一行时会触发此事件。
  • 使用 Intent 启动新的 Activity(就像您所做的那样)。您可以向包含关键信息(如 ID)的 Intent 添加一个 Bundle。 Bundle 用于存储简单的数据类型,因此 ID 是首选方式。
  • 使用 ID,您可以从数据库中将数据加载到新 activity 中并进行修改。

如果确实有理由通过多个活动提供产品,而不仅仅是 ID,您可以在应用程序中定义对象的静态实例。即使这似乎是您应该小心执行此操作的更简单方法。

列表可以通过 onItemClickedListener 通知项目点击。监听器是通过类似的命名设置函数设置的。在回调中,位置指示正在单击哪一行。