0

我创建了 2 个活动 A 和 B。我有 listview,它从活动“A”中的 SQLite 数据库和活动“B”中的 Spinner 填充列表视图中的数据。我想将列表视图中的数据添加到活动“B”中的微调器中。我该怎么做。请给我任何舔。谢谢高级。

    public class DefineProducts_Activity6 extends Activity {

    private com.db_mgmt.DbHelper mHelper;
    private SQLiteDatabase dataBase;

    private ArrayList<String> products_Id_ArrayList = new ArrayList<String>();
    private ArrayList<String> products_Name_ArrayList = new ArrayList<String>();
    private 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);

            }
        });


    @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());
        }
        Product_List_Adapter disadpt = new Product_List_Adapter(DefineProducts_Activity6.this,products_Id_ArrayList, products_Name_ArrayList, products_Details_ArrayList);
        products_List.setAdapter(disadpt);
        mCursor.close();
    }



}
4

2 回答 2

0

此代码是如何为微调器设置数据的示例......通过将列表传递给适配器以设置微调器数据。

现在你必须根据你的要求进行操作。据我了解,您必须在其他活动“B”中设置微调器,因此您必须将列表从活动“A”传递给活动“B”(通过 Intent,将列表作为统计信息或任何其他方式),然后传递给适配器设置微调器的数据。就像你在下面的行中所做的那样......

Product_List_Adapter disadpt = new
Product_List_Adapter(DefineProducts_Activity6.this,products_Id_ArrayList,    products_Name_ArrayList, products_Details_ArrayList);

希望你明白我想说什么......如果发生任何问题......然后恢复......

import java.util.ArrayList;
import com.example.dip.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyAdapter extends ArrayAdapter<String> {

ArrayList list, list1;

// Constructor for getting details from calling method/class
public MyAdapter(Context context, int textViewResourceId, ArrayList list3, ArrayList list4) {

    super(context, textViewResourceId, list3);
    list = list3;
    list1 = list4;
}

public View getDropDownView(int position, View convertView,ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

public View getCustomView(int position, View convertView, ViewGroup parent) { 

    // Setting the layout
    LayoutInflater inflater=(LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row=inflater.inflate(R.layout.networkrow, parent, false);

    TextView network=(TextView)row.findViewById(R.id.net); 
    TextView id=(TextView)row.findViewById(R.id.pid);  // Setting id of network

    network.setText((CharSequence) list1.get(position));
    id.setText((CharSequence) list.get(position));  // Setting name of network

    return row;
}

}
于 2013-11-06T11:39:26.030 回答
0

通过 putextra 将 arraylists 发送到第二个活动,并在第二个活动中创建另一个 arrayadapter 并设置 spinner.setApdater(yourAdapter)

于 2013-11-06T11:18:30.567 回答