分割它传递的数据,但不更新列表适配器

输血量

我一个有两个片段Activity当我单击片段A上的按钮时,对象将通过托管两者的接口实现传递到片段B。Activity它确实到达了Fragment B,但是列表没有得到更新,它保持为空。

我已经尝试过notifyDataSetChanged()以各种可能的方式放置

片段B:

public class HistoryFragment extends ListFragment
 {

private static PasswordAdapter adapter ;
private static List<Password> historyList = new ArrayList<Password>();  

 //This is the object I get from Fragment A via the activity interface implementation
public static void addToData( Password addToList ) 
{ 
historyList.add( addToList );
}

@Override
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState);
historyList = Password.getTriedPasswords();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup   container, Bundle savedInstanceState)        { 

View view = inflater.inflate(R.layout.fragment_history, container, false);
adapter = new PasswordAdapter( historyList );
setListAdapter(adapter);
return view;
}

private class PasswordAdapter extends BaseAdapter
{
private ArrayList<Password> data; 

public PasswordAdapter( List<Password> historyList ) 
{
    data = new ArrayList<Password>();
    notifyDataSetChanged();
    data.addAll( historyList );
}

@Override
public int getCount() 
{
    return data.size();
}

@Override
public Password getItem( int position ) 
{
    return data.get( position );
}

@Override
public long getItemId(int position) {
    // TODO implement you own logic with ID
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final View result;

    if (convertView == null) 
    {
        result = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_list_item, parent, false);
    } 
    else 
    {
        result = convertView;
    }

    Password item = getItem( position );

    ( (TextView) result.findViewById(R.id.historyPasswordTextView) ).setText(item.getPasswordString());
    ( (TextView) result.findViewById(R.id.historyDateTextView) ).setText(item.getPasswordString());

    return result;
  }
 }
先生

将项目添加到列表时,不会通知您的适配器。尝试在您的片段中执行此操作

public void addToData( Password addToList ) 
{       
     historyList.add(addToList);
     adapter.updateList(historyList);
}

在您的适配器中创建一个新方法

public void updateList(ArrayList<Password> newData){
    data.clear();
    for(int i = 0; i < newData.size(); i++){
        data.add(newData.get(i));
    }
    notifyDataSetChanged();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何更新适配器数据列表?

如何更新RecyclerView适配器数据?

将列表视图的数据从Asynctask传递到自定义列表适配器类

自定义listview适配器中的clear()也会更新列表数据

适配器数据更改时如何更新可扩展列表视图

从另一个适配器中更新适配器数据集

RecyclerView 适配器不使用数据绑定更新数据

如何将数据从片段传递到适配器

Android - 在数据绑定适配器中传递视图

从回收站适配器的单击功能传递数据

将数据从 Recyclerview 适配器传递到 Fragment

如何从Recyclerview适配器的片段之间传递数据

Kotiln:将数据从适配器传递到活动

将数据从onPostExecute()传递给适配器

如何在Android适配器中传递Json数据

如何将数据从适配器传递到片段?

从适配器更新recyclerview

更新ArrayList适配器

从适配器更新ActionBar

如何刷新/更新listView适配器中的数据

Json数据适配器

SqlHelper数据适配器

如何使用自定义列表适配器中的“更新图像”按钮更新数据库中的值?

如何从自定义列表视图适配器获取数据并传递给意图添加额外的内容?

在visual-studio android sdk中,当适配器数据发生变化时更新列表视图

在RecycleViewer适配器中传递参数

如何向列表视图中的每个按钮添加一个 onclick,填充自定义数组适配器,从数组中删除该项目并更新它?

卡列表视图的多个适配器

无法从适配器内部更新imageButton