如何在Android中从listview添加数据?

时间:2013-11-06 11:10:25

标签: java android

我创建了2个活动A和B.我有listview填充了     活动'A'和Spinner中SQLite数据库的List视图中的数据     在活动'B'中。         我想将listview中的数据添加到Activity'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();
    }



}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

此代码是如何为spinner ....设置数据的示例,方法是将list传递给adapter以设置微调器数据。

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

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;
}

}