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

Posted

技术标签:

【中文标题】如何通过单击按钮将 Listview 中的数据传递到购物车列表(新活动)。我插入到 listview 的数据正在使用 SQLite【英文标题】:How to pass the data in Listview to Cart list (new activity) by click on button . My data inserted to listview is using SQLite 【发布时间】:2016-04-23 17:29:13 【问题描述】:

这是我的后台任务代码

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】:

使用Intent 通过putExtra() 方法在两个活动之间传递数据,它被重载到从原始数据类型到Arrays 的许多东西

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 放入intent,查看documentation 了解更多信息。

【讨论】:

以上是关于如何通过单击按钮将 Listview 中的数据传递到购物车列表(新活动)。我插入到 listview 的数据正在使用 SQLite的主要内容,如果未能解决你的问题,请参考以下文章

如何通过php中的按钮单击将AJAX信息传递给另一个用户?

如何通过单击 TableCell 中的按钮将我的 TableCell 中的标签值传递给 TableViewController? [关闭]

如何获取listView位置值并通过intent传递给另一个类

Flutter 如何遍历 ListView 中的每个项目?

如何通过单击 PyQt 中的按钮将参数传递给函数?

如何在单击 ListView 中的按钮时取消选中所有列表项