在列表视图,Android中滚动或添加项目时按钮文本消失(button text disappears when scrolling or adding and item in listview, Android)
我遇到了我正在使用的列表视图问题。 列表视图中的每个项目都有两个按钮,用户可以使用这两个按钮来添加或取消每个参赛者的积分。
问题是当我向列表视图中添加新项目或滚动时(至少添加了一个项目后)代码随机将文本从一个或多个项目中的按钮中取出。
任何人都可以告诉我A)是什么导致这个? 和B)如何解决它?
我已经包含了我认为相关的代码。 如果你认为我需要在这里再增加一些,请告诉我。
我的适配器类:
public class ContestantListAdapter extends BaseAdapter { private ArrayList<ContestantItem> contestantList; private LayoutInflater layoutInflater; public ContestantListAdapter(Context context, ArrayList<ContestantItem> contestantList){ this.contestantList = contestantList; layoutInflater = LayoutInflater.from(context); } @Override public int getCount(){ return contestantList.size(); } public View getView(int position, View convertView, ViewGroup parentView){ ViewHolder viewHolder; if(convertView == null){ convertView = layoutInflater.inflate(R.layout.contestant_row_layout,null);//null might need to be activity_main viewHolder = new ViewHolder(); viewHolder.nameView = (TextView) convertView.findViewById(R.id.nameView); viewHolder.pointsView = (TextView) convertView.findViewById(R.id.pointsView); convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder)convertView.getTag(); } viewHolder.nameView.setText((contestantList.get(position).getName())); viewHolder.pointsView.setText(Integer.toString(contestantList.get(position).getPoints())); //get button position Button button =(Button) convertView.findViewById(R.id.Minus); button.setOnClickListener(minusListener); Button buttonPlus = (Button) convertView.findViewById(R.id.Plus); buttonPlus.setOnClickListener(addListener); return convertView; } private View.OnClickListener minusListener = new View.OnClickListener() { @Override public void onClick(View v) { View parentRow = (View) v.getParent(); ListView listView = (ListView) parentRow.getParent(); final int position = listView.getPositionForView(parentRow); ContestantItem tmpContestant = (ContestantItem) contestantList.get(position); int tmpPoints = tmpContestant.getPoints()-1; if(tmpPoints<0){tmpPoints=0;} tmpContestant.setPoints(tmpPoints); notifyDataSetChanged(); } }; private View.OnClickListener addListener = new View.OnClickListener() { @Override public void onClick(View v) { View parentRow = (View) v.getParent(); ListView listView = (ListView) parentRow.getParent(); final int position = listView.getPositionForView(parentRow); ContestantItem tmpContestant = (ContestantItem) contestantList.get(position); int tmpPoints = tmpContestant.getPoints()+1; tmpContestant.setPoints(tmpPoints); notifyDataSetChanged(); } }; static class ViewHolder { TextView nameView; TextView pointsView; } @Override public Object getItem(int index){ return contestantList.get(index); } @Override public long getItemId(int index){ return index; } //this method should add the contestent to the listView. must know what position to add it in. public void addItem(String name, int index){ ContestantItem tempContestant = new ContestantItem(); tempContestant.setName(name); tempContestant.setPoints(1); contestantList.add(index,tempContestant); notifyDataSetChanged(); } . . .
ContestantRowLayout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="0.15" android:id="@+id/Minus" android:text="@string/minus" /> <TextView android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="0.5" android:id="@+id/nameView" /> <TextView android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="0.2" android:id="@+id/pointsView" /> <Button android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="0.15" android:id="@+id/Plus" android:text="@string/plus" /> </LinearLayout>
主要活动:
public class MainActivity extends AppCompatActivity implements ContestantsFragment.OnFragmentInteractionListener{ ListView listView1; ContestantListAdapter contestantAdapter; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList<ContestantItem> rowDetails = getListData(); listView1 = (ListView) findViewById(R.id.contactsListView); listView1.setAdapter(new ContestantListAdapter(this,rowDetails)); contestantAdapter = (ContestantListAdapter) listView1.getAdapter(); } private ArrayList getListData(){ ArrayList<ContestantItem> contestantList = new ArrayList<ContestantItem>(); ContestantItem cItem; for(int i=0;i<10;i++){ cItem = new ContestantItem(); cItem.setName("Danny Anderson"); cItem.setPoints(5); contestantList.add(cItem); } return contestantList; } @Override public void onFragmentInteraction(Uri uri) { } //if the name is found returns the index position in the list. If not found -99 public void findAlphabetical(View view){ //TODO implement Binary search algorithm. hideKeyboard(); EditText tmpEditTxt = (EditText) findViewById(R.id.searchField); String inputTxt = tmpEditTxt.getText().toString(); int findAtIndex =contestantAdapter.searchAlphabetical(inputTxt)-1; if(findAtIndex <0){findAtIndex=0;} listView1.smoothScrollToPosition(findAtIndex); } public void addInAlpabetOrder(View view){ //TODO add the name in alphabetical order hideKeyboard(); //ListView tmpListView = (ListView) contestantsFragment.getView().findViewById(R.id.contactsListView); EditText tmpEditTxt = (EditText) findViewById(R.id.searchField); String inputTxt = tmpEditTxt.getText().toString(); int addAtIndex =contestantAdapter.searchAlphabetical(inputTxt); contestantAdapter.addItem(inputTxt, addAtIndex); listView1.smoothScrollToPosition(addAtIndex); } public void clearContestantList(View view){ //TODO clear out all contestants from the list contestantAdapter.clearAllItems(); } . . .
I'm having an issue with a list view I'm using. Each item in the listview has two buttons that users can use to add or take away points from each contestant.
The issue is when I add a new item to the list view or when scrolling (after having added at least one item) the code randomly takes the text out of the buttons on one or more of the items.
Can any one tell me A) what is causing this? and B) how to solve it?
I've included what I believe to be the relevant code. Let me know if you think I need to put some more up here.
My adapter class:
public class ContestantListAdapter extends BaseAdapter { private ArrayList<ContestantItem> contestantList; private LayoutInflater layoutInflater; public ContestantListAdapter(Context context, ArrayList<ContestantItem> contestantList){ this.contestantList = contestantList; layoutInflater = LayoutInflater.from(context); } @Override public int getCount(){ return contestantList.size(); } public View getView(int position, View convertView, ViewGroup parentView){ ViewHolder viewHolder; if(convertView == null){ convertView = layoutInflater.inflate(R.layout.contestant_row_layout,null);//null might need to be activity_main viewHolder = new ViewHolder(); viewHolder.nameView = (TextView) convertView.findViewById(R.id.nameView); viewHolder.pointsView = (TextView) convertView.findViewById(R.id.pointsView); convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder)convertView.getTag(); } viewHolder.nameView.setText((contestantList.get(position).getName())); viewHolder.pointsView.setText(Integer.toString(contestantList.get(position).getPoints())); //get button position Button button =(Button) convertView.findViewById(R.id.Minus); button.setOnClickListener(minusListener); Button buttonPlus = (Button) convertView.findViewById(R.id.Plus); buttonPlus.setOnClickListener(addListener); return convertView; } private View.OnClickListener minusListener = new View.OnClickListener() { @Override public void onClick(View v) { View parentRow = (View) v.getParent(); ListView listView = (ListView) parentRow.getParent(); final int position = listView.getPositionForView(parentRow); ContestantItem tmpContestant = (ContestantItem) contestantList.get(position); int tmpPoints = tmpContestant.getPoints()-1; if(tmpPoints<0){tmpPoints=0;} tmpContestant.setPoints(tmpPoints); notifyDataSetChanged(); } }; private View.OnClickListener addListener = new View.OnClickListener() { @Override public void onClick(View v) { View parentRow = (View) v.getParent(); ListView listView = (ListView) parentRow.getParent(); final int position = listView.getPositionForView(parentRow); ContestantItem tmpContestant = (ContestantItem) contestantList.get(position); int tmpPoints = tmpContestant.getPoints()+1; tmpContestant.setPoints(tmpPoints); notifyDataSetChanged(); } }; static class ViewHolder { TextView nameView; TextView pointsView; } @Override public Object getItem(int index){ return contestantList.get(index); } @Override public long getItemId(int index){ return index; } //this method should add the contestent to the listView. must know what position to add it in. public void addItem(String name, int index){ ContestantItem tempContestant = new ContestantItem(); tempContestant.setName(name); tempContestant.setPoints(1); contestantList.add(index,tempContestant); notifyDataSetChanged(); } . . .
ContestantRowLayout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="0.15" android:id="@+id/Minus" android:text="@string/minus" /> <TextView android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="0.5" android:id="@+id/nameView" /> <TextView android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="0.2" android:id="@+id/pointsView" /> <Button android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="0.15" android:id="@+id/Plus" android:text="@string/plus" /> </LinearLayout>
Main Activity:
public class MainActivity extends AppCompatActivity implements ContestantsFragment.OnFragmentInteractionListener{ ListView listView1; ContestantListAdapter contestantAdapter; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList<ContestantItem> rowDetails = getListData(); listView1 = (ListView) findViewById(R.id.contactsListView); listView1.setAdapter(new ContestantListAdapter(this,rowDetails)); contestantAdapter = (ContestantListAdapter) listView1.getAdapter(); } private ArrayList getListData(){ ArrayList<ContestantItem> contestantList = new ArrayList<ContestantItem>(); ContestantItem cItem; for(int i=0;i<10;i++){ cItem = new ContestantItem(); cItem.setName("Danny Anderson"); cItem.setPoints(5); contestantList.add(cItem); } return contestantList; } @Override public void onFragmentInteraction(Uri uri) { } //if the name is found returns the index position in the list. If not found -99 public void findAlphabetical(View view){ //TODO implement Binary search algorithm. hideKeyboard(); EditText tmpEditTxt = (EditText) findViewById(R.id.searchField); String inputTxt = tmpEditTxt.getText().toString(); int findAtIndex =contestantAdapter.searchAlphabetical(inputTxt)-1; if(findAtIndex <0){findAtIndex=0;} listView1.smoothScrollToPosition(findAtIndex); } public void addInAlpabetOrder(View view){ //TODO add the name in alphabetical order hideKeyboard(); //ListView tmpListView = (ListView) contestantsFragment.getView().findViewById(R.id.contactsListView); EditText tmpEditTxt = (EditText) findViewById(R.id.searchField); String inputTxt = tmpEditTxt.getText().toString(); int addAtIndex =contestantAdapter.searchAlphabetical(inputTxt); contestantAdapter.addItem(inputTxt, addAtIndex); listView1.smoothScrollToPosition(addAtIndex); } public void clearContestantList(View view){ //TODO clear out all contestants from the list contestantAdapter.clearAllItems(); } . . .
原文:https://stackoverflow.com/questions/34101401
最满意答案
您应该使用开源解决方案OpenBravo ERP来实现此目的。 这是下载链接OpenBravo ERP下载 。 我为我的客户定制了这个,它是用JAVA编写的开源解决方案,非常容易定制。
You should use open source solution OpenBravo ERP for this purpose. Here is its download link OpenBravo ERP Download. I customized this for one of my client it is open source solution written in JAVA and very easy to customise.
相关问答
更多-
有用JAVA做的ERP系统吗[2023-06-23]
有,但很少,用JAVA做的ERP在功能方面应该不能满足,用JAVA开放OA这些功能相对简单的比较好。 -
那里有java的ERP管理系统视频啊!!![2023-08-07]
http://www.verycd.com/topics/2758880/ 看看这个是不是你要的 -
问题是,你真的需要开源吗? 或者只是没有成本? 如果是后者,那么Windows Sharepoint Services(WSS)是一个相当不错的DMS。 它是免费的微软,你可以编写.NET代码来增强它,如果你想。 它没有Office Sharepoint的所有东西,但它很不错。 The question is, do you really need open source? Or just no cost? If the latter, then Windows Sharepoint Services (W ...
-
Jetty是一个可嵌入的开源应用程序服务器(例如,它在手机上运行时具有较低的内存占用空间等)。 Jetty is an open-source application server that is embeddable (i.e. it has a low memory footprint as it runs on mobile phones, etc.).
-
开源Java CMS [关闭](Open Source Java CMS [closed])[2022-08-09]
首先,我可以断言,这是一个完全合理的问题,因为CMS Web技术的重要性,它运行着WWW的很大一部分,并且被许多Java感知的人正确地询问,因为所有的许多(仍然受欢迎)基于PHP的系统,如Drupal,Wordpress和Joomla,包括: 无法很好地与图形统一建模语言(UML)工程集成 大量使用自由泳字符串作为哈希数组键(而不是作为系统范围的共享预定义字符串常量),将数据结构作为难以置信的惯例,以hashmaps的形式进行掩埋,并将所有的“Don” t重复自己(DRY)原则,而不是使用面向对象的可重用封 ... -
如果您是主流用户,可能对您没有直接的好处。 但是,Java的开源基础使人们更容易使其适应闭源供应商认为不需要支持的更多利基要求。 较小的供应商(或开源项目)可以提供满足这些特殊需求的解决方案。 例如,Java运行在各种各样的平台和操作系统上,其中大部分都是由Sun以外的公司支持的(被授予,即使在开源之前也是如此)。 有任何专业人士注意到自改变以来有任何重大差异 我喜欢Linux发行版现在包含“官方”Sun JVM和JDK,而不是单独安装它或使用提供的“大多数兼容”替代实现。 If you are a mai ...
-
WebApp如何与数十家公司的ERP系统集成?(How can a WebApp integrate with ERP-systems of dozens of firms? [closed])[2022-05-18]
那么,帕特里克15079 ...就是说,温和地说,这是一个非常广泛的问题! 如果确实没有标准格式 ,那么最好的办法是创建一个中间层,将ERP输入转换为您设计的某种标准格式。 然后,您的B2B Marketplace可以使用已经通过管道传输到中间层的任何系统中的信息。 很可能业内人士试图制定标准,您可以利用该标准 - 根据您的需求进行调整。 然后,对于每个不同的ERP系统,您还需要创建插入中间层的低级转换器。 仔细记录所有这些内容,理论上,一些ERP供应商(或顾问)可以构建自己的翻译器并允许将它们插入您的系统 ... -
AFAIK Xopus是目前唯一支持的基于XSD的Web编辑器。 它不是开源的,而是专为您定位的最终用户设计的。 它需要XML,XSD和XSL来创建友好且100%验证的WYSIWYG编辑UI。 看看演示 ,看看有什么可能。 免责声明:我在Xopus工作。 AFAIK Xopus is the only XSD based Web Editor currently supported. It is not open source, but is especially designed for the end ...
-
当然, Java消息传递与IBM互操作。 在IBM方面,您需要能够与MQSeries交谈。 尝试从这个DeveloperWorks网站开始。 Sure, Java Messaging interoperates with IBM. On the IBM side you need to be able to talk to MQSeries. Try starting with this DeveloperWorks site.
-
您应该使用开源解决方案OpenBravo ERP来实现此目的。 这是下载链接OpenBravo ERP下载 。 我为我的客户定制了这个,它是用JAVA编写的开源解决方案,非常容易定制。 You should use open source solution OpenBravo ERP for this purpose. Here is its download link OpenBravo ERP Download. I customized this for one of my client it is ...