Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中设置单个listView项目的样式?_Java_Android_Xml_Listview_User Interface - Fatal编程技术网

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

Java 如何在android中设置单个listView项目的样式?,java,android,xml,listview,user-interface,Java,Android,Xml,Listview,User Interface,对不起,如果这是一个有点不切实际的问题。 我习惯于用JS和CSS编写Windows8应用程序,但我对java还不是很在行 我正在制作我的第一个android应用程序(笔记录)。 我在xml中定义了一个标准ListView,如下所示: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

对不起,如果这是一个有点不切实际的问题。 我习惯于用JS和CSS编写Windows8应用程序,但我对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>
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;
    }
}
}

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

我正在尝试将样式应用于每个单独的listview项目,但我在任何地方都找不到这样做的noobs指南

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

有谁能告诉我从XML中为每个listview项目应用样式的最佳方法(如设置边距等)

还有人知道如何动态更改各个listview项目吗(就像我希望能够做到的那样,如果用户从对话框中选择某个颜色,则单个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);
        }
package com.fishingfon.notetakerui;
公共类ListNotesActivity扩展了活动{
@凌驾
公共布尔值onContextItemSelected(MenuItem项){
AdapterView.AdapterContextMenuInfo信息=(AdapterView.AdapterContextMenuInfo)项。getMenuInfo();
注释。移除(信息位置);
大众主义者();
//populateLateCustomAdapter();
返回true;
}
@凌驾
public void onCreateContextMenu(ContextMenu菜单,视图v,ContextMenu.ContextMenuInfo菜单信息){
super.onCreateContextMenu(menu,v,menuInfo);
MenuInflater充气机=getMenuInflater();
充气机。充气(右菜单。上下文菜单,菜单);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(resultCode==RESULT\u取消){
返回;
}
SerializableExtra=data.getSerializableExtra(“注释”);
如果(额外!=null){
注:新注=(注)额外;
如果(编辑注释ID>-1){
notes.set(editingNoteId,newNote);
editingNoteId=-1;
}
否则{
注释。添加(新注释);
};
大众主义者();
//populateLateCustomAdapter();
}
}
私有列表注释=新的ArrayList();
私有列表视图notesListView;
private int editingNoteId=-1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u list\u notes);
ListView notesListView=(ListView)findViewById(R.id.notesListView);
notesListView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView适配器、视图视图、int itemNumber、长id){
Intent editNoteIntent=新意图(view.getContext(),EditNotesActivity.class);
editNoteIntent.putExtra(“Note”,notes.get(itemNumber));
editingNoteId=itemNumber;
startActivityForResult(editNoteIntent,1);
}
});
registerForContextMenu(notesListView);
注释。添加(新注释(“1注释”,“诸如此类”,新日期());
添加(新注释(“2注释”,“废话”,新日期());
添加(新注释(“3注释”,“废话”,新日期());
添加(新注释(“4注释”,“废话”,新日期());
添加(新注释(“5注释”,“废话”,新日期());
注释。添加(新注释(“6注释”,“诸如此类”,新日期());
添加(新注释(“7注释”,“废话”,新日期());
添加(新注释(“8注释”,“废话”,新日期());
大众主义者();
//populateLateCustomAdapter();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.list\u notes,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//注释。添加(新注释(“添加注释”,“废话”,新日期());
//大众主义者();
Intent editNoteIntent=新的Intent(这是EditNotesActivity.class);
startActivityForResult(editNoteIntent,1);
返回true;
}
//填充方法
私有void populateList(){
列表值=新的ArrayList();
用于(注释:注释){
add(note.getTitle());
}
CustomListAdapter CustomAdapter=新的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;


}}
class CustomListAdapter扩展了BaseAdapter{
语境;
列表列表;
公共CustomListAdapter(上下文、列表值){
mList=数值;
mContext=上下文;
}
@凌驾
public int getCount(){
返回mList.size();
}
@凌驾
公共字符串getItem(int位置){
返回mList.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
//调用此方法以绘制列表的每一行
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//在这里,您可以为该行膨胀所需的布局
最终视图=视图。充气(mContext,R.layout.item_list,null);
返回视图;
}}
我的问题在于大众主义方法。我已经在上面把这句话加粗了。 我不确定在populateList方法中放入什么代码和参数来创建新适配器

我只是想知道您是否知道我将使用什么代码: CustomListAdapter CustomAdapter=新的CustomListAdapter()

传递什么参数

谢谢你

提前谢谢

干杯
Corey B

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

<?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>
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;
    }
}
}
class CustomListAdapter扩展了BaseAdapter{
语境;
列表列表;
公共CustomListAdapter(上下文、列表){
mList=列表;
mContext=上下文;
}
@凌驾
public int getCount(){
返回mList.size();