ListView的ArrayAdapter的多个元素

时间:2017-02-01 12:26:21

标签: android listview android-arrayadapter

我一直在尝试将密码描述显示在ListView中的TextView中 - 没有运气。有人能帮助我做我需要做的事吗?

此时您认为我需要使用ArrayList而不是ArrayAdapter吗?

private static TextView mPasswordView;
private String mPassword;
private TextView mPasswordDesc;
private Button mGeneratepas;
private ListView mPasswordListView;
private ArrayAdapter<String> mAdapter;
private PasswordDBHelper mHelper;
private PasswordGenerator mPasswordGenerator = new PasswordGenerator();


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

    mHelper = new PasswordDBHelper(this);
    mPasswordView = (TextView) findViewById( R.id.textPassword );
    mGeneratepas = (Button) findViewById( R.id.buttonGenpas );
    mPasswordListView = (ListView) findViewById(R.id.list_password);
    updateList();


    mGeneratepas.setOnClickListener( new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            mPassword = mPasswordGenerator.generateSessionKey(8);
            mPasswordView.setText(mPassword);
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_add_task:
            final EditText passdescEditText = new EditText(this);
            AlertDialog dialog = new AlertDialog.Builder(this)
                    .setTitle("Add Password to List")
                    .setMessage("Saving Password:  " + mPassword)
                    .setView(passdescEditText)
                    .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String password = String.valueOf(mPassword);
                            String passdesc = String.valueOf(passdescEditText.getText());
                            SQLiteDatabase db = mHelper.getReadableDatabase();
                            ContentValues values = new ContentValues();
                            values.put (PasswordContract.PasswordEntry.COL_PASSWORD, password);
                            values.put (PasswordContract.PasswordEntry.COL_PASS_DESC, passdesc);
                            db.insertWithOnConflict(PasswordContract.PasswordEntry.TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
                            db.close();
                            updateList();
                        }
                    })
                    .setNegativeButton("Cancel", null)
                    .create();
            dialog.show();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }

}



private void updateList(){
       ArrayList<String> tasklist = new ArrayList<>();
       SQLiteDatabase db = mHelper.getReadableDatabase();
       Cursor cursor = db.query( PasswordContract.PasswordEntry.TABLE, new String[]{PasswordContract.PasswordEntry._ID, PasswordContract.PasswordEntry.COL_PASSWORD, PasswordContract.PasswordEntry.COL_PASS_DESC},
               null, null, null, null, null);
       while (cursor.moveToNext()){
           int idx = cursor.getColumnIndex(PasswordContract.PasswordEntry.COL_PASSWORD);
           tasklist.add(cursor.getString(idx));
       }

       if (mAdapter == null){
           mAdapter = new ArrayAdapter<>(this,
                   R.layout.item_password,
                   R.id.password,
                   tasklist);
           mPasswordListView.setAdapter(mAdapter);
       }else{
           mAdapter.clear();
           mAdapter.addAll(tasklist);
           mAdapter.notifyDataSetChanged();
       }
       cursor.close();
       db.close();

这是示例图片:

here is a sample image

2 个答案:

答案 0 :(得分:2)

您需要调整您的任务列表,使其包含具有2个字符串字段的对象,用于存储密码及其相关描述。 例如:

class PasswordItem {
    String password;
    String description;
    PasswordItem(String password, String description){
        this.password = password;
        this.description= description;
}

然后用它填写你的任务清单:

while (cursor.moveToNext()){
    String password = cursor.getString(cursor.getColumnIndex(PasswordContract.PasswordEntry.COL_PASSWORD));
    String description = cursor.getString(cursor.getColumnIndex(PasswordContract.PasswordEntry.COL_PASS_DESC));
    tasklist.add(new PasswordItem(password,description));
   }

之后,对于mAdapter字段,您需要扩展ArrayAdapter类并覆盖其getView(int,View,ViewGroup)方法以返回所需的自定义视图类型(此方法将调用以生成每行你的ListView)。

例如:

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

    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_password, parent, false);
    }

    TextView passwordView = (TextView) convertView.findViewById(R.id.password);
    TextView passwordDescriptionView = (TextView) convertView.findViewById(R.id.description); // Use the id your chose for the password description view

    // We are extracting the PasswordItem at the right 'position' from the tasklist
    PasswordItem passwordItem = getItem(position);

    // Now can set the texts for each view
    passwordView.setText(passwordItem.password);
    passwordDescriptionView.setText(passwordItem.description);

    // Return the completed view to render on screen
    return convertView;
    }
}

答案 1 :(得分:0)

你的问题在这里:

mAdapter = new ArrayAdapter<>(this,
               R.layout.item_password,
               R.id.password,
               tasklist);

这里你要为适配器分配id“R.id.password”。因此适配器将在您给出的id中设置文本。所以你的密码描述将无法获得任何东西。要使用说明,您需要制作自定义适配器。