我有一个listview
,listview
中的每个项目都包含button
。
当method
点击时,会调用button
。
预期的行为是文字更改为红色,文字大小增加。
现在,当listview
适合屏幕时,此功能完全正常。但是,当listview
不适合屏幕时,
如果我按下第一项不适合的button
,那么该项目和listview
中的第一项都会突出显示。类似地,
如果我按下listview
中的第一项,则该内容和第一项不适合突出显示。
我假设我需要使用位置变量以某种方式解决这个问题但是我无法确定问题是什么。
public void clicking(View v) {
Button b = (Button) v;
LinearLayout layout = (LinearLayout) v.getParent();
String buttontext = b.getText().toString();
TextView betidtextbox = (TextView) layout.findViewById(R.id.gid);
LinearLayout layoutt = (LinearLayout) layout.getParent();
String betid = betidtextbox.getText().toString();
if (b.getTag() == "highlighted") {
b.setTextColor(Color.parseColor("#000000"));
b.setTextSize(18);
b.setTag("");
selection = "home";
else {
b.setTextColor(Color.parseColor("#EB102E"));
b.setTextSize(20);
b.setTag("highlighted");
selection = "home";
活动代码:
public class AllGameslistActivity extends ListActivity {
private Bet newBet = new Bet();
private double stake = 0.00;
private String name = "";
private double newwinnings;
private String newwinningstoString;
private View itemView;
private String selection;
private ArrayList<TipDisplayer> tomee = new ArrayList<>();
// Progress Dialog
private static String url_all_games = "***";
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> gamesList;
// url to get all products list
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_GAMELIST = "gamelist";
private static final String TAG_ID = "ID";
private static final String TAG_LEAGUE = "League";
private static final String TAG_TEAMS = "Teams";
private static final String TAG_BET = "Bet";
private static final String TAG_ODDS = "Odds";
private static final String TAG_DATETIMER = "DateTimer";
private static final String TAG_COMMENTS = "Comments";
private static final String TAG_TYPE = "Type";
// products JSONArray
JSONArray allgames = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_bets);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
menu menu = (menu) getFragmentManager().findFragmentById(R.id.fragment);
menu.betnowclick();
SessionManager session;
session = new SessionManager(getApplicationContext());
HashMap<String, String> user = session.getUserDetails();
name = user.get(SessionManager.USERNAME);
menu.updateinfo(getName());
// Hashmap for ListView
gamesList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllGames().execute();
// Get listview
ListView lv = getListView();
}
/**
* Background Async Task to Load all product by making HTTP Request
*/
class LoadAllGames extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* getting All products from url
*/
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_games, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Games: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Games
allgames = json.getJSONArray(TAG_GAMELIST);
// looping through All Products
for (int i = 0; i < allgames.length(); i++) {
JSONObject c = allgames.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String league = c.getString(TAG_LEAGUE);
String odds = c.getString(TAG_ODDS);
String comments = c.getString(TAG_COMMENTS);
String type = c.getString(TAG_TYPE);
String bet = c.getString(TAG_BET);
String datetimer = c.getString(TAG_DATETIMER);
String Teams = c.getString(TAG_TEAMS);
Double Odds = Double.parseDouble(odds);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_LEAGUE,league);
map.put(TAG_ODDS,odds);
map.put(TAG_COMMENTS,comments);
map.put(TAG_TYPE,type);
map.put(TAG_BET,bet);
map.put(TAG_DATETIMER,datetimer);
map.put(TAG_TEAMS, Teams);
Log.d("id", id);
Log.d("league", league);
Log.d("odds", odds);
Log.d("comments", comments);
Log.d("Type", type);
Log.d("bet", bet);
Log.d("datetimer", datetimer);
Log.d("teams",Teams);
tomee.add(i, new TipDisplayer(id, league, Teams, bet, odds, datetimer, comments, type));
// adding HashList to ArrayList
gamesList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return "";
}
/**
* After completing background task Dismiss the progress dialog
* *
*/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
// updating UI from Background Thread
PopulateList();
}
}
private class MyListAdapter extends ArrayAdapter<TipDisplayer> {
public MyListAdapter() {
super(AllGameslistActivity.this, R.layout.list_item, tomee);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
}
TextView leaguetxt = (TextView) itemView.findViewById(R.id.leaguetxt);
TextView datetxt = (TextView) itemView.findViewById(R.id.datetxt);
TextView teamstxt = (TextView) itemView.findViewById(R.id.teamstxt);
TextView bettxt = (TextView) itemView.findViewById(R.id.bettxt);
TextView betid = (TextView) itemView.findViewById(R.id.gid);
TextView typetxt = (TextView) itemView.findViewById(R.id.difficultytxt);
TextView commentstxt = (TextView) itemView.findViewById(R.id.commenttxt);
Button oddsbtn = (Button) itemView.findViewById(R.id.oddsbutton);
TipDisplayer currentwriter = tomee.get(position);
String leaguetext = currentwriter.getLeague();
String datetext = currentwriter.getDatetimer();
String teamstext = currentwriter.getTeams();
String bettext = currentwriter.getBet();
String typetext = currentwriter.getType();
String idtext = currentwriter.getId();
String commentsText = currentwriter.getComments();
String oddstext = currentwriter.getOdds();
leaguetxt.setText(leaguetext);
datetxt.setText(datetext.substring(0,datetext.lastIndexOf(":")) + " GMT");
teamstxt.setText(teamstext);
bettxt.setText(bettext);
betid.setText(idtext);
commentstxt.setText(commentsText);
oddsbtn.setText(oddstext);
typetxt.setText(typetext);
if (typetext.equals("Low Risk")) {
typetxt.setTextColor(Color.parseColor("#067103"));
}
else if (typetext.equals("Medium Risk")) {
typetxt.setTextColor(Color.parseColor("#D9D216"));
}
else if (typetext.equals("Longshot")) {
typetxt.setTextColor(Color.parseColor("#F75528"));
}
return itemView;
}
}
private void PopulateList() {
ArrayAdapter<TipDisplayer> adapter = new MyListAdapter();
final ListView list = (ListView) findViewById(R.id.mylist);
list.setAdapter(adapter);
}
public void SelectBet(View v) {
clicking(v);
}
public void clicking(View v) {
Button b = (Button) v;
ListView lv = getListView();
int position = lv.getPositionForView(v);
LinearLayout layout = (LinearLayout) v.getParent();
String buttontext = b.getText().toString();
TextView betidtextbox = (TextView) layout.findViewById(R.id.gid);
LinearLayout layoutt = (LinearLayout) layout.getParent();
String betid = betidtextbox.getText().toString();
if (b.getTag().toString().equals("highlighted")) {
b.setTextColor(Color.parseColor("#000000"));
b.setTextSize(18);
b.setTag("");
selection = "home";
TextView teamss = (TextView) layoutt.findViewById(R.id.teamstxt);
String teams = teamss.getText().toString();
newBet.generateoddstesting(betid, buttontext, false,teams,selection);
double newodds = newBet.calculateodds();
TextView myBetOdds = (TextView) findViewById(R.id.bettingodds);
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
myBetOdds.setText("@" + String.format("%.2f", newodds) + "/1");
EditText mEdit = (EditText) findViewById(R.id.editText2);
if (mEdit.getText().toString().length() == 0) {
stake = 0.00;
newwinnings = 0.00;
potentialWinnings.setText("0.00");
} else {
mEdit.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() != 0) {
stake = Double.parseDouble(s.toString());
double newodds = newBet.calculateodds();
newwinnings = stake * newodds;
TextView myBetOdds = (TextView) findViewById(R.id.bettingodds);
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
myBetOdds.setText("@" + String.format("%.2f",newodds) + "/1");
potentialWinnings.setText(String.format("%.2f", (newwinnings)));
newwinningstoString = potentialWinnings.getText().toString();
} else {
stake = 0.00;
newwinnings = 0.00;
double newodds = newBet.calculateodds();
TextView myBetOdds = (TextView) findViewById(R.id.bettingodds);
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
myBetOdds.setText("@" + String.format("%.2f",newodds) + "/1");
potentialWinnings.setText(String.format("%.2f", (newwinnings)));
newwinningstoString = potentialWinnings.getText().toString();
}
}
public void afterTextChanged(Editable s) {
}
});
//stake = Double.parseDouble(mEdit.getText().toString());
newwinnings = stake*newodds;
potentialWinnings.setText(String.format("%.2f", (newwinnings)));
newwinningstoString = potentialWinnings.getText().toString();
}
} else {
b.setTextColor(Color.parseColor("#EB102E"));
b.setTextSize(20);
b.setTag("highlighted");
selection = "home";
String getodds = b.getText().toString();
EditText mEdit = (EditText) findViewById(R.id.editText2);
if (mEdit.getText().toString().length() == 0) {
stake = 0.00;
newwinnings = 0.00;
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
potentialWinnings.setText("0.00");
} else {
mEdit.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() != 0) {
stake = Double.parseDouble(s.toString());
double newodds = newBet.calculateodds();
newwinnings = stake * newodds;
TextView myBetOdds = (TextView) findViewById(R.id.bettingodds);
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
myBetOdds.setText("@" + String.format("%.2f",newodds) + "/1");
potentialWinnings.setText(String.format("%.2f", (newwinnings)));
newwinningstoString = potentialWinnings.getText().toString();
} else {
stake = 0.00;
double newodds = newBet.calculateodds();
newwinnings = stake * newodds;
TextView myBetOdds = (TextView) findViewById(R.id.bettingodds);
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
myBetOdds.setText("@" + String.format("%.2f",newodds) + "/1");
potentialWinnings.setText(String.format("%.2f", (newwinnings)));
newwinningstoString = potentialWinnings.getText().toString();
}
}
public void afterTextChanged(Editable s) {
}
});
stake = Double.parseDouble(mEdit.getText().toString());
TextView teamms = (TextView) layoutt.findViewById(R.id.teamstxt);
String teams = teamms.getText().toString();
newBet.generateoddstesting(betid, buttontext, true,teams,selection);
double newodds = newBet.calculateodds();
newwinnings = stake * newodds;
TextView myBetOdds = (TextView) findViewById(R.id.bettingodds);
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
myBetOdds.setText("@" + String.format("%.2f",newodds) + "/1");
potentialWinnings.setText(String.format("%.2f", (newwinnings)));
newwinningstoString = potentialWinnings.getText().toString();
}
}
}
list_item布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/whatisthis"
android:weightSum="160">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<TextView
android:layout_width="0dp"
android:id="@+id/leaguetxt"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="50"
android:gravity="center"
android:text="English Premier League"
android:textColor="#067103"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/datetxt"
android:layout_gravity="center"
android:layout_weight="50"
android:gravity="center"
android:text="BET UNTIL : 23/05/2015 15:00 GMT"
android:textColor="#067103"
android:textSize="12sp"
android:textStyle="italic" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="30">
<TextView
android:id="@+id/teamstxt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:gravity="left|center"
android:text="Sevilla FC - FC Barcelona"
android:textColor="#067103"
android:textSize="20sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:orientation="horizontal">
<!-- Name Label -->
<TextView
android:layout_width="0dp"
android:id="@+id/bettxt"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="30"
android:gravity="center"
android:textSize="23sp"
android:text="FC Barcelona Win and BTTS"
android:textColor="#067103"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_weight="20"
android:weightSum="100"
android:orientation="horizontal"
android:layout_height="0dp">
<TextView
android:id="@+id/difficultytxt"
android:layout_weight="100"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#067103"
android:text="MEDIUM RISK"
android:layout_width="0dp"
android:layout_height="fill_parent" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:id="@+id/commenttxt"
android:textStyle="italic"
android:layout_height="wrap_content"
android:textColor="#067103"
android:text = " "/>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_weight="20"
android:weightSum="100"
android:orientation="horizontal"
android:layout_height="0dp">
<TextView
android:layout_width="0dp"
android:layout_weight="40"
android:layout_height="fill_parent" />
<Button
android:id="@+id/oddsbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20"
android:layout_gravity="center"
android:tag = ""
android:gravity="center"
android:onClick="SelectBet"
android:text="3.60"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:id = "@+id/gid"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
XML活动代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/timeleft"
android:orientation="horizontal"
android:weightSum="100">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="5"
android:text="Stake"
android:gravity="center"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="14dp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_weight="30"
android:layout_height="fill_parent"
android:ems="10"
android:textColorHint="#FFFFFF"
android:textColor="#FFFFFF"
android:hint="0"
android:gravity="center_vertical"
android:inputType="number" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="65"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_gravity="top"
android:layout_marginBottom="-5dp"
android:includeFontPadding="false"
android:text="@string/potentialwinnings"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="@+id/bettingodds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:text="\@1.00/1"
android:textColor="#FFFFFF"
android:textSize="12dp"
android:textStyle="italic">
</TextView>
<TextView
android:id="@+id/potentialwinnings"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:gravity="bottom"
android:text="0.00"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="5"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:onClick="NewBetMaker"
android:text="Save Bet!"></Button>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/bottom_control_bar"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.albert.betterapp.menu"
android:id="@+id/fragment"
tools:layout="@layout/fragment_menu" />
<ListView
android:id="@+id/mylist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/bottom_control_bar" />
<ListView
android:id="@android:id/list"
android:layout_width="0dp"
android:visibility="gone"
android:layout_height="0dp"
/>
</LinearLayout>
</RelativeLayout>
因此listview中的每个项目都有一个按钮,当我点击listview中第一个项目的按钮时,对第一个项目和第一个不可见的项目运行onclick方法,如果我为第二项,它运行第二项和第二项不可见项。同样,如果我向下滚动并单击第一个不可见的项目,它将继续运行该项目以及第一个项目和模式。
答案 0 :(得分:0)
在getView()
内,您可以使用OnClickListener
:
oddsbtn.setText(oddstext);
oddsbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// Do whatever you want, view v is your button.
});
答案 1 :(得分:0)
我注意到你刚从ListView添加了getPositionForView
()调用。这不是一种可行的方法。原因是AdapterView中的位置没有很好地定义。它与选择哪一行没有直接关系。文档甚至没有说它是。
但是,您应该可以通过在适配器上添加代码来实现目标。我现在没有多少时间。现在,请阅读一个好的网页教程@ Android Listview。 请查看页面中的代码段:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
编辑1:
一个可疑代码是您需要将ArrayList传递给适配器。您不应该依赖静态对象tomee
。适配器更喜欢稳定的对象和#34;最终&#34;。
更改自:
private void PopulateList() {
ArrayAdapter<TipDisplayer> adapter = new MyListAdapter();
以强>:
private void PopulateList() {
ArrayAdapter<TipDisplayer> adapter = new MyListAdapter(tomee);
添加强>:
public MyListAdapter(ArrayList<TipDisplayer> tomee) {
super(AllGameslistActivity.this, R.layout.list_item, tomee);
this.tomee = tomee; // declare tomee in the Adapter, don't use static
}
注意:
PopulateList
()中的更改。PopulateList
()中调用onPostExecute
()。我认为这是一个聪明的主意,但我不确定它的可靠性,需要进一步思考。首先修复一个。