gpt4 book ai didi

java - 数组列表、 ListView 和按钮

转载 作者:太空宇宙 更新时间:2023-11-04 13:47:32 26 4
gpt4 key购买 nike

我花了几天时间进行研究,但还没有找到我正在寻找的答案,所以也许你们可以提供一些帮助。

我的应用程序是一个交易卡牌组构建器。我有两个 ArrayList、数据库和 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;
}

最后是甲板 View 适配器

    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() 方法中,但无论我在那里扔什么,我都无法让它工作。有什么建议吗?

最佳答案

我明白了。通过将 onClick 方法放置在 on-item click 方法中,我产生了我想要的结果。其实我很沮丧,事情就这么简单。我花了四天时间试图解决这个问题。

     //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();
}
});

关于java - 数组列表、 ListView 和按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30678627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com