arraylists,listviews和按钮

时间:2015-06-06 03:28:09

标签: java android listview arraylist android-button

我花了几天时间研究,但我找不到我正在寻找的答案,所以也许你们可以帮忙一点。

我的应用是一个交易卡牌建设者。我有两个ArrayLists,数据库和currentDeck,两者都正常工作。我想要完成的任务是将所选的数据库项添加为currentDeck中的新项。

这是主要方法:

public class DeckBuilderActivity extends ActionBarActivity {

ArrayList<Card> database = new ArrayList<Card>(); //create new arraylist of datatype card
ArrayList<Card> currentDeck = new ArrayList<Card>(); //create arraylist for currently loaded deck

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_deck_builder);

    //begin method to create main database.
    cardfightMainDatabase(database);

    //format display in listview
    DatabaseAdapter adapter = new DatabaseAdapter(this, database);
    ListView lv = (ListView) findViewById(R.id.card_database_listview);
    lv.setAdapter(adapter);

    //format display in deck view
    DeckAdapter adapter2 = new DeckAdapter(this, currentDeck);
    GridView gv = (GridView) findViewById(R.id.deckGridView);
    gv.setAdapter(adapter2);

    //set up click event
    AdapterView.OnItemClickListener mMessageClickedHandler = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {

            //link image view
            ImageView iv = (ImageView) findViewById(R.id.cardDisplay);

            //link scrollable textview
            TextView tv = (TextView) findViewById(R.id.cardDataDisplay);

            // Do something in response to the click
            //set image id
            int imageId = database.get(position).getCardImgID();

            //display image to imageview
            iv.setImageResource(imageId);

            //display text to textview
            //Print card name, expansion, card number, skill and flavor text to scrollable textview
            tv.setText(database.get(position).getCardName() + "\nExpansion Set: " + database.get(position).getExpansion() +
                        "\nCard #: " + database.get(position).getCardNum() + "\nSkill:\n" + database.get(position).getUnitSkill()
                + "\nFlavor Text:\n" + "\"" + database.get(position).getCardLore()+ "\"");

        }
    };

    lv.setOnItemClickListener(mMessageClickedHandler);
} //end OnCreate Method

数据库适配器

public class DatabaseAdapter extends ArrayAdapter<Card> {


//constructor
public DatabaseAdapter(Context context, ArrayList<Card> database)
{
    super(context, 0, database); //call parent
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    //get the data item for this position
    Card card = getItem(position);
    //check if an existing view is being reused, otherwise inflate the view



    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(database_layout, parent, false);
    }
    //Lookup view for data population
    ImageView cardIcon = (ImageView) convertView.findViewById(R.id.card_icon);
    TextView cardInfo = (TextView) convertView.findViewById(R.id.card_info);
    TextView cardStats = (TextView) convertView.findViewById(R.id.card_stats);

    //set text views
    cardInfo.setText("<" + card.getUnitGrade() + ">" + " " + card.getCardName());
    cardStats.setText("Power " + card.getUnitPower() + "   Shield: " + card.getUnitShield());
    //set image view
    int cardIconId = card.getCardImgID(); //get image assigned to card and store as local int
    cardIcon.setImageResource(cardIconId); //populate imageview in list
    return convertView;
    }

最后是甲板视图适配器

    public class DeckAdapter extends ArrayAdapter<Card> {

public DeckAdapter(Context context, ArrayList<Card> currentDeck)
{
    super(context, 0, currentDeck); //call parent as constructor
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    //get data from card
    Card card = getItem(position);

    //check if an existing view is being reused, otherwise inflate the view

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(deck_layout, parent, false);
    }
    //get imageView and link to grid display
    ImageView iv = (ImageView) convertView.findViewById(R.id.grid_item_image);

    //get imageview id
    int cardIconId = card.getCardImgID(); //get image assigned to card and store as local int
    iv.setImageResource(cardIconId); //set image to view
    return convertView;

}

}

理论上,我想做一个

    addButton = (Button) findViewById(R.id.addToDeckButton);
            addButton.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v, int position) {

                    currentDeck.add(database.get(position));
                    adapter.notifyDataSetChanged();
                }

并将其放入数据库适配器的getView()方法中,但无论我在那里扔什么,我都无法让它工作。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我明白了。通过在on-item单击方法中放置onClick方法,我生成了我想要的结果。我真的很难过就是那么简单。我失去了四天试图解决这个问题。

     //set up click event for database
    AdapterView.OnItemClickListener mMessageClickedHandler = new AdapterView.OnItemClickListener() {
        public void onItemClick(final AdapterView parent, View v, final int position, long id) {
            //link image view
            ImageView iv = (ImageView) findViewById(R.id.cardDisplay);
            //link scrollable textview
            TextView tv = (TextView) findViewById(R.id.cardDataDisplay);
            // Do something in response to the click
            //set image id
            int imageId = database.get(position).getCardImgID();
            //display image to imageview
            iv.setImageResource(imageId);

            //Configure button click event
            addButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //add card from database to current deck
                    currentDeck.add(database.get(position));
                    adapter2.notifyDataSetChanged();
                }
            });