Few questions about ListViews and buttons

0 Dário Pedro · August 6, 2015
How can I get the button listener changing the text of a TextField outside a ListView?


1- FIRST

I've tryed creating a Listview.setOnItemClickListener(...) but when I click it , nothing happens.
I've also tryed creating a button listener when  the item is created on the getView method but cant accept the outside TextView.


2-SECOND

I would like to create some sorte of effect that when I click the button it will be filled ( like with water or something ) and the time that takes to be filled will be showed in the text , below the button. ( the value of the timer can be changed , changing the countdown timer and the time it takes to fill the button).


CODE AND SCREENSHOTS


package com.example.dario.game_v1.Navigation_bar_default_dir;

import android.app.Activity;
import android.database.ContentObserver;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dario.game_v1.R;
import com.example.dario.game_v1.Recurso_ListViewItens;
import com.example.dario.game_v1.userInfo;

import java.util.Arrays;
import java.util.List;

/**
* Created by Dario on 06/08/2015.
*/
public class PlaceholderFragment extends Fragment {

// private final String START_MONEY = "500";
private final String TAG = "Dhemage";
public TextView curr_money_txt;

public PlaceholderFragment() {
}


/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";

/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int sectionNumber = this.getArguments().getInt(ARG_SECTION_NUMBER);
View rootView;

switch (sectionNumber) {
case 1:
rootView = inflater.inflate(R.layout.home, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.activity_recursos, container, false);

/*CREATE Money TXT*/
curr_money_txt = (TextView) rootView.findViewById(R.id.curr_money_text);
curr_money_txt.setText(((navigation_bar) getActivity()).START_MONEY);

/*CREATE LIST VIEW*/
List nomes_recursos = Arrays.asList("Iron", "Silver", "Gold", "Painite", "Benitoite", "Deuterium");
ListAdapter recursosAdapter = new Recurso_ListViewItens(getActivity(), R.layout.recurso_box_fragment, nomes_recursos);
ListView recursos_ListView = (ListView) rootView.findViewById(R.id.recursos_listview);
recursos_ListView.setAdapter(recursosAdapter);
recursos_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Click ListItem Number " + position, Toast.LENGTH_LONG).show();
}
});

break;
case 3:
rootView = inflater.inflate(R.layout.fragment_navigation_bar, container, false);
break;
default:
rootView = inflater.inflate(R.layout.fragment_navigation_bar, container, false);
break;
}


return rootView;
}

public void add_money_resource(int value) {
int curr_money = Integer.parseInt(curr_money_txt.getText().toString());
curr_money = curr_money + value;
curr_money_txt.setText(String.valueOf(curr_money));
}

public int get_curr_money() {
return Integer.parseInt(curr_money_txt.getText().toString());
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((navigation_bar) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER));
}
}








package com.example.dario.game_v1;

import com.example.dario.game_v1.Navigation_bar_default_dir.PlaceholderFragment;
import com.example.dario.game_v1.recursos;

import android.media.Image;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewParent;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;

import java.lang.reflect.Method;
import java.util.List;

import static android.widget.Toast.*;
import static com.example.dario.game_v1.Navigation_bar_default_dir.PlaceholderFragment.*;
import static com.example.dario.game_v1.recursos.*;


/**
* Created by Dario on 02/08/2015.
*/
public class Recurso_ListViewItens extends ArrayAdapter {


private int layout;
private Context context;

public Recurso_ListViewItens(Context _context, int resource, List objects) {
super(_context, resource, objects);
layout = resource;
this.context = _context;
}


@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder mainViewholder = null;

if (convertView == null) {
//Inflate View
LayoutInflater fragmentInflater = LayoutInflater.from(getContext());
convertView = fragmentInflater.inflate(R.layout.recurso_box_fragment, parent, false);

//Create the viewHolder
final ViewHolder viewHolder = new ViewHolder();

//get the name of mineral
String singleRecursoItem = getItem(position);

//WIDGETS ON FRAGMENT
viewHolder.textoPreco = (TextView) convertView.findViewById(R.id.countdown);
viewHolder.texto_level_recurso = (TextView) convertView.findViewById(R.id.level_of_resource);
viewHolder.imageRecurso = (ImageView) convertView.findViewById(R.id.imagem_recurso);
viewHolder.getmoneyButton = (Button) convertView.findViewById(R.id.button_recurso_fragment);
viewHolder.upgradeButton = (Button) convertView.findViewById(R.id.upg_butt_recurso_fragment);

//set tag
convertView.setTag(viewHolder);

//SET INFORMATION ON WIDGET
viewHolder.texto_level_recurso.setId(position);
viewHolder.upgradeButton.setText("1");
viewHolder.getmoneyButton.setText(singleRecursoItem);

switch (position) {
case 0:
viewHolder.imageRecurso.setImageResource(R.mipmap.resource_iron);
break;
case 1:
viewHolder.imageRecurso.setImageResource(R.mipmap.resource_silver);
break;
case 2:
viewHolder.imageRecurso.setImageResource(R.mipmap.resource_gold);
break;
case 3:
viewHolder.imageRecurso.setImageResource(R.mipmap.resource_painite);
;
break;
case 4:
viewHolder.imageRecurso.setImageResource(R.mipmap.resource_benitoite);
break;
case 5:
viewHolder.imageRecurso.setImageResource(R.mipmap.resource_deuterium);
break;
}


//UPGRADE BUTTON
viewHolder.upgradeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//gets ugrade price
String precotxt = viewHolder.upgradeButton.getText().toString();
int curr_upg_price = Integer.parseInt(precotxt);

Method metodo[] = context.getClass().getMethods();

//check if got money to pay
/* if ( .curr_money_txt > curr_upg_price) {
//pays the upgrade
((recursos) getContext()).add_money_resource(-curr_upg_price);

//level icon increments
String niveltxt = viewHolder.texto_level_recurso.getText().toString();
int lvl = Integer.parseInt(niveltxt);
lvl++;
viewHolder.texto_level_recurso.setText(String.valueOf(lvl));

//increses price of upgrade
int next_price = next_upgrade_price(position, curr_upg_price);
viewHolder.upgradeButton.setText(String.valueOf(next_price));
} else {
Toast.makeText((recursos) getContext(), "NOT ENOUGH COIN", Toast.LENGTH_SHORT);
}*/
}
});

//GET MONEY
viewHolder.getmoneyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//get money on click
String money_on_click = viewHolder.texto_level_recurso.getText().toString();

int money_to_add = Integer.parseInt(money_on_click);
//add money
// ((recursos) getContext()).add_money_resource(money_to_add);
}
});
} else {
mainViewholder = (ViewHolder) convertView.getTag();
}
return convertView;
}

public class ViewHolder {
//WIDGETS ON FRAGMENT
TextView textoPreco;
TextView texto_level_recurso;
ImageView imageRecurso;
Button getmoneyButton;
Button upgradeButton;
}

//button upgrade pressed
//returns 0 if error occurs
public int next_upgrade_price(int position, int curr_price) {
int next_price;
switch (position) {
case 0:
next_price = (int) (curr_price * 1.5 + 1);
break;
case 1:
next_price = (int) (curr_price * 2 + 1);
break;
case 2:
next_price = (int) (curr_price * 2.5);
break;
case 3:
next_price = (int) (curr_price * 2.7);
break;
case 4:
next_price = (int) (curr_price * 3);
break;
case 5:
next_price = (int) (curr_price * 5);
break;
default:
return 0;
}

return next_price;
}

//upgrades the income
//returns 0 if error occurs
public int get_money(int position, int curr_price) {
int next_price;
switch (position) {
case 0:
next_price = (int) (curr_price * 1.5 + 1);
break;
case 1:
next_price = (int) (curr_price * 2 + 1);
break;
case 2:
next_price = (int) (curr_price * 2.5);
break;
case 3:
next_price = (int) (curr_price * 2.7);
break;
case 4:
next_price = (int) (curr_price * 3);
break;
case 5:
next_price = (int) (curr_price * 5);
break;
default:
return 0;
}

return next_price;
}


}




/images/forum/upload/2015-08-06/402912d70304add069c4f1ed0d95cd22.png



/images/forum/upload/2015-08-06/1e31c67322a51c346aeb9353f8e1ad8d.png


Post a Reply

Replies

Oldest  Newest  Rating
0 Dário Pedro · August 7, 2015
Edited: the code field correctly
0 Dário Pedro · August 8, 2015
Help me pls :S
  • 1

Java / Android Development

107,142 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator