Adapterの使い方

4782 ワード

Adapterの使い方:

public class ListAdapter extends BaseAdapter {
	private Context mContext;
    
    private ArrayList<String> mTitles = new ArrayList<String>();

	private ArrayList<String> mDialogue = new ArrayList<String>();
    
    private ArrayList<Boolean> mExpanded = new ArrayList<Boolean>();
    
    private int mCount = 0;
	
    public ListAdapter (Context context, 
    		ArrayList<String> titles,
    		ArrayList<String> dialogue,
    		ArrayList<Boolean> expanded,
    		int count)
    {
    	mTitles = titles;
        mContext = context;
        mDialogue = dialogue;
        mExpanded = expanded;
        mCount = count;
    }
    
    public int getCount() {
        return mCount;
    }

    
    public void setCount(int mCount) {
		this.mCount = mCount;
	}

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        VerView sv;// 
        if (convertView == null) {
            sv = new VerView(mContext, (String)mTitles.get(position), mDialogue.get(position), mExpanded.get(position));
        } else {
            sv = (VerView)convertView;
            sv.setTitle(mTitles.get(position));
            sv.setDialogue(mDialogue.get(position));
            sv.setExpanded(mExpanded.get(position));
        }
        
        return sv;
    }

    public void toggle(int position) {
    	mExpanded.set(position, !mExpanded.get(position));
    	for(int i = 0; i < mExpanded.size(); i++){
    		 if (i != position){
    			 mExpanded.set(i, false);
    		 }
    	}
    	
        notifyDataSetChanged();
    }

    public ArrayList<String> getTitles() {
		return mTitles;
	}

	public void setTitles(ArrayList<String> mTitles) {
		this.mTitles = mTitles;
	}

	public ArrayList<String> getDialogue() {
		return mDialogue;
	}

	public void setDialogue(ArrayList<String> mDialogue) {
		this.mDialogue = mDialogue;
		notifyDataSetChanged();
	}

	public ArrayList<Boolean> getExpanded() {
		return mExpanded;
	}

	public void setExpanded(ArrayList<Boolean> mExpanded) {
		this.mExpanded = mExpanded;
	}
}

public class VerView extends LinearLayout {
	private TextView mTitle;
    private TextView mDialogue;
    private ImageView mImageView;
    
    public VerView(Context context, String title, String dialogue, boolean expanded) {
        super(context);
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.list_item_view, null).findViewById(R.id.verListView);
		addView(layout);
		
		RelativeLayout rl = (RelativeLayout) layout.getChildAt(0);
		mTitle = (TextView)rl.getChildAt(0);
		mTitle.setText(title);
		mImageView = (ImageView)rl.getChildAt(1);
		mImageView.setImageResource(R.drawable.more_normal);
		
		mDialogue = (TextView) layout.getChildAt(1);
		mDialogue.setText(dialogue);

        mDialogue.setVisibility(expanded ? VISIBLE : GONE);
    }
    
    /**
     * Convenience method to set the title of a SpeechView
     */
    public void setTitle(String title) {
        mTitle.setText(title);
    }
    
    /**
     * Convenience method to set the dialogue of a SpeechView
     */
    public void setDialogue(String words) {
        mDialogue.setText(words);
    }
    
    /**
     * Convenience method to expand or hide the dialogue
     */
    public void setExpanded(boolean expanded) {
        mDialogue.setVisibility(expanded ? VISIBLE : GONE);
        if (expanded){
        	mImageView.setImageResource(R.drawable.more_normal_right);
        	this.setBackgroundColor(Color.WHITE);
        }
        else{
        	mImageView.setImageResource(R.drawable.more_normal);
        	this.setBackgroundColor(Color.TRANSPARENT);
        }
    }
    
}