gpt4 book ai didi

java - 如何通过单击按钮将 ListView 中的数据传递到购物车列表(新 Activity )。我插入 ListView 的数据使用 SQLite

转载 作者:太空宇宙 更新时间:2023-11-04 13:01:58 25 4
gpt4 key购买 nike

这是我的后台任务代码

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 函数还是其他函数?

最佳答案

使用Intent通过putExtra()方法在2个 Activity 之间传递数据,它重载了从原始数据类型到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放入 Intent 中,检查documentation了解更多信息。

关于java - 如何通过单击按钮将 ListView 中的数据传递到购物车列表(新 Activity )。我插入 ListView 的数据使用 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34856499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com