如何通过单击按钮将Listview中的数据传递到购物车列表(新活动)。我插入listview的数据是使用SQLite

时间:2016-01-18 13:58:15

标签: java android sqlite listview android-listview

这是我的后台任务代码

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
public class BackgroundTask extends AsyncTask<String,Product,String> {
    Context ctx;
    ProductAdapter productAdapter;
    Activity activity;
    ListView listView;

    BackgroundTask(Context ctx) {
        this.ctx = ctx;
        activity = (Activity) ctx;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {

        String method = params[0];
        DbOperation dbOperation = new DbOperation(ctx);

        if (method.equals("add_info")) {
            String Vendor = params[1];
            String Product = params[2];
            String Pprice = params[3];
            String Cprice = params[4];
            SQLiteDatabase db = dbOperation.getWritableDatabase();
            dbOperation.addInformation(db, Vendor, Product, Pprice, Cprice);
            return "One Row Inserted...";


        } else if (method.equals("get_info")) {
            listView = (ListView) activity.findViewById(R.id.display_listview);
            SQLiteDatabase db = dbOperation.getReadableDatabase();
            Cursor cursor = dbOperation.getInformation(db);
            productAdapter = new ProductAdapter(ctx, R.layout.display_product_row);
            String vendor, product,pprice, cprice;

            while (cursor.moveToNext()) {
                vendor = cursor.getString(cursor.getColumnIndex(ProductContract.ProductEntry.VENDOR));
                product = cursor.getString(cursor.getColumnIndex(ProductContract.ProductEntry.PRODUCT));
                pprice = cursor.getString(cursor.getColumnIndex(ProductContract.ProductEntry.PPRICE));
                cprice = cursor.getString(cursor.getColumnIndex(ProductContract.ProductEntry.CPRICE));
                Product product1 = new Product(vendor, product, pprice, cprice);
                publishProgress(product1);
            }
            return "get_info";

        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Product... values) {

        productAdapter.add(values[0]);

    }

    @Override
    protected void onPostExecute(String result) {

        if (result.equals("get_info")) {
            listView.setAdapter(productAdapter);

        } else {
            Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
        }
    }
}

这是我的DbOperation.java类

public class DbOperation extends SQLiteOpenHelper{

    private static final int DB_VERSION= 1;
    private static final String DB_NAME="product_info.db";
    private static final String CREATE_QUERY="create table " +ProductContract.ProductEntry.TABLE_NAME+
            "("+ ProductContract.ProductEntry.VENDOR+ " text," + ProductContract.ProductEntry.PRODUCT+ " text," +
            ProductContract.ProductEntry.PPRICE+ " text," + ProductContract.ProductEntry.CPRICE+ " text);";
    DbOperation(Context ctx)
    {
        super(ctx,DB_NAME,null,DB_VERSION);
        Log.d("Database operation","Database created...");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_QUERY);
        Log.d("Database operation", "Table created...");


    }

    public void addInformation(SQLiteDatabase db,String vendor,String product,String pprice,String cprice)
    {
        ContentValues contentValues=new ContentValues();
        contentValues.put(ProductContract.ProductEntry.VENDOR,vendor);
        contentValues.put(ProductContract.ProductEntry.PRODUCT,product);
        contentValues.put(ProductContract.ProductEntry.PPRICE,pprice);
        contentValues.put(ProductContract.ProductEntry.CPRICE,cprice);
        db.insert(ProductContract.ProductEntry.TABLE_NAME, null, contentValues);
        Log.d("Database operation", "One Row Inserted...");

    }

    public Cursor getInformation(SQLiteDatabase db)
    {
        String[] projections={ProductContract.ProductEntry.VENDOR,ProductContract.ProductEntry.PRODUCT,ProductContract.ProductEntry.PPRICE,ProductContract.ProductEntry.CPRICE};

        Cursor cursor=db.query(ProductContract.ProductEntry.TABLE_NAME,projections,
                null,null,null,null,null);

        return cursor;
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    }
}

这是我的DisplayProduct.java类

public class DisplayProduct extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_product_layout);
        BackgroundTask backgroundTask=new BackgroundTask(this);
        backgroundTask.execute("get_info");

    }
}

这是我的ProductAdapter.java类

public class ProductAdapter extends ArrayAdapter {
    List list=new ArrayList();

    public ProductAdapter(Context context, int resource) {
        super(context, resource);
    }


    public void add(Product object){
        list.add(object);
        super.add(object);


    }
    @Override
    public int getCount(){
        return list.size();

    }

    @Override
    public Object getItem(int position){
        return list.get(position);
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent){
        View row=convertView;
        ProductHolder productHolder;
        if(row==null)
        {
            LayoutInflater layoutInflater= (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row=layoutInflater.inflate(R.layout.display_product_row,parent,false);
            productHolder=new ProductHolder();
            productHolder.tx_vendor=(TextView)row.findViewById(R.id.t_vendor);
            productHolder.tx_product=(TextView)row.findViewById(R.id.t_product);
            productHolder.tx_pprice=(TextView)row.findViewById(R.id.t_pprice);
            productHolder.tx_cprice=(TextView)row.findViewById(R.id.t_cprice);
            row.setTag(productHolder);

        }
        else
        {
            productHolder=(ProductHolder)row.getTag();
        }
        Product product=(Product)getItem(position);
        productHolder.tx_vendor.setText(product.getVendor().toString());
        productHolder.tx_product.setText(product.getProduct().toString());
        productHolder.tx_pprice.setText(product.getPprice().toString());
        productHolder.tx_cprice.setText(product.getCprice().toString());
        return row;    
    }

    static class ProductHolder
    {
        TextView tx_vendor,tx_product,tx_pprice,tx_cprice;

    }

}

最后一个是我的ProductContract.java类

public final class ProductContract {

    ProductContract(){}

        public static  abstract class ProductEntry
        {
               public static final String VENDOR="vendor";
                public static final String PRODUCT="product";
                public static final String PPRICE="pprice";
                public static final String CPRICE="cprice";
            public static final String TABLE_NAME ="product_table";
        }
}

如何将数据从listview传递到cartlist?是使用i.putextra函数还是其他?

1 个答案:

答案 0 :(得分:0)

使用Intent通过putExtra()方法在两个活动之间传递数据,它已经从原始数据类型重载到Array

Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
intent.putExtra("Key1", "Hello");
intent.putExtra("Key2", 2016);
startActivity(intent);

并在NewActivity内{例如onCreate()

Intent intent = getIntent();
String value1 = intent.getStringExtra("key1");
// zero here is default value that will be returned if no value found to "key2" key
int value2 = intente.getIntExtra("key2", 0);

您也可以将ArrayList放入意图中,查看documentation以获取更多信息。