当我点击 Android 列表视图标题时如何取消排序列表?
我有一个列表视图,我为它创建了标题(通过使用 addHeader(layout)),我的标题有文本视图,我为标题中的文本视图之一创建了单击事件,并在事件中编写了排序代码。
package com.sample;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import android.R.array;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class SampleActivity extends Activity {
/** Called when the activity is first created. */
List<String> ls;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lst=(ListView)findViewById(R.id.bookListView);
View v = getLayoutInflater().inflate(
R.layout.header, null);
lst.addHeaderView(v);
lst.setAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, COUNTRIES));
ls=Arrays.asList(COUNTRIES);
// Collections.sort(ls);
TextView bt=(TextView)v.findViewById(R.id.item2);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Arrays.sort(COUNTRIES);
lst.invalidateViews();
}});
static final String[] COUNTRIES = new String[] {
"Afghanistan", "japan", "China", "Barma",
};
}
它工作正常,但当我再次单击标题时,我想从排序列表中返回未排序列表,所以我该如何做这个请帮助我。
谢谢, @唠叨。
I've a list view and i created header (By using addHeader(layout)) to it, and my header having textviews and i created click event for one of the textviews in header and writen sorted code in the event.
package com.sample;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import android.R.array;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class SampleActivity extends Activity {
/** Called when the activity is first created. */
List<String> ls;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lst=(ListView)findViewById(R.id.bookListView);
View v = getLayoutInflater().inflate(
R.layout.header, null);
lst.addHeaderView(v);
lst.setAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, COUNTRIES));
ls=Arrays.asList(COUNTRIES);
// Collections.sort(ls);
TextView bt=(TextView)v.findViewById(R.id.item2);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Arrays.sort(COUNTRIES);
lst.invalidateViews();
}});
static final String[] COUNTRIES = new String[] {
"Afghanistan", "japan", "China", "Barma",
};
}
And its working fine but i want to back unsort list from sorted list when i click again on the header so how can i do this one plz help me.
Thanks,
@nag.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在初始化适配器时更改您在适配器中设置的数据(设置未排序的数据)并使用
adapter.notifyDataSetChanged()
来获得效果。change the data(set unsorted data) which you set in the adapter while inititalizing it and use
adapter.notifyDataSetChanged()
to get the effect.