Это один мой код фона TaskКак передать данные в списке Listview в корзину (новая активность), нажав кнопку. Мои данные, вставленные в ListView использует SQLite
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
или другого?