2014-04-13 64 views
0

对我的应用程序,我需要一个checkboxex列表,其中点击文本会导致新的意图,但点击复选框会导致保存复选框的值。我不知道如果我的问题是清楚,所以这是我的例子:listview with checkbox android

我有一个列表( '_' 是复选框):

  • _值a
  • _ VALUEB
  • _ valueC

让我们点击复选框的旁边放A和C:

  • X值a
  • _ VALUEB
  • X valueC

让我们点击 '值a' 的TextView。现在,新的意图应该开始,矢量[valueA,valueC]应该被保存并发送到下一个意图。

对于测试,我创建了一个列表,其中每个项目都是带有textView和复选框的linearView。我创建了两个方法:onClick_textView和onClick_checkBox。在这个方法和'onListItemClick'中,我只是显示吐司来测试哪一个被执行。唯一一个在onClick_checkBox中工作的人。我甚至不知道从哪里开始完成这项任务。你能告诉我一些提示吗?有用的方法,适当的文档网站,算法的草稿可能?

+1

你可以在Vogella的http://www.vogella.com/tutorials/AndroidListView/article.html相当完整教程的列表视图覆盖checboxes开始 – njzk2

+0

谢谢!我会检查它:) – Malvinka

回答

1

请参考下面的代码:

States.java

public class States { 

String code = null; 
String name = null; 
boolean selected = false; 

public States(String code, String name, boolean selected) { 
    super(); 
    this.code = code; 
    this.name = name; 
    this.selected = selected; 
} 

public String getCode() { 
    return code; 
} 

public void setCode(String code) { 
    this.code = code; 
} 

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

} 

MainActivity.java文件

public class MainActivity extends Activity { 

MyCustomAdapter dataAdapter = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Generate list View from ArrayList 
    displayListView(); 

    checkButtonClick(); 

} 

private void displayListView() { 

    // Array list of countries 
    ArrayList<States> stateList = new ArrayList<States>(); 

    States _states = new States("91", "India", false); 
    stateList.add(_states); 
    _states = new States("61", "Australia", true); 
    stateList.add(_states); 
    _states = new States("55", "Brazil", false); 
    stateList.add(_states); 
    _states = new States("86", "China", true); 
    stateList.add(_states); 
    _states = new States("49", "Germany", true); 
    stateList.add(_states); 
    _states = new States("36", "Hungary", false); 
    stateList.add(_states); 
    _states = new States("39", "Italy", false); 
    stateList.add(_states); 
    _states = new States("1", "US", false); 
    stateList.add(_states); 
    _states = new States("44", "UK", false); 
    stateList.add(_states); 

    // create an ArrayAdaptar from the String Array 
    dataAdapter = new MyCustomAdapter(this, R.layout.state_info, stateList); 
    ListView listView = (ListView) findViewById(R.id.listView1); 
    // Assign adapter to ListView 
    listView.setAdapter(dataAdapter); 

    listView.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // When clicked, show a toast with the TextView text 
      States state = (States) parent.getItemAtPosition(position); 
      Toast.makeText(getApplicationContext(), 
        "Clicked on : " + state.getName(), Toast.LENGTH_LONG) 
        .show(); 
     } 
    }); 
} 

private class MyCustomAdapter extends ArrayAdapter<States> { 

    private ArrayList<States> stateList; 

    public MyCustomAdapter(Context context, int textViewResourceId, 

    ArrayList<States> stateList) { 
     super(context, textViewResourceId, stateList); 
     this.stateList = new ArrayList<States>(); 
     this.stateList.addAll(stateList); 
    } 

    private class ViewHolder { 
     TextView code; 
     CheckBox name; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder holder = null; 

     Log.v("ConvertView", String.valueOf(position)); 

     if (convertView == null) { 

      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      convertView = vi.inflate(R.layout.state_info, null); 

      holder = new ViewHolder(); 
      holder.code = (TextView) convertView.findViewById(R.id.code); 
      holder.name = (CheckBox) convertView 
        .findViewById(R.id.checkBox1); 

      convertView.setTag(holder); 

      holder.name.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        CheckBox cb = (CheckBox) v; 
        States _state = (States) cb.getTag(); 

        Toast.makeText(
          getApplicationContext(), 
          "Checkbox: " + cb.getText() + " -> " 
            + cb.isChecked(), Toast.LENGTH_LONG) 
          .show(); 

        _state.setSelected(cb.isChecked()); 
       } 
      }); 

     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     States state = stateList.get(position); 

     holder.code.setText(" (" + state.getCode() + ")"); 
     holder.name.setText(state.getName()); 
     holder.name.setChecked(state.isSelected()); 

     holder.name.setTag(state); 

     return convertView; 
    } 

} 

private void checkButtonClick() { 

    Button myButton = (Button) findViewById(R.id.findSelected); 

    myButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      StringBuffer responseText = new StringBuffer(); 
      responseText.append("Selected Countries are...\n"); 

      ArrayList<States> stateList = dataAdapter.stateList; 

      for (int i = 0; i < stateList.size(); i++) { 
       States state = stateList.get(i); 

       if (state.isSelected()) { 
        responseText.append("\n" + state.getName()); 
       } 
      } 

      Toast.makeText(getApplicationContext(), responseText, 
        Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

} 

activity_main.xml中文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#ffeeeeee" 
android:gravity="center" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:text="Country Codes" 
    android:textSize="20sp" /> 

<Button 
    android:id="@+id/findSelected" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Get Selected Items" /> 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

</LinearLayout> 

state_info.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:padding="6dip" > 

<CheckBox 
    android:id="@+id/checkBox1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:text="checkbox" 
    android:textColor="#B40404" /> 

<TextView 
    android:id="@+id/code" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@id/checkBox1" 
    android:layout_alignBottom="@id/checkBox1" 
    android:layout_toRightOf="@id/checkBox1" 
    android:text="textview" 
    android:textColor="#ff000000" /> 

</RelativeLayout> 

您也可以参考此链接 http://developerandro.blogspot.com/2013/09/listview-with-checkbox-android-example.html

+0

谢谢,谢谢,谢谢! – Malvinka