1

ボタンがクリックされたときにチェックされるチェックボックスを表示するテキストビューを設定しようとしています。

いつかこの情報を保持できるようになることを望んでいますが、どれが選択されているかを知るだけでうまくいくでしょう。

テストテキストビューを使用しています

助言がありますか?

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


        } 

}

4

2 に答える 2

0

これが私のコードです。これが役立つことを願っています。そのリストから選択されたアイテムを取得するには、スキル リストをループして skill.isSelected == true を見つけるだけです。true の場合、アイテムが選択されていることを簡単に知ることができます。

たとえば、ボタン (btnGetSelectedSkill) をアクティビティに追加できます。次に、onClick リスナーは、次のメソッド getSelectedSkills(); を呼び出すことができます。

private void getSelectedSkill()
    {
        List<Skill> selectedSkill = new ArrayList<Skill>();
        for(Skill s: this.skills)
        {
            if(s.isSelected())
                selectedSkill.add(s); // Here you will get your selected skill in this list.
        }
    }

スキル.java

package com.skill.vo;

public class Skill
{
    private long id;
    private String name;
    private boolean selected;

    public Skill()
    {

    }

    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

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

SkillView.java

package com.skill.component;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.skill.vo.Skill;

public class SkillView extends LinearLayout
{
    // To store skill information
    private Skill skill;
    // For UI
    private Context context;
    private CheckBox chkSelect;
    private TextView tvSkillName;

    public SkillView(Context context, Skill skill)
    {
        super(context);
        this.context = context;
        this.skill = skill;

        /* Initialize UI components */
        this.chkSelect = new CheckBox(this.context);
        this.chkSelect.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                SkillView.this.skill.setSelected(((CheckBox) view).isChecked());
            }
        });

        this.tvSkillName = new TextView(this.context);
        this.tvSkillName.setTextSize(15);
        this.tvSkillName
                .setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        this.tvSkillName.setText(" " + this.skill.getName());

        // Add components to main layout
        this.setOrientation(HORIZONTAL);
        this.addView(this.chkSelect, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        this.addView(this.tvSkillName, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    }

    public Skill getSkill()
    {
        return skill;
    }

    public void setSkill(Skill skill)
    {
        this.skill = skill;
    }
}

スキルビューアダプター

package com.skill.component;

import java.util.List;

import com.skill.vo.Skill;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class SkillViewAdapter extends BaseAdapter
{
    private Context context = null;
    private List<Skill> skillList = null;

    public SkillViewAdapter(Context context, List<Skill> skillList)
    {
        this.context = context;
        this.skillList = skillList;
    }

    public int getCount()
    {
        return this.skillList != null ? this.skillList.size() : 0;
    }

    public Object getItem(int position)
    {
        return this.skillList != null ? this.skillList.get(position) : null;
    }

    public long getItemId(int position)
    {
        if (this.skillList != null)
        {
            Skill p = this.skillList.get(position);
            return p.getId();
        }
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        SkillView skillView;

        if (convertView == null)
        {
            skillView = new SkillView(this.context, this.skillList.get(position));
        } else
        {
            skillView = (SkillView) convertView;
            skillView.setSkill(this.skillList.get(position));
        }
        return skillView;
    }

    public List<Skill> getProductList()
    {
        return this.skillList;
    }
}

スキルアクティビティ

package com.skill.activity;

import java.util.ArrayList;
import java.util.List;

import com.skill.component.SkillViewAdapter;
import com.skill.vo.Skill;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class SkillAndroidActivity extends Activity
{

    private ListView listViewSkills = null;
    private List<Skill> skills = null;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.listViewSkills = (ListView) this.findViewById(R.id.lv_skills);

        this.skills = new ArrayList<Skill>();
        Skill skill = null;

        skill = new Skill();
        skill.setId(1);
        skill.setName("Java");
        this.skills.add(skill);

        skill = new Skill();
        skill.setId(2);
        skill.setName("PHP");
        this.skills.add(skill);

        skill = new Skill();
        skill.setId(3);
        skill.setName("C++");
        this.skills.add(skill);

        skill = new Skill();
        skill.setId(4);
        skill.setName("VB.net");
        this.skills.add(skill);

        SkillViewAdapter adapter = new SkillViewAdapter(this, this.skills);
        this.listViewSkills.setAdapter(adapter);
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_main"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ListView
        android:id="@+id/lv_skills"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="9" >
    </ListView>
</LinearLayout>

スクリーンショット

于 2012-04-19T03:54:27.163 に答える
0

すべてのスキルをループして、どれが正しいかを確認する必要がisCheckedあります。

于 2012-04-19T02:01:12.787 に答える