3

对不起,如果这是一个有点愚蠢的问题。我习惯用 JS 和 CSS 编写 windows 8 应用程序,但我对 java 仍然不是很好。

我正在制作我的第一个 android 应用程序(记笔记)。我在我的 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>

我有我的笔记应用程序工作,但目前的用户界面是 s**t。

我正在尝试将样式应用于每个单独的列表视图项目,但我找不到任何地方的新手指南。

我想要它,以便添加到列表中的每个新注释自动获得预定义的样式(如边距)。

谁能告诉我将样式应用于 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);
        }

这是您给我的 apadter 类,但带有我的变量名称等:

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

4

在您的适配器中,您需要将 item_list 资源与您的注释列表绑定,如下所示:

class CustomListAdapter extends BaseAdapter {

    Context mContext;
    List<String> mList;

    public CustomListAdapter (Context context, List<String> list) {
        mList = list;
        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);

        // you bind the layout with the content of your list
        // for each element of your list of notes, the adapter will create a row and affect the right title
        final TextView noteTitle= (TextView)view.findViewById(R.id.note_title);
        noteTitle.setText(mList.getItem(position));


        return view;
    }
}
}

这是文件 item_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="match_parent">

   <TextView
            android:id="@+id/note_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</LinearLayout>

然后要创建适配器,请在填充方法中编写:

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

        for(Note note : notes) {

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

        CustomListAdapter CustomAdapter = new CustomListAdapter(this, values);

        notesListView.setAdapter(CustomAdapter);
    }
于 2013-06-06T12:03:57.567 回答