问题:在我检查CheckBoxes
了自定义中显示的多个后,无论我所做的选择如何ListView
,我仍然会返回自定义中显示的整行数据值。ListView
我猜CheckBox
' 的状态在我检查后根本没有改变。CheckBox
那么在我检查多个之后如何设置状态(真) CheckBoxes
?此外,当我点击提交时,如何获取选中的CheckBoxes
位置?那么,如何调用的类来检索特定的数据行,包括和(供用户输入)以显示在另一个自定义列表中以供检查?ListView
Button
Model
TextView
EditText
CheckBoxes
摘要:我想仅在另一页上显示带有选中复选框的值行数组。
测试1.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="310dp"
android:layout_x="0dp"
android:layout_y="-4dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="293dp" >
</ListView>
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="113dp"
android:layout_y="346dp"
android:text="Button" />
</AbsoluteLayout>
测试.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="208dp"
android:layout_y="4dp"
android:text="CheckBox" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="11dp"
android:layout_y="15dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="22dp"
android:layout_y="49dp"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="42dp"
android:layout_y="89dp"
android:text="TextView" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="35dp"
android:layout_y="123dp"
android:text="TextView" />
<EditText
android:id="@+id/editText1"
android:layout_width="42dp"
android:layout_height="wrap_content"
android:layout_x="147dp"
android:layout_y="13dp"
android:ems="10"
android:inputType="number" />
</AbsoluteLayout>
MultipleSet1.java 类:
public class MultipleSet1 extends ListActivity
{
List<Model> list = new ArrayList<Model>();
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.testing1, null);
this.setContentView(viewToLoad);
//final ViewHolder viewHolder = new ViewHolder();
String temp = "Set_A";
String response = null;
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("Name", temp));
try
{
response = CustomHttpClient.executeHttpPost("PhPScripts", postParameters);
}
catch (Exception e)
{
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Fail to connect to server7777777777.", 3).show();
}
int size = 0;
try
{
JSONArray jArray = new JSONArray(response);
size = jArray.length();
for(int i =0; i<size; i++)
{
JSONObject json_data = jArray.getJSONObject(i);
list.add(get(json_data.getString("description"), json_data.getString("price"), false));
}
}
catch (JSONException e)
{
Toast.makeText(getApplicationContext(), "Fail to connect to server7777777777.", 3).show();
}
ArrayAdapter<Model> adapter = new InteractiveListViewAdapter(this, list);
setListAdapter(adapter);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
//@Override
public void onClick(View v) {
ListView list = (ListView)findViewById(android.R.id.list);
list = getListView();
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
int size = list.getCount();
String checked = "";
String unchecked = "";
SparseBooleanArray sparseBooleanArray = list.getCheckedItemPositions();
for(int i = 0; i < size; i++)
{
if(sparseBooleanArray.get(i) == true)
{
checked += list.getItemAtPosition(i).toString() + "\n";
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked+= list.getItemAtPosition(i).toString() + "\n";
}
}
Intent intent = new Intent(getApplicationContext(), SingleSet.class);
intent.putExtra("TAG_DESCRIPTION", unchecked);
// Create the view using TabTour's LocalActivityManager
View view1 = bookingTab.group.getLocalActivityManager().startActivity("BookingTab", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Again, replace the view
bookingTab.group.replaceView(view1);
}
});
}
private Model get(String name, String score, boolean selected)
{
return new Model (name, score, selected);
}
}
InteractiveListViewAdapter.java 类:
public class InteractiveListViewAdapter extends ArrayAdapter<Model>
{
private final List<Model> list;
private final Activity context;
public InteractiveListViewAdapter(Activity context, List<Model> list)
{
super(context, R.layout.testing, list);
this.context = context;
this.list = list;
}
static class ViewHolder
{
protected TextView text;
protected CheckBox checkbox;
protected EditText scores;
}
//@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = null;
if (convertView == null)
{
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.testing, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.textView1);
viewHolder.scores=(EditText) view.findViewById(R.id.editText1);
viewHolder.scores.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count)
{
Model element=(Model)viewHolder.scores.getTag();
element.setScore(s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count,int after)
{
}
public void afterTextChanged(Editable s)
{
}
});
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkBox1);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
viewHolder.checkbox.setTag(list.get(position));
viewHolder.scores.setTag(list.get(position));
view.setTag(viewHolder);
}
else
{
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
((ViewHolder) view.getTag()).scores.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.scores.setText(list.get(position).getScore());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
Model.java 类:
public class Model
{
private String name;
private String score;
private boolean selected;
public Model(String name, String score, boolean selected)
{
this.name = name;
this.score = score;
this.selected = selected;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public boolean isSelected()
{
return selected;
}
public void setSelected(boolean selected)
{
this.selected = selected;
}
public String getScore()
{
return score;
}
public void setScore(String score)
{
this.score = score;
}
}