3

これが少し初心者の質問である場合は申し訳ありません。私は JS と CSS で Windows 8 アプリを作成することに慣れていますが、Java はまだあまり得意ではありません。

私は私の最初のアンドロイド アプリ (ノート テイカー) を作成中です。次のようにxmlで定義された標準のListViewがあります。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
    >

<ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/notesListView"
        android:fastScrollEnabled="true"
        android:dividerHeight="2dp"
        android:layout_alignParentStart="true"
        android:divider="#b5b5ae"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"

        />


</RelativeLayout>

メモ作成アプリは動作していますが、現在の UI はひどいものです。

個々のリストビュー項目にスタイルを適用しようとしていますが、どこでもそれを行うためのnoobsガイドが見つかりません。

リストに追加された新しいメモごとに、事前定義されたスタイル (余白など) が自動的に取得されるようにしたい。

XMLから個々のリストビュー項目にスタイルを適用する最良の方法を教えてください(マージンの設定など)。

また、個々のリストビュー項目を動的に変更する方法を知っている人はいますか(ユーザーがダイアログから特定の色を選択すると、その個々のリストビュー項目が BG 色を変更できるようにしたいなど)。

編集1:

こんにちは、私のlistNotesアクティビティのコードは次のとおりです。

   package com.fishingfon.notetakerui;


public class ListNotesActivity extends Activity {

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info =   (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    notes.remove(info.position);
    populateList();
    //populateLateCustomAdapter();

    return true;
}


@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_CANCELED){
        return;
    }
    Serializable extra = data.getSerializableExtra("Note");
    if (extra != null){

        Note newNote = (Note)extra;
        if (editingNoteId > -1){

            notes.set(editingNoteId, newNote);
            editingNoteId = -1;
        }
        else {

            notes.add(newNote);
        };
        populateList();
        //populateLateCustomAdapter();

    }

}



private List<Note> notes = new ArrayList<Note>();
private ListView notesListView;
private int editingNoteId = -1;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_notes);
    ListView notesListView = (ListView)findViewById(R.id.notesListView);

    notesListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int itemNumber, long id) {

            Intent editNoteIntent = new Intent(view.getContext(), EditNotesActivity.class);
            editNoteIntent.putExtra("Note", notes.get(itemNumber));
            editingNoteId = itemNumber;
            startActivityForResult(editNoteIntent, 1);


        }
    });

    registerForContextMenu(notesListView);

    notes.add(new Note("1 Note", "blah blah", new Date()));
    notes.add(new Note("2 Note", "blah blah", new Date()));
    notes.add(new Note("3 Note", "blah blah", new Date()));
    notes.add(new Note("4 Note", "blah blah", new Date()));
    notes.add(new Note("5 Note", "blah blah", new Date()));
    notes.add(new Note("6 Note", "blah blah", new Date()));
    notes.add(new Note("7 Note", "blah blah", new Date()));
    notes.add(new Note("8 Note", "blah blah", new Date()));

    populateList();
    //populateLateCustomAdapter();



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.list_notes, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    //notes.add(new Note("Added note", "blah", new Date()));
    //populateList();

    Intent editNoteIntent = new Intent (this, EditNotesActivity.class);
    startActivityForResult(editNoteIntent, 1);


    return true;


}




        // Populate Method
        private void populateList() {
            List<String> values = new ArrayList<String>();

            for(Note note : notes) {

                values.add(note.getTitle());
            }

            CustomListAdapter CustomAdapter = new CustomListAdapter();

            notesListView.setAdapter(CustomAdapter);
        }

そして、これがあなたが私にくれたアダプタークラスですが、私の変数名などがあります:

class CustomListAdapter extends BaseAdapter {

Context mContext;
List<String> mList;

public CustomListAdapter (Context context, List<String> values) {
    mList = values;
    mContext = context;
}

@Override
public int getCount() {
    return mList.size();
}

@Override
public String getItem(int position) {
    return mList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

// This method is called to draw each row of the list
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // here you inflate the layout you want for the row
    final View view = View.inflate(mContext, R.layout.item_list, null);
    return view;


}}

私の問題は populateList メソッドにあります。上記の行を太字にしました。新しいアダプターを作成するために populateList メソッドにどのコードとパラメーターを入れればよいかわかりません。

CustomListAdapter CustomAdapter = new CustomListAdapter(); に使用するコードを知っているかどうか疑問に思っていました。

そして、どのパラメータを渡すのですか?.

おかげでヒープ

前もって感謝します

乾杯コーリー B

4

1 に答える 1