我正在尝试将列表视图中的项目添加到微调器中。我的第一个活动包含列表视图。我每次都使用按钮,在我的活动 ListView 中使用 SQLite 数据库插入一个新的行条目,并且每一行都正确添加到列表视图中。另一个我在第二个活动中有微调器。
我想在第二个活动中将每个 list_view 项目添加到微调器中。
这是我的产品列表适配器类:
public class Product_List_Adapter extends BaseAdapter
{
@SuppressWarnings("unused")
private Context mContext;
private ArrayList<String> Productid_ArrayList;
private ArrayList<String> ProductName_ArrayList;
private ArrayList<String> ProductDescription_ArrayList;
public Product_List_Adapter(Context mContext,
ArrayList<String> productid_ArrayList,
ArrayList<String> productName_ArrayList,
ArrayList<String> productDescription_ArrayList)
{
super();
this.mContext = mContext;
Productid_ArrayList = productid_ArrayList;
ProductName_ArrayList = productName_ArrayList;
ProductDescription_ArrayList = productDescription_ArrayList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Productid_ArrayList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int pos, View child, ViewGroup parent) {
// TODO Auto-generated method stub
Holder mHolder;
LayoutInflater layoutInflater;
if(child == null)
{
layoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater .inflate(R.layout.define_products_listrow, null);
mHolder = new Holder();
mHolder.txt_product_id = (TextView)child.findViewById(R.id.txt_ProductId);
mHolder.txt_product_name = (TextView)child.findViewById(R.id.txt_ProductName);
mHolder.txt_product_description = (TextView)child.findViewById(R.id.txt_ProductDescr);
child.setTag(mHolder);
}
else
{
mHolder = (Holder) child.getTag();
}
mHolder.txt_product_id.setText(Productid_ArrayList.get(pos));
mHolder.txt_product_name.setText(ProductName_ArrayList.get(pos));
mHolder.txt_product_description.setText(ProductDescription_ArrayList.get(pos));
return child;
}
public class Holder {
TextView txt_product_id;
TextView txt_product_name;
TextView txt_product_description;
}
}
The ProductDefine Sctivity which is contain Listview
public class DefineProducts_Activity6 extends Activity {
String log;
List<String> list_Dataset;
String[] str_Splitup1;
String[] str_Splitup2;
String[] str_Splitup3;
Product_List_Adapter disadpt;
//public ArrayAdapter<String> adapter;
private com.db_mgmt.DbHelper mHelper;
private SQLiteDatabase dataBase;
private static ArrayList<String> products_Id_ArrayList = new ArrayList<String>();
private static ArrayList<String> products_Name_ArrayList = new ArrayList<String>();
private static ArrayList<String> products_Details_ArrayList = new ArrayList<String>();
private ListView products_List;
private AlertDialog.Builder build;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.define_products_listview);
products_List = (ListView) findViewById(R.id.products_List);
mHelper = new DbHelper(this);
//add new record
findViewById(R.id.btnAdd_DefineProduct).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
Add_Define_Product.class);
i.putExtra("update", false);
startActivity(i);
}
});
//click to update data
products_List.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i = new Intent(getApplicationContext(),
Add_Define_Product.class);
i.putExtra("productsName", products_Name_ArrayList.get(arg2));
i.putExtra("productsDetails", products_Details_ArrayList.get(arg2));
i.putExtra("productsID", products_Id_ArrayList.get(arg2));
i.putExtra("update", true);
startActivity(i);
}
});
//long click to delete data
products_List.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
build = new AlertDialog.Builder(DefineProducts_Activity6.this);
build.setTitle("Delete " + products_Name_ArrayList.get(arg2) + " "
+ products_Details_ArrayList.get(arg2));
build.setMessage("Do you want to delete ?");
build.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(
getApplicationContext(),
products_Name_ArrayList.get(arg2) + " "
+ products_Details_ArrayList.get(arg2)
+ " is deleted.", Toast.LENGTH_LONG).show();
dataBase.delete(
DbHelper.TABLE_DEFINE_PRODUCT_NAME,
DbHelper.KEY_ID + "="
+ products_Id_ArrayList.get(arg2), null);
displayData();
dialog.cancel();
}
});
build.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
AlertDialog alert = build.create();
alert.show();
return true;
}
});
}
@Override
protected void onResume() {
displayData();
super.onResume();
}
/**
* displays data from SQLite
*/
private void displayData() {
dataBase = mHelper.getWritableDatabase();
Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
+ DbHelper.TABLE_DEFINE_PRODUCT_NAME, null);
products_Id_ArrayList.clear();
products_Name_ArrayList.clear();
products_Details_ArrayList.clear();
if (mCursor.moveToFirst())
{
do
{
products_Id_ArrayList.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
products_Name_ArrayList.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_PRODUCTS_NAME)));
products_Details_ArrayList.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_PRODUCTS_DETAILS)));
} while (mCursor.moveToNext());
}
SharedPreferences spdata =
PreferenceManager.getDefaultSharedPreferences(this);
String strDataSet1 = spdata.getString("LISTS_ID",",");
String strDataSet2 = spdata.getString("LISTS_NAME", "");
String strDataSet3 = spdata.getString("LISTS_Detail", "");
Log.e(log,strDataSet1);
Log.e(log,strDataSet2);
Log.e(log,strDataSet3);
list_Dataset = Arrays.asList(strDataSet1.split(","));
list_Dataset = Arrays.asList(strDataSet3.split(","));
list_Dataset = Arrays.asList(strDataSet2.split(","));
str_Splitup1 = strDataSet1.split(",");
str_Splitup2 = strDataSet2.split(",");
str_Splitup3 = strDataSet3.split(",");
List<String> items1 = Arrays.asList(strDataSet1.split(","));
List<String> items2 = Arrays.asList(strDataSet2.split(","));
List<String> items3 = Arrays.asList(strDataSet3.split(","));
ArrayList<String> itemsarraylist = new ArrayList<String>();
for (int j = 0; j < items2.size(); j++)
{
itemsarraylist.add(j, items2.get(j));
}
disadpt = new Product_List_Adapter(DefineProducts_Activity6.this,products_Id_ArrayList, products_Name_ArrayList, products_Details_ArrayList);
products_List.setAdapter(disadpt);
disadpt.notifyDataSetChanged();
mCursor.close();
}
}