如何在android中设置单个listView项目的样式?

时间:2013-06-06 10:59:47

标签: java android xml listview user-interface

对不起,如果这是一个小小的问题。 我习惯用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>

我的笔记应用程序应用程序正在运行,但目前用户界面还没有。

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

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

任何人都可以告诉我从XML中为每个listview项目应用样式的最佳方法(比如设置边距等)。

也有人知道如何动态更改单个列表视图项(如果用户从对话框中选择某种颜色,我希望能够这样做,单个列表视图项改变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

1 个答案:

答案 0 :(得分:1)

在您的适配器中,您需要将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方法:

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