【问题标题】:Sorting two arrays with arrayadapter, files and files description使用arrayadapter、文件和文件描述对两个数组进行排序
【发布时间】:2013-04-01 02:02:49
【问题描述】:

我有 2 个 String[] 数组:
字符串[] mFileList;
字符串[] mFileList_description;
这是我的函数,它显示带有文件描述列表的警报对话框:

private void Load_file_list(){
Dialog dialog = null;
AlertDialog.Builder alertdg = new AlertDialog.Builder(this); 
alertdg.setTitle("FILES");
alertdg.setTitle("Select file");

final ArrayAdapter<String> adapter;
final EditText filtr = new EditText(this); 
final ListView lista = new ListView(this); 

 adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, mFileList_description);
 lista.setAdapter(adapter);
/////////
 filtr.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable arg0) {
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }
    @Override
    public void onTextChanged(CharSequence cs, int start, int before,
            int count) {
        // sort
        adapter.getFilter().filter(cs);
        adapter.notifyDataSetChanged();
    } 
 });
 lista.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Log.d("TEST","File id:"+arg2+" File name:"+mFileList[arg2]);
    }
   });
 /////////

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(filtr); 
layout.addView(lista); 
alertdg.setView(layout); 
alertdg.show(); 
}

我有问题,当我对描述列表进行排序时,因为 setOnItemClickListener 方法返回用户单击的描述的 id,但它不是数组 mFileList 中的 id(有时这个 id 来自另一个文件)

如何使用描述对我的文件进行排序?我厌倦了在二维数组中这样做,但我认为另一种方式会更好......

谢谢,对不起我的英语...... ;)

【问题讨论】:

    标签: android arrays sorting android-arrayadapter


    【解决方案1】:

    首先,对于大多数人来说,你在这里遵循了一个错误的模式

    优化方案:

    我建议您保留 1 个 ArrayArrayList 的用户定义对象类型,而不是保留 2 个数组。

    例如:

    /**
    Here `MyFileClass` is a clsss with attributes whatever you want to store for 
    Files and also contains getter/setter methods **/
    
    MyFileClass[] objFiles = new MyFileClass[4];
    or
    ArrayList<MyFileClass> listFileObjs = new ArrayList<MyFileClass>();
    

    但我还是建议您使用ArrayList,因为与Array 相比,它易于管理。

    现在,由于您拥有单个 ArrayList&lt;MyFileClass&gt;,您可以通过执行任何您想要的操作轻松管理它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多