2013-10-30 70 views
0

到目前为止,我可以通过其字符串ArrayAdapter排序listview事情。但是现在,随着我想要进步,我希望能够通过CustomAdapter来排序listviewAndroid CustomAdapter ListView排序

这里就是我到目前为止已经试过:

MainActivity

String[] text, price; 
ArrayList<String> priceList; 

private DBHelper dbHelper; 
SimpleCursorAdapter dataAdapter; 
Cursor cursor; 

MyCustomAdapter adapter; 

Button back, filter; 
TextView highest, lowest, location; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.viewhouseandland); 

    initControls(); 

    displayRecords(); 
} 

private void displayRecords() { 
    // TODO displayRecords 
    // TODO CheckDBConnection 
    checkDatabaseConnection(); 

    text = dbHelper.getAll(); 
    price = dbHelper.getAllPrices(); 

    adapter = new MyCustomAdapter(imgs, text, price); 

    lv.setAdapter(adapter); 

} 

    @SuppressLint("InlinedApi") 
private void displayDialog() { 
    // TODO displayDialog 
    final ArrayAdapter<String> adp = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, sortBy); 

    LayoutInflater li = LayoutInflater.from(this); 
    View promptsView = li.inflate(R.layout.dialog_layout, null); 

    promptsView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT)); 

    final Spinner mSpinner= (Spinner) promptsView 
      .findViewById(R.id.spDialog); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 

    builder.setTitle("Sort By..."); 
    builder.setIcon(R.drawable.launcher); 

    mSpinner.setAdapter(adp); 
    mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

    public void onItemSelected(AdapterView<?> parent, View v, 
      int pos, long id) { 
     strSpinner = mSpinner.getSelectedItem().toString(); 

     if(strSpinner.equals("Highest Price")){ 
      highest.setTypeface(Typeface.DEFAULT_BOLD); 
      lowest.setTypeface(Typeface.DEFAULT); 
      location.setTypeface(Typeface.DEFAULT); 
      price = dbHelper.sortHighestPrice(); 

      adapter = new MyCustomAdapter(imgs, text, price); 
      lv.setAdapter(adapter); 
      adapter.notifyDataSetChanged(); 

     } else if (strSpinner.equals("Lowest Price")){ 
      highest.setTypeface(Typeface.DEFAULT); 
      lowest.setTypeface(Typeface.DEFAULT_BOLD); 
      location.setTypeface(Typeface.DEFAULT); 
      price = dbHelper.sortLowestPrice(); 

      adapter = new MyCustomAdapter(imgs, text, price); 
      lv.setAdapter(adapter); 
      adapter.notifyDataSetChanged(); 
     } else if (strSpinner.equals("Location")) { 
      highest.setTypeface(Typeface.DEFAULT); 
      lowest.setTypeface(Typeface.DEFAULT); 
      location.setTypeface(Typeface.DEFAULT_BOLD); 
     } else { 
      Log.d("Default", "Default"); 
     } 
    } 

    public void onNothingSelected(AdapterView<?> arg0) { 

    } 
    }); 

    builder.setPositiveButton("Okay", 
      new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      dialog.dismiss(); 
     } 
    }); 
    builder.setNegativeButton("Cancel", 
      new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      dialog.dismiss(); 
     } 
    }); 

    builder.setView(promptsView); 
    AlertDialog alert = builder.create(); 
    alert.show(); 

    //((Button)alert.findViewById(android.R.id.button1)).setBackgroundResource(R.drawable.custom_button); 
    //((Button)alert.findViewById(android.R.id.button2)).setBackgroundResource(R.drawable.custom_button); 
} 

    class MyCustomAdapter extends BaseAdapter 
{ 
    String[] data_text1; 
    String[] data_text2; 
    int[] data_image; 

MyCustomAdapter() { 
    data_text1 = null; 
    data_text2 = null; 
    data_image = null; 
} 

MyCustomAdapter(int[] image, String[] house, String[] price) { 
    data_text1 = house; 
    data_text2 = price; 
    data_image = image; 
} 

public int getCount() { 
    return data_text1.length; 
} 

public String getItem(int position) { 
    return null; 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 

    LayoutInflater inflater = getLayoutInflater(); 
    View row; 

    row = inflater.inflate(R.layout.listrow, null); 

    TextView textview1 = (TextView) row.findViewById(R.id.text1); 
    TextView textview2 = (TextView) row.findViewById(R.id.text2); 
    ImageView imageview = (ImageView) row.findViewById(R.id.image); 

    imageview.setScaleType(ImageView.ScaleType.FIT_XY); 
    textview1.setText(data_text1[position]); 
    textview2.setText(data_text2[position]); 
    imageview.setImageResource(data_image[position]); 

    return (row); 

    } 
} 

DBHelper

public String[] getAll(){ 
      Cursor localCursor = 
        this.myDataBase.query(DB_TABLE, new String[] { 
         KEY_ID, KEY_HOUSE, KEY_PRICE }, null, null, null, null, null); 
      String[] array = new String[localCursor.getCount()]; 
      int i = 0; 
      while(localCursor.moveToNext()){ 
       String uname = localCursor.getString(localCursor.getColumnIndex(DBHelper.KEY_HOUSE)); 
       array[i] = uname; 
       i++; 
      } 
      return array; 
     } 

     public String[] getAllPrices(){ 
      Cursor localCursor = 
        this.myDataBase.query(DB_TABLE, new String[] { 
         KEY_ID, KEY_HOUSE, KEY_PRICE }, null, null, null, null, null); 
      String[] array = new String[localCursor.getCount()]; 
      int i = 0; 
      while(localCursor.moveToNext()){ 
       String uname = localCursor.getString(localCursor.getColumnIndex(DBHelper.KEY_PRICE)); 
       array[i] = uname; 
       i++; 
      } 
      return array; 
     } 

     public String[] sortHighestPrice(){ 
      Cursor localCursor = 
        this.myDataBase.query(DB_TABLE, new String[] { 
         KEY_ID, KEY_HOUSE, KEY_PRICE }, 
         null, null, null, null, 
         KEY_PRICE + " DESC"); 
      String[] array = new String[localCursor.getCount()]; 
      int i = 0; 
      while(localCursor.moveToNext()){ 
       String uname = localCursor.getString(localCursor.getColumnIndex(DBHelper.KEY_PRICE)); 
       array[i] = uname; 
       i++; 
      } 
      return array; 
     } 

     public String[] sortLowestPrice(){ 
      Cursor localCursor = 
        this.myDataBase.query(DB_TABLE, new String[] { 
         KEY_ID, KEY_HOUSE, KEY_PRICE }, 
         null, null, null, null, 
         KEY_PRICE + " ASC"); 
      String[] array = new String[localCursor.getCount()]; 
      int i = 0; 
      while(localCursor.moveToNext()){ 
       String uname = localCursor.getString(localCursor.getColumnIndex(DBHelper.KEY_PRICE)); 
       array[i] = uname; 
       i++; 
      } 
      return array; 
     } 

基本上,我的应用功能:

  • 它显示
  • 当用户点击过滤器按钮选择从最高价格最低的价格和位置,该应用应该通过根据用户的选择进行排序响应数据(图像,标题和字幕)的列表(排序最高,最低或位置)...

因此,这些数据来自本地db(sqlite)

没有错误,到目前为止,但它只是在列表未更新/排序。我在代码中错过了什么?有任何想法吗?我真的需要帮助。谢谢。

更新:我得到它的工作,但现在的问题是价格是一个字符串,当我整理由最高的,它给了900,而不是1000我该如何处理呢?

+0

是您从'sortHighestPrice()'和'sortLowestPrice()'正确排序得到的数据? **编辑**:似乎查询是正确的,所以问题可能是该列表不更新。 –

+0

@JeffreyKlardie我已更新我的代码,请查看我的更新说明。 – Dunkey

+0

我建议将价格设为数字字段,例如整数并存储美分。然后检索,这使排序更容易。另一种解决方案是将字符串排序为整数:“CAST(value AS INTEGER)DESK”,其中value是列的名称。 –

回答

1

This答案给出了一个很好的概述:

两个选择:要么不放,你 传递到构造函数的ArrayList的引用,以便您可以修改实际列表数据 后(由于该表ISN不会复制,修改适配器外部的数据仍会更新适配器所引用的指针),或者重写适配器以允许将列表重置为另一个对象。

无论哪种情况,ArrayList发生更改后,必须调用 notifyDataSetChanged()以更新您的ListView。这个 可以在适配器内部或外部完成。

在我看来

干净的方法是创建一个像updateData()内MyCustonAdapter的方法:

public void updateData(int[] image, String[] house, String[] price) { 
    data_text1 = house; 
    data_text2 = price; 
    data_image = image; 
    notifyDataSetChangeD(); 
}