gpt4 book ai didi

java - ListView 不从 OnClickListener 重绘

转载 作者:行者123 更新时间:2023-11-30 09:43:01 26 4
gpt4 key购买 nike

我有一个按钮,它应该使用自定义适配器对 ListView 进行排序。按钮监听器如下:

collectionSort.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sortOrder = -sortOrder;
Log.d("DBGINF", "Sort Order: " + sortOrder);
m.sortByTitles(sortOrder);
m.notifyDataSetChanged();
});

排序顺序按应有的方式更改,并且数组中的数据已排序,但实际 View 永远不会被 notifyDataSetChanged 更新。这是 sortByTitles() 方法:

public void sortByTitles(int dir) {
if (dir > 0) {
sort(new StringComparator());
} else if (dir < 0) {
sort(new ReverseStringComparator());
}
Log.d("Data Set: ", this.getItem(0) + "");
}

notifyDataSetChanged() 永远不会更新 OnClickListener 内的 View ,但总是在外部 OnClickListener 之一正常工作。我做错了什么?

编辑

这是适配器类中的构造函数:

public MainMenuArrayAdapter(Context context, CollectionObject[] objects) {
super(context, R.layout.mainmenurow, objects);
myContext = context;
}

下面是它是如何在 Activity 的 onCreate() 方法中初始化的:

CollectionObject[] sample = new CollectionObject[] { c1, c2, c3, c4, c5, c6 };
final MainMenuArrayAdapter m = new MainMenuArrayAdapter(this, sample);

其中 c1 - c6 只是一些用于测试的临时 CollectionObjects。

最佳答案

您要扩展哪种适配器?我认为您实际上是在对适配器中的项目副本进行排序。您是否再次在适配器中设置 myCollections ?顺便说一句,如果你扩展 ArrayAdapter 请记住适配器本身有一个接受你自己的比较器的排序方法。

编辑:我用自定义适配器和排序写了一个 super 愚蠢的例子,你可以按菜单对项目进行排序。希望对您有所帮助!

public class SortActivity extends ListActivity {

private StupidAdapter stupidAdapter;
private List<String> list = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

list.add("Android-a");
list.add("Android-b");
list.add("Android-c");
list.add("BlackBerry-a");
list.add("BlackBerry-b");
list.add("BlackBerry-c");
list.add("Windows-a");
list.add("Windows-b");
list.add("Windows-c");

// keep in mind that from now on whatever changes you do on list is not reflected on the adapter.
// the array adapter keeps its own internal data
stupidAdapter = new StupidAdapter(this, list);
setListAdapter(stupidAdapter);

}

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
new MenuInflater(this.getApplication()).inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
final int id = item.getItemId();
if (id == R.id.menu_main_sort_az) {
stupidAdapter.sort(new Comparator<String>() {
public int compare(String object1, String object2) {
return object1.compareTo(object2);
};
});
} else if (id == R.id.menu_main_sort_za) {
stupidAdapter.sort(new Comparator<String>() {
public int compare(String object1, String object2) {
return object2.compareTo(object1);
};
});
}
stupidAdapter.notifyDataSetChanged();
return true;
}

class StupidAdapter extends ArrayAdapter<String> {

public StupidAdapter(Context context, List<String> list) {
super(context, R.layout.list_item, list);
}

@Override
public View getView(final int position, View row, final ViewGroup parent) {
ViewHolder viewHolder;

final String item = getItem(position);

if (row == null) {
row = getLayoutInflater().inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder(row);

row.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) row.getTag();
}

viewHolder.refreshData(item);

return row;
}

class ViewHolder {
TextView title;
public ViewHolder(View row) {
this.title = (TextView) row.findViewById(R.id.title);
}
public void refreshData(final String text) {
this.title.setText(text);
}
}

}

}

list_item.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffffff">

<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="15sp"
android:textColor="#ff000000"/>

</LinearLayout>

main_menu.xml(在 res/menu 下)

<menu xmlns:android="http://schemas.android.com/apk/res/android">   
<item android:id="@+id/menu_main_sort_az" android:title="Sort A-Z"/>
<item android:id="@+id/menu_main_sort_za" android:title="Sort Z-A"/>
</menu>

关于java - ListView 不从 OnClickListener 重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8440836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com