1

问题:在我检查CheckBoxes了自定义中显示的多个后,无论我所做的选择如何ListView,我仍然会返回自定义中显示的整行数据值。ListView我猜CheckBox' 的状态在我检查后根本没有改变。CheckBox那么在我检查多个之后如何设置状态(真) CheckBoxes?此外,当我点击提交时,如何获取选中的CheckBoxes位置?那么,如何调用的类来检索特定的数据行,包括和(供用户输入)以显示在另一个自定义列表中以供检查?ListViewButtonModelTextViewEditTextCheckBoxes

摘要:我想仅在另一页上显示带有选中复选框的值行数组。

测试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;     
    }
}
4

1 回答 1

0

你的代码有很多问题:

  • 不要使用AbsolutLayout,该类已被弃用,因为您不能使用它来制作在 Android 设备所具有的所有屏幕上都可以很好缩放的布局。
  • 不要将可滚动的小部件(如 a ListView)放在其他可滚动的小部件(如 a ScrollView)中,这不会很好。
  • 不要在主线程上做一些可能需要很长时间才能完成的事情,因为你可能会得到一个“不稳定的”应用程序,或者更糟糕的是可怕的 ANR 对话框。此操作包括任何网络操作、数据库访问等。使用线程或AsyncTasks从 Internet 获取数据。
  • 我不确定你在做什么,ActivityManager但我有一种不对劲的感觉。只需将Activity'startActivity方法与Intent.
  • getCheckedItemPositions使用 SDK 中的默认ArrayAdapter布局和其中一种布局。如果您使用自定义行布局制作自己的适配器,那么您必须自己管理CheckBox状态(并设置一种检索该状态的方法)。这部分应该很容易,关于如何做到这一点有数以千计的问题和博客。
  • 您的适配器的getView方法不正确。每次调用getView方法时,应在两个视图上设置标签(因此应在该if (convertView == null)块之外设置标签)。我也不认为那些听众会很好地工作。
  • 即便如此,您的代码无论如何也不会工作,因为您设置了Intent所需的数据,但您实际上并没有使用它Intent来启动新的Activity ,因此您不会将任何信息传递给新开始的Activity

这应该让你朝着正确的方向前进。

于 2012-11-17T10:22:56.977 回答