首页 \ 问答 \ 在列表视图,Android中滚动或添加项目时按钮文本消失(button text disappears when scrolling or adding and item in listview, Android)

在列表视图,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
更新时间:2023-02-19 13:02

最满意答案

您应该使用开源解决方案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在功能方面应该不能满足,用JAVA开放OA这些功能相对简单的比较好。
  • 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.).
  • 首先,我可以断言,这是一个完全合理的问题,因为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 ...
  • 那么,帕特里克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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)