Android 如何在资产数据库文件的listview项中设置OnClickListener

Android 如何在资产数据库文件的listview项中设置OnClickListener,android,listview,android-fragments,Android,Listview,Android Fragments,我想问一下你的情况。我在listview中调用了外部数据库文件[assets folder]。我想在listview中的每个项目上设置onClicklistener。当listview项目单击时,希望显示一个带有数据字段的小片段。我尝试了许多源代码,但仍然无法工作 下面是我们想要做的示例片段。 下面是数据库结构。 下面是DatabaseHelper.java package com.example.arlequina.sqlitefromassetexample.database; imp

我想问一下你的情况。我在listview中调用了外部数据库文件[assets folder]。我想在listview中的每个项目上设置onClicklistener。当listview项目单击时,希望显示一个带有数据字段的小片段。我尝试了许多源代码,但仍然无法工作

下面是我们想要做的示例片段。

下面是数据库结构。

下面是DatabaseHelper.java

package com.example.arlequina.sqlitefromassetexample.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.arlequina.sqlitefromassetexample.model.Product;

import java.util.ArrayList;
import java.util.List;
import java.sql.Blob;

/**
 * Created by ARLEQUINA on 2/10/2017.
 */

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String dbname = "sample.db";
    public static final String dblocation = "/data/data/com.example.arlequina.sqlitefromassetexample/databases/";
    private Context mContext;
    private SQLiteDatabase mDatabase;

    public DatabaseHelper(Context context){
        super(context, dbname, null, 1);
        this.mContext = context;
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
    public void openDatabase(){
        String dbPath = mContext.getDatabasePath(dbname).getPath();
        if(mDatabase != null && mDatabase.isOpen()){
            return;
        }
        mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
    }
    public void closeDatabase(){
        if(mDatabase != null){
            mDatabase.close();
        }
    }
    public List<Product> getListProduct(){
        Product product = null;
        List<Product> productList = new ArrayList<>();
        openDatabase();
        Cursor cursor = mDatabase.rawQuery(" SELECT * FROM Product ", null);
        cursor.moveToFirst();
        while(!cursor.isAfterLast()){
            product = new Product(cursor.getInt(0), cursor.getString(1), cursor.getString(2),cursor.getString(3));
            productList.add(product);
            cursor.moveToNext();
        }
        cursor.close();
        closeDatabase();
        return productList;
    }
}

您可以使用
listview.setOnItemClickListener
v.setOnClickListener
来解决您的问题。这是最基本的,伙计。

因为您的问题是使列表项可单击,所以您可以在活动中初始化listview之后使用此选项

lvProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
});
lvProduct.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
}
});
position
是列表中单击项目的位置。因此,您可以使用
mpProductList.get(position)

在您可以获得该物品后,现在是展示您的物品的时候了。您可以使用
自定义对话框
使其与演示类似(当然,您必须再创建一个xml):


检查此官方链接中的“自定义”对话框:

您的问题可能不清楚。关于
setOnClickListener
读取数据库的问题?是的,关于setOnClickListener,我是as的新成员,所以如何在片段中显示数据?我需要创建新的xml文件吗?当然,首先创建布局文件,然后在片段中覆盖
onCreateView
,以扩大布局。非常感谢兄弟。:)
package com.example.arlequina.sqlitefromassetexample;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.arlequina.sqlitefromassetexample.adapter.ListProductAdapter;
import com.example.arlequina.sqlitefromassetexample.database.DatabaseHelper;
import com.example.arlequina.sqlitefromassetexample.model.Product;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by ARLEQUINA on 2/10/2017.
 */

public class MainActivity extends Activity {
    private ListView lvProduct;
    private ListProductAdapter adapter;
    private List<Product> mProductList;
    private DatabaseHelper mDBHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        lvProduct = (ListView)findViewById(R.id.listview_product);
        mDBHelper = new DatabaseHelper(this);
        //Check exists database
        File database = getApplicationContext().getDatabasePath(DatabaseHelper.dbname);
        if(false == database.exists()){
            mDBHelper.getReadableDatabase();

            //Copy db
            if(copyDatabase(this)){
                Toast.makeText(this,"Copy database success", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "Copy data error", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        //Get product list in db when db exists
        mProductList = mDBHelper.getListProduct();
        //Init adapter
        adapter  = new ListProductAdapter(this,mProductList);
        //Set adapter for listview
        lvProduct.setAdapter(adapter);




    }
    private boolean copyDatabase(Context context){
        try{
            InputStream inputStream =  context.getAssets().open(DatabaseHelper.dbname);
            String outFileName = DatabaseHelper.dblocation + DatabaseHelper.dbname;
            OutputStream outputStream = new FileOutputStream(outFileName);
            byte[] buff = new byte[1024];
            int length = 0;
            while((length = inputStream.read(buff)) > 0){
                outputStream.write(buff, 0, length);
            }
            outputStream.flush();
            outputStream.close();
            Log.v("MainActivity", "DB copied");
            return true;
        } catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
<?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"
    android:background="#ffffff"
    android:paddingBottom="5dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_product_name"
        android:text = "Name"
        android:textColor="#4bb6d6"
        android:textSize="20dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id = "@+id/tv_product_price"
        android:text="100$"
        android:textColor="#b30000"
        android:textSize="18dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id = "@+id/tv_product_desc"
        android:text="Description"
        android:textSize="16dp"
        android:textStyle="italic"/>
</LinearLayout>
<?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"
    android:background="#d1d1d1">

    <ListView
        android:id = "@+id/listview_product"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:divider="#d1d1d1"
        android:dividerHeight="10dp"></ListView>
</LinearLayout>
package com.example.arlequina.sqlitefromassetexample.model;

import java.sql.Blob;

/**
 * Created by ARLEQUINA on 2/10/2017.
 */

public class Product {

    private int id;
    private String name;
    private String price;
    private String desc;
    //private Blob img;

    public Product(int id, String name, String price, String desc){
        this.id = id;
        this.name = name;
        this.price = price;
        this.desc = desc;
        //this.img = img;
    }

    public int getId(){
        return id;
    }
    public void setId(int id){
        this.id = id;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getPrice(){
        return price;
    }
    public void setPrice(String price){
        this.price = price;
    }
    public String getDesc(){
        return desc;
    }
    public void setDesc(String desc){
        this.desc = desc;
    }

}
lvProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
});