我可以同時爲2個條件排序我的列表視圖嗎?我有4行:標題,描述,圖像的url和pinned.I必須顯示記錄在頂部固定== true,然後我必須排序字母標題行,在顯示後固定==真正的lines.How可以做到這一點?MainActivity和適配器放在下面。 MainActivity:爲2個條件排序列表視圖
public class MainActivity extends ListActivity {
private Context context;
SqlHelper dbHelper;
Intent intent;
private static String url = "https://fierce-citadel-4259.herokuapp.com/hamsters";
private static final String TITLE = "title";
private static final String DESCRIPTION = "description";
private static final String IMAGE = "image";
private static final String PINNED = "pinned";
ArrayList<HashMap<String,String>> jsonlist = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String,String>> bdList = new ArrayList<HashMap<String, String>>();
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actbar = getActionBar();
actbar.setDisplayShowHomeEnabled(false);
actbar.setDisplayShowTitleEnabled(false);
actbar.setDisplayUseLogoEnabled(false);
lv=(ListView) findViewById(android.R.id.list);
LayoutInflater mInflater =LayoutInflater.from(MainActivity.this);
View mcustomview = mInflater.inflate(R.layout.customactionbar, null);
ImageButton imgbutt =(ImageButton)mcustomview.findViewById(R.id.actbarimagebutton);
imgbutt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ProgressTask(MainActivity.this).execute();
}
});
actbar.setCustomView(mcustomview);
actbar.setDisplayShowCustomEnabled(true);
if(isNetworkConnected()==true) {
new ProgressTask(MainActivity.this).execute();
ForItemClick();
}
else {
try {
dbHelper = new SqlHelper(MainActivity.this);
WithoutInternetItemClick();
imgbutt.setClickable(false);
} catch (SQLException e) {
e.printStackTrace();
}
displaysavedlv();
}
}
private class ProgressTask extends AsyncTask<String,Void,Boolean> {
private ProgressDialog dialog;
private ListActivity activity;
private Context context;
public ProgressTask(MainActivity activity) {
this.activity = activity;
context = activity;
dialog = new ProgressDialog(context);
try {
dbHelper = new SqlHelper(MainActivity.this);
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void onPreExecute(){
this.dialog.setMessage("Progress start");
this.dialog.show();
}
protected void onPostExecute(final Boolean success){
try{
if((this.dialog != null)&& this.dialog.isShowing()){
this.dialog.dismiss();
}
if(isNetworkConnected()==true) {
CustomListAdapter adapter = new CustomListAdapter(MainActivity.this, jsonlist, R.layout.list_item, new String[]{TITLE, DESCRIPTION}, new int[]{R.id.title, R.id.description});
lv.setAdapter(adapter);
}else{
displaysavedlv();
}
}catch (final IllegalArgumentException e){e.printStackTrace();}
}
protected Boolean doInBackground(String... args) {
JSONParser jParser = new JSONParser();
JSONArray json = jParser.getJSONFromUrl(url);
for(int i =0;i<json.length();i++) {
try {
JSONObject c = json.getJSONObject(i);
String vtitle = c.getString(TITLE);
String vdescription = c.getString(DESCRIPTION);
String vimage = c.getString(IMAGE);
Boolean vpinned = c.getBoolean(PINNED);
dbHelper.open();
dbHelper.createEntry(vtitle, vimage, vdescription);
dbHelper.close();
HashMap<String, String> map = new HashMap<>();
map.put(TITLE, vtitle);
map.put(DESCRIPTION, vdescription);
map.put(IMAGE, vimage);
map.put(PINNED, String.valueOf(vpinned));
jsonlist.add(map);
} catch (JSONException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// There are no active networks.
return false;
} else
return true;
}
public void ForItemClick(){
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String title1 = jsonlist.get(position).get("title");
String description1 = jsonlist.get(position).get("description");
String url1 = jsonlist.get(position).get("image");
intent = new Intent(MainActivity.this, DetailInfo.class);
intent.putExtra("title", title1);
intent.putExtra("description", description1);
intent.putExtra("url", url1);
startActivity(intent);
}
});
};
private void displaysavedlv(){
bdList = dbHelper.getAllData();
lv=(ListView) findViewById(android.R.id.list);
CustomListAdapter adapter1 = new CustomListAdapter(MainActivity.this,bdList,R.id.list_item,new String[]{TITLE,DESCRIPTION},new int[]{R.id.title,R.id.description});
lv.setAdapter(adapter1);
}
public void WithoutInternetItemClick(){
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String title1 = bdList.get(position).get("title");
String description1 = bdList.get(position).get("description");
String url1 = bdList.get(position).get("image");
intent = new Intent(MainActivity.this, DetailInfo.class);
intent.putExtra("title", title1);
intent.putExtra("description", description1);
intent.putExtra("url", url1);
startActivity(intent);
}
});
}
}
適配器:
public class CustomListAdapter extends SimpleAdapter {
private static final String IMAGE = null;
private final Activity context;
private ArrayList<HashMap<String, String>> result;
public CustomListAdapter(Activity context, ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to) {
super(context,data,resource,from,to);
// TODO Auto-generated constructor stub
this.result = data;
this.context = context;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.list_item, null, true);
ImageView imageView = (ImageView) rowView.findViewById(R.id.image);
TextView title = (TextView) rowView.findViewById(R.id.title);
title.setText(result.get(position).get("title"));
TextView description = (TextView) rowView.findViewById(R.id.description);
description.setText(result.get(position).get("description"));
String url = result.get(position).get("image");
Picasso.with(context)
.load(url).into(imageView);
return rowView;
}
}
設置一個實現您業務規則的Comparator,然後使用該Comparator對您的模型集合進行排序()。 – CommonsWare