我只是想设置一个文本视图,显示单击按钮时选中的复选框。
我希望有一天能够保留这些信息,但现在只知道选择了哪些信息就可以了。
我正在使用测试文本视图
有什么建议么?
public class skills_management extends Activity implements View.OnClickListener{
private Button buttonLocation;
private ListView mainListView ;
private Skill[] Skills ;
private ArrayAdapter<Skill> listAdapter ;
static CheckBox cb;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.skills);
Button buttonLocation = (Button) findViewById(R.id.buttonLocation);
buttonLocation.setOnClickListener(this);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// When item is tapped, toggle checked properties of CheckBox and Skill.
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick( AdapterView<?> parent, View item, int position, long id) {
Skill Skill = listAdapter.getItem( position );
Skill.toggleChecked();
SkillViewHolder viewHolder = (SkillViewHolder) item.getTag();
viewHolder.getCheckBox().setChecked( Skill.isChecked() );
}
});
// Create and populate Management Skills.
Skills = (Skill[]) getLastNonConfigurationInstance() ;
if ( Skills == null ) {
Skills = new Skill[] {
new Skill("Fiscal Management"),
new Skill("Empowers Others"),
new Skill("Ethical"),
new Skill("Project Management"),
new Skill("Problem Solving"),
new Skill("Strategic"),
new Skill("Entrepreneurial"),
new Skill("Strong Communication"),
new Skill("Results Oriented"),
new Skill("Delegation"),
new Skill("Professional"),
new Skill("Decision Maker"),
new Skill("Sales"),
new Skill("Computer"),
new Skill("Marketing"),
new Skill("Training"),
new Skill("Executive")
};
}
ArrayList<Skill> SkillList = new ArrayList<Skill>();
SkillList.addAll( Arrays.asList(Skills) );
// Set our custom array adapter as the ListView's adapter.
listAdapter = new SkillArrayAdapter(this, SkillList);
mainListView.setAdapter( listAdapter );
}
/** Holds Skill data. */
private static class Skill {
private String name = "" ;
private boolean checked = false ;
public Skill( String name ) {
this.name = name ;
}
public String getName() {
return name;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String toString() {
return name ;
}
public void toggleChecked() {
checked = !checked ;
}
}
/** Holds child views for one row. */
private static class SkillViewHolder {
private CheckBox checkBox ;
private TextView textView ;
public SkillViewHolder( TextView textView, CheckBox checkBox ) {
this.checkBox = checkBox ;
this.textView = textView ;
}
public CheckBox getCheckBox() {
return checkBox;
}
public TextView getTextView() {
return textView;
}
}
/////DISPLAY TEXTVIEW
//called when the button is clicked
public void onClick(View view)
{
TextView test= (TextView) findViewById(R.id.testTextView);
test.setText("test"); //shows whole filename
}
/** Custom adapter for displaying an array of Skill objects. */
private static class SkillArrayAdapter extends ArrayAdapter<Skill> {
private LayoutInflater inflater;
public SkillArrayAdapter( Context context, List<Skill> SkillList ) {
super( context, R.layout.skill_row, R.id.rowTextView, SkillList );
// Cache the LayoutInflate to avoid asking for a new one each time.
inflater = LayoutInflater.from(context) ;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Skill to display
Skill Skill = (Skill) this.getItem( position );
// The child views in each row.
final CheckBox checkBox ;
TextView textView ;
// Create a new row view
if ( convertView == null ) {
convertView = inflater.inflate(R.layout.skill_row, null);
// Find the child views.
textView = (TextView) convertView.findViewById( R.id.rowTextView );
checkBox = (CheckBox) convertView.findViewById( R.id.CheckBox01 );
// Optimization: Tag the row with it's child views, so we don't have to
// call findViewById() later when we reuse the row.
convertView.setTag( new SkillViewHolder(textView,checkBox) );
// If CheckBox is toggled, update the Skill it is tagged with.
checkBox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
cb = (CheckBox) v ;
Skill Skill = (Skill) cb.getTag();
Skill.setChecked( cb.isChecked() );
}
});
}
// Reuse existing row view
else {
// Because we use a ViewHolder, we avoid having to call findViewById().
SkillViewHolder viewHolder = (SkillViewHolder) convertView.getTag();
checkBox = viewHolder.getCheckBox() ;
textView = viewHolder.getTextView() ;
}
// Tag the CheckBox with the Skill it is displaying, so that we can
// access the Skill in onClick() when the CheckBox is toggled.
checkBox.setTag( Skill );
// Display Skill data
checkBox.setChecked( Skill.isChecked() );
textView.setText( Skill.getName() );
return convertView;
}//end class
}
}