Android - ขอคำแนะนำในการเก็บค่าจาก check box ใน listview แล้วส่งค่าที่เก็บได้ไปอีกหน้าครับ ตอนนี้ทำได้แค่เอาข้อมูลจาก DB ออกมาโชว์ โค้ดทีทำอยู่ด้านใน
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
Date :
2015-06-11 13:37:37
By :
funrun
View :
1982
Reply :
4
ผมได้แนะนำไปก่อนหน้านี้แล้วครับ ถ้าตอนนี้คุณอ่านรายการ Checkbox ที่เลือกได้แล้ว ให้ส่งไปในรูปแบบของ JSON หรือว่าจะต่อ String แบบง่าย ๆ เช่น Comma ขั้นก็ได้ครับ ที่เหลือก็เพียงใช้การ Intent ปกติ
Date :
2015-06-11 17:33:38
By :
mr.win
ผมยังอ่านรายการไม่ได้ครับ ที่ทำได้คือเอาข้อมูลใน DB ออกมาโชว์เท่านั้น ตอนนี้ยังเก็บค่าจาก checkbox ไม่ได้เลยครับ รบกวนช่วยแนะนำหน่อยนะครับว่าผมต้องแก้ตรงไหนถึงจะ get value จาก checkbox มาได้
Date :
2015-06-12 13:31:22
By :
funrun
Android ListView and Checkbox
Date :
2015-06-12 13:33:40
By :
mr.win
ผมลองทำตามดูแล้วมันฟ้อง 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
Load balance : Server 05