【问题标题】:How to style individual listView items in android?如何在android中设置单个listView项目的样式?
【发布时间】:2013-06-06 10:59:47
【问题描述】:

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

我的笔记应用程序正在运行,但目前 UI 很糟糕。

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

我希望它使添加到列表中的每个新注释自动获得预定义的样式(如边距)。

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

【问题讨论】:

    标签: java android xml listview user-interface


    【解决方案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 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);
        }
    

    【讨论】:

    • 嗨,这似乎是完美的谢谢。我只有一个问题。我用什么语法来设置这个适配器?在我使用的前一个适配器上,我使用了这个:“ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);”。但这不适用于您提供的适配器。你知道我会用什么来设置适配器吗?再次感谢。科里“
    • 您是否将自定义布局与列表绑定?我已经更新了我的帖子。
    • 在getView方法中检查convertView参数是否为空。如果是,那么您应该为新布局充气。如果不是,请重复使用相同的。这对系统资源来说更经济,并且可以实现更优化的列表视图。
    • 是的,你是对的。对于这个例子,我希望尽可能简单。但是对于复杂的自定义布局,我们绝对应该尽量重用相同的布局。
    • 嗨,是的,我已经编写了自己的 item_list。我已经用我的问题更新了我的原始帖子。再次感谢科里
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多