我是Android开发的新手,我发现编程很难,这就是为什么我要求你的帮助。我正在研究项目,我可以在列表上添加注释。我还有一个活动,我编辑笔记,我想在那里添加一个按钮,当我点击它,在noteActivity中添加一个项目在我的笔记前面。我想在笔记的前面加上一个标记为重要标记的解释标记。我真的不知道怎么做,我在哭。请有人帮我吗?有什么例子我怎么能这样做?
以下是笔记的主要活动:
公共类MainActivity扩展了ListActivity {
private static final int EDITOR_ACTIVITY_REQUEST = 1001;
private NotesDataSource datasource;
List<NoteItem> notesList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new NotesDataSource(this);
refreshDisplay();
}
private void refreshDisplay() {
notesList= datasource.findAll();
ArrayAdapter<NoteItem> adapter =
new ArrayAdapter<>(this, R.layout.list_item_layout, notesList);
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_create) {
createNote();
}
return super.onOptionsItemSelected(item);
}
private void createNote() {
NoteItem note = NoteItem.getNew();
Intent intent = new Intent(this, NoteEditorActivity.class);
intent.putExtra("key", note.getKey());
intent.putExtra("text", note.getText());
startActivityForResult(intent, EDITOR_ACTIVITY_REQUEST);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
NoteItem note = notesList.get(position);
Intent intent = new Intent(this, NoteEditorActivity.class);
intent.putExtra("key", note.getKey());
intent.putExtra("text", note.getText());
startActivityForResult(intent, EDITOR_ACTIVITY_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == EDITOR_ACTIVITY_REQUEST && resultCode == RESULT_OK ) {
NoteItem note = new NoteItem();
note.setKey(data.getStringExtra("key"));
note.setText(data.getStringExtra("text"));
datasource.update(note);
refreshDisplay();
}
}
}
这是我的NoteEditorActivity
public class NoteEditorActivity extends Activity{
private NoteItem note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_editor);
getActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = this.getIntent();
note = new NoteItem();
note.setKey(intent.getStringExtra("key"));
note.setText(intent.getStringExtra("text"));
EditText et = (EditText)findViewById(R.id.headerText);
et.setText(note.getText());
et.setSelection(note.getText().length());
}
private void saveAndFinish(){
EditText et = (EditText)findViewById(R.id.headerText);
String headerText = et.getText().toString();
Intent intent = new Intent();
intent.putExtra("key", note.getKey());
intent.putExtra("text", headerText);
setResult(RESULT_OK, intent);
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== android.R.id.home) {
saveAndFinish();
}
return false;
}
@Override
public void onBackPressed() {
saveAndFinish();
}
}
答案 0 :(得分:0)
阅读ArrayAdapter部分。
对于解决方案,您必须将标记的信息添加到NoteItem类中,并检查适配器中的getter(将它们添加到您的类中)
您需要编写自己的自定义适配器。
public class MainActivity extends ListActivity {
private static final int EDITOR_ACTIVITY_REQUEST = 1001;
private NotesDataSource datasource;
List<NoteItem> notesList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new NotesDataSource(this);
refreshDisplay();
}
private void refreshDisplay() {
notesList= datasource.findAll();
ArrayAdapter<NoteItem> adapter =
new CustomAdapter(this, R.layout.custom_layout, notesList);
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_create) {
createNote();
}
return super.onOptionsItemSelected(item);
}
private void createNote() {
NoteItem note = NoteItem.getNew();
Intent intent = new Intent(this, NoteEditorActivity.class);
intent.putExtra("key", note.getKey());
intent.putExtra("text", note.getText());
startActivityForResult(intent, EDITOR_ACTIVITY_REQUEST);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
NoteItem note = notesList.get(position);
Intent intent = new Intent(this, NoteEditorActivity.class);
intent.putExtra("key", note.getKey());
intent.putExtra("text", note.getText());
startActivityForResult(intent, EDITOR_ACTIVITY_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == EDITOR_ACTIVITY_REQUEST && resultCode == RESULT_OK ) {
NoteItem note = new NoteItem();
note.setKey(data.getStringExtra("key"));
note.setText(data.getStringExtra("text"));
datasource.update(note);
refreshDisplay();
}
}
private class CustomAdapter extend ArrayAdapter<NoteItem> {
private int mResourceId;
private List<NoteItem> mData;
public CustomAdapter(Context context, int resourceId, List<NoteItem> data) {
super(context, resourceId, data);
mResourceId = resourceId;
mData = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
NoteItem item = mData.get(position);
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(resourceId, null);
}
TextView titleTV = (TextView) convertView.findViewById(R.id.title);
TextView textTV = (TextView) convertView.findViewById(R.id.text);
CheckBox markedCB = (CheckBox) convertView.findViewById(R.id.mark);
titleTV.setText(item.getKey());
textTV.setText(item.getText());
markedCB.setChecked(itemt.getMarked());
return convertView;
}
}
}
将以下内容另存为custom_layout
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/mark"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
你总是创建一个新的适配器,这不是一个好方法。请改用clear()和add(): Docu