gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:41:13 25 4
gpt4 key购买 nike

抱歉,如果这是一个有点菜鸟的问题。我习惯用 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 很糟糕。

我正在尝试将样式应用于每个单独的 ListView 项目,但我无法在任何地方找到这样做的菜鸟指南。

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

谁能告诉我将样式应用到 XML 中每个单独的 ListView 项的最佳方法(例如设置边距等)。

还有谁知道如何动态更改单个 ListView 项(比如我希望能够做到这一点,如果用户从对话框中选择某种颜色,则该单个 ListView 项会更改 BG 颜色)。

编辑 1:

你好,这是我的 listNotes Activity 的代码:

   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

最佳答案

在您的适配器中,您需要像这样将您的 item_list 资源与您的注释列表绑定(bind):

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

关于java - 如何在 android 中设置单个 listView 项目的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16960415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com