Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Mobile Forum > Android - ขอคำแนะนำในการเก็บค่าจาก check box ใน listview แล้วส่งค่าที่เก็บได้ไปอีกหน้าครับ ตอนนี้ทำได้แค่เอาข้อมูลจาก DB ออกมาโชว์ โค้ดทีทำอยู่ด้านใน



 

Android - ขอคำแนะนำในการเก็บค่าจาก check box ใน listview แล้วส่งค่าที่เก็บได้ไปอีกหน้าครับ ตอนนี้ทำได้แค่เอาข้อมูลจาก DB ออกมาโชว์ โค้ดทีทำอยู่ด้านใน

 



Topic : 117074



โพสกระทู้ ( 71 )
บทความ ( 0 )



สถานะออฟไลน์




listview ที่เอามาโชว์ผมดังข้อมูลมาจาก DB แล้วทีนี้ผมอยากจะให้ผู้ใช้คลิก check box ซึ่งเลือกได้มากกว่า1 แล้วเอาค่าที่ได้ส่งไปอีกหน้าเพื่อเอาไปใช้ต่อไปครับ ผมใช้ android studio เขียนแอพ ส่วน API คือ 19


proTypeList.java
public class PromotionTypeList extends Activity {

    private static final String TAG_PROMOTION_Type = "promotion_type";
    private static final String TAG_PROMOTION_Type_ID = "proTypeID";
    private static final String TAG_PROMOTION_Type_NAME = "proTypeName";

    JSONArray PromoTypeArray = null;

    public String getJson= null;

    ListView listView;
    SimpleAdapter adapter;

    Button btnGetType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.type_list);

        listView = (ListView) findViewById(R.id.listView4);

        ArrayList<HashMap<String, String>> promotionTypeList = new ArrayList<HashMap<String, String>>();

        getJson = getIntent().getStringExtra("PromotionType");
        Log.d("GetJson", getJson);

        try {
            PromoTypeArray = jsonObject.getJSONArray(TAG_PROMOTION_Type);

            if (PromoTypeArray.length() == 0) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.setTitle("Caution!");
                dialog.setIcon(R.drawable.ic_action_error);
                dialog.setCancelable(false);
                dialog.setMessage("No Promotion Type Data.");
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i = new Intent(getApplicationContext(), Find_Promotion.class);
                        startActivity(i);
                        finish();
                    }
                });
                dialog.show();
            } else {
                Log.d("SIZE ARRAY", String.valueOf(PromoTypeArray.length()));
                for (int i = 0; i < PromoTypeArray.length(); i++) {
                    JSONObject object = PromoTypeArray.getJSONObject(i);

                    String proType_id = object.getString(TAG_PROMOTION_Type_ID);
                    String proType_name = object.getString(TAG_PROMOTION_Type_NAME);

                    Log.d("proType_id", proType_id);
                    Log.d("proType_name", proType_name);

                    HashMap<String, String> proType = new HashMap<String, String>();

                    proType.put(TAG_PROMOTION_Type_ID, proType_id);
                    proType.put(TAG_PROMOTION_Type_NAME, proType_name);

                    promotionTypeList.add(proType);
                }
            }
        } catch (JSONException e) {
            Log.d("JSON PARSER", "Parsing Error");
        }

        adapter = new SimpleAdapter(this, promotionTypeList,
                R.layout.type_item,
                new String[]{TAG_PROMOTION_Type_NAME, TAG_PROMOTION_Type_ID}, new int[]{
                R.id.promotionTypeName, R.id.promotionTypeId }
        );

        listView.setAdapter(adapter);

        btnGetType = (Button) findViewById(R.id.protypeToFindPro);

    }

}



type_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="   OK   "
        android:id="@+id/protypeToFindPro"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView4"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="25dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You can select more than one"
        android:id="@+id/textView12"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:textSize="20dp"
        android:layout_marginLeft="5dp"/>
</RelativeLayout>



type_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/promotionTypeId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="0dp"
        />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Free"
        android:id="@+id/promotionTypeName"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:textSize="20dp"
        android:padding="15dp"
        android:layout_marginLeft="5dp"/>

</RelativeLayout>




Tag : Mobile, MySQL, Android, Mobile









ประวัติการแก้ไข
2015-06-11 13:39:22
2015-06-11 13:49:02
2015-06-12 13:33:06
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2015-06-11 13:37:37 By : funrun View : 1917 Reply : 4
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ผมได้แนะนำไปก่อนหน้านี้แล้วครับ ถ้าตอนนี้คุณอ่านรายการ Checkbox ที่เลือกได้แล้ว ให้ส่งไปในรูปแบบของ JSON หรือว่าจะต่อ String แบบง่าย ๆ เช่น Comma ขั้นก็ได้ครับ ที่เหลือก็เพียงใช้การ Intent ปกติ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-06-11 17:33:38 By : mr.win
 


 

No. 2



โพสกระทู้ ( 71 )
บทความ ( 0 )



สถานะออฟไลน์


ผมยังอ่านรายการไม่ได้ครับ ที่ทำได้คือเอาข้อมูลใน DB ออกมาโชว์เท่านั้น ตอนนี้ยังเก็บค่าจาก checkbox ไม่ได้เลยครับ รบกวนช่วยแนะนำหน่อยนะครับว่าผมต้องแก้ตรงไหนถึงจะ get value จาก checkbox มาได้
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-06-12 13:31:22 By : funrun
 

 

No. 3



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook



Android ListView and Checkbox

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-06-12 13:33:40 By : mr.win
 


 

No. 4



โพสกระทู้ ( 71 )
บทความ ( 0 )



สถานะออฟไลน์


ผมลองทำตามดูแล้วมันฟ้อง null ที่ CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.promotionTypeChk); ผมทำอะไรผิดตรงไหนช่วยแนะนำด้วย

Code Java
public class PromotionTypeList extends Activity {

    private static final String TAG_PROMOTION_Type = "promotion_type";
    private static final String TAG_PROMOTION_Type_ID = "proTypeID";
    private static final String TAG_PROMOTION_Type_NAME = "proTypeName";

    JSONArray PromoTypeArray = null;

    public String getJson= null;

    ListView listView;
    SimpleAdapter adapter;

    Button btnGetType;

    ArrayList<HashMap<String, String>> promotionTypeList = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.promotion_type_list);

        ActionBar actionBar = getActionBar();
        actionBar.setTitle("Promotion Type");

        listView = (ListView) findViewById(R.id.listView4);

        getJson = getIntent().getStringExtra("PromotionType");
        Log.d("GetJson", getJson);

        JSONObject jsonObject = null;

        try {
            PromoTypeArray = jsonObject.getJSONArray(TAG_PROMOTION_Type);

            if (PromoTypeArray.length() == 0) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.setTitle("Caution!");
                dialog.setIcon(R.drawable.ic_action_error);
                dialog.setCancelable(false);
                dialog.setMessage("No Promotion Type Data.");
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i = new Intent(getApplicationContext(), Find_Promotion.class);
                        startActivity(i);
                        finish();
                    }
                });
                dialog.show();
            } else {
                Log.d("SIZE ARRAY", String.valueOf(PromoTypeArray.length()));
                for (int i = 0; i < PromoTypeArray.length(); i++) {
                    JSONObject object = PromoTypeArray.getJSONObject(i);

                    String proType_id = object.getString(TAG_PROMOTION_Type_ID);
                    String proType_name = object.getString(TAG_PROMOTION_Type_NAME);

                    Log.d("proType_id", proType_id);
                    Log.d("proType_name", proType_name);

                    HashMap<String, String> proType = new HashMap<String, String>();

                    proType.put(TAG_PROMOTION_Type_ID, proType_id);
                    proType.put(TAG_PROMOTION_Type_NAME, proType_name);

                    promotionTypeList.add(proType);
                }

            }
        } catch (JSONException e) {
            Log.d("JSON PARSER", "Parsing Error");
        }

        listView.setAdapter(new CountryAdapter(this));

        btnGetType = (Button) findViewById(R.id.protypeToFindPro);
        btnGetType.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int countArray = listView.getAdapter().getCount();
                Log.d("count", String.valueOf(countArray));

                for (int j = 0; j < countArray; j++) {
                    RelativeLayout itemLayout = (RelativeLayout)listView.getChildAt(j);
                    CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.promotionTypeChk);
                    if(checkbox.isChecked())
                    {
                        Log.d("Item "+String.valueOf(j), checkbox.getTag().toString());

                        Toast.makeText(PromotionTypeList.this,checkbox.getTag().toString() ,Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
    }

    public class CountryAdapter extends BaseAdapter {
        private Context context;

        public CountryAdapter(Context c) {
            //super( c, R.layout.activity_column, R.id.rowTextView, );
            // TODO Auto-generated method stub
            context = c;
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return promotionTypeList.size();
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.promotion_type_item, null);

            }

            // Type ID
            TextView txtID = (TextView) convertView.findViewById(R.id.promotionTypeId);
            txtID.setText(promotionTypeList.get(position).get("proTypeID") + ".");

            // Type Name
            TextView txtCode = (TextView) convertView.findViewById(R.id.promotionTypeName);
            txtCode.setText(promotionTypeList.get(position).get("proTypeName"));

            // Type Check
            CheckBox Chk = (CheckBox) convertView.findViewById(R.id.promotionTypeChk);
            Chk.setTag(promotionTypeList.get(position).get("proTypeID"));

            return convertView;

        }
    }

    public void onBackPressed(){
        Intent intent = new Intent(getApplicationContext(), Find_Promotion.class);
        startActivity(intent);
    }
}


list
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffffff">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="   OK   "
        android:id="@+id/protypeToFindPro"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView4"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="25dp"
        android:background="#ffffffff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You can select more than one"
        android:id="@+id/textView12"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:textSize="20dp"
        android:layout_marginLeft="5dp"/>
</RelativeLayout>


Item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/promotionTypeId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="0dp"
        />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/promotionTypeChk"
        android:textColor="#ffffffff"
        android:textSize="20dp"
        android:padding="15dp"
        android:layout_marginLeft="5dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Free"
        android:id="@+id/promotionTypeName"
        android:textSize="20dp"
        android:padding="15dp"
        android:layout_marginLeft="5dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-06-12 14:28:28 By : funrun
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Android - ขอคำแนะนำในการเก็บค่าจาก check box ใน listview แล้วส่งค่าที่เก็บได้ไปอีกหน้าครับ ตอนนี้ทำได้แค่เอาข้อมูลจาก DB ออกมาโชว์ โค้ดทีทำอยู่ด้านใน
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่