1
我已經花了幾天的時間進行研究,並且我還沒有找到我正在尋找的答案,所以也許你們可以幫助一下。數組列表,列表視圖和按鈕
我的應用程序是一個交易卡甲板生成器。我有兩個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()方法,但我不能讓它的工作,無論我在那裏扔。有什麼建議麼?
無法讓它工作怎麼樣?你嘗試了什麼?你想要發生什麼?究竟發生了什麼? – azurefrog
我嘗試獲取該項目的視圖位置,並通過將兩個陣列列表傳遞給該按鈕,執行一個操作以將所選項目「數據庫」添加到「當前套牌」。我一直在尋找使用onClickListener的例子和每個答案,但是我怎樣才能爲一個獲取數組列表的按鈕創建一個onClickListener?我必須創建一個自定義onClickListener嗎? –
是否有可能將ListView視圖作爲參數傳遞給按鈕,還是應該爲類似於我的getView方法的按鈕專門創建一個完全onClickListener? –