2017-02-01 5 views
0

ListView内のTextViewに表示するパスワードの説明を取得しようとしています。誰かが私がやるべきことを私に助けてくれる?ListViewのArrayAdapterへの複数の要素

この時点で、ArrayAdapterではなくArrayListを使用する必要があると思いますか?

here is a sample image

答えて

0

あなたの問題はここにある:ここ

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

あなたは、アダプタにID "R.id.password" を割り当てるここ

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(); 

はサンプル画像です。したがって、アダプターはあなたが与えたIDでテキストを設定します。だからあなたのパスワードの説明は何も得ることができないでしょう。説明するには、カスタムアダプターを作成する必要があります。

2

タスクリストを調整して、パスワードとその関連説明を格納する2つの文字列フィールドを持つオブジェクトが含まれるようにする必要があります。たとえば :

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

次に、あなたはそれであなたのタスクリストを埋める:あなたのmAdapterフィールドのために、あなたはArrayAdapterクラスを拡張し、そのgetViewメソッド(int型、ビューをオーバーライドする必要が

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)); 
    } 

、後、の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; 
    } 
} 
+0

。それが動作するかどうか私に見てみましょう。ありがとうございました。 –

関連する問題