我正在制作我的第一个 android 应用程序,但我遇到了问题。
我正在努力做到这一点,如果有人单击我的列表视图中的某个项目,它就会更改它的 XML 视图。在我的 listNotes 活动中,我创建了一个名为 itemNumber 的 Int 变量,并将其设置为 -10 的内容。然后,我为 listView 项目 Rows 设置了一个 onclick 事件,并在其中进行了设置,以便在 onClick 事件中, itemNumber 被设置为被单击项目的位置。
但我需要这样做,以便在我的自定义适配器中,如果 itemNumber = -10,它会默认运行,但如果 itemNumber 不 = -10,它只会更改用户单击的项目的 XML 视图(位置存储在 itemnumber 中。
我曾尝试在自定义适配器中使用 if 语句和方法,但我遇到了很多错误。谁能告诉我我应该用哪种方式来做这件事,因为它已经让我烦恼了 2 天。
这是代码:
public class ListNotesActivity extends Activity {
public ArrayList <Note> notes = new ArrayList<Note>();
private ListView notesListView;
private int editingNoteId = -1;
public int itemNumberId = -10;
int itemListLayout = R.layout.item_list;
int itemListOnCLickLayout = R.layout.item_list_onclick;
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
notes.remove(info.position);
populateList(itemListLayout, itemNumberId);
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;
}
if (resultCode == EditNotesActivity.RESULT_DELETE){
notes.remove(editingNoteId);
editingNoteId = -1;
populateList(itemListLayout, itemNumberId);
}
Serializable extra = data.getSerializableExtra("Note");
if (extra != null){
Note newNote = (Note)extra;
if (editingNoteId > -1){
notes.set(editingNoteId, newNote);
editingNoteId = -1;
}
else {
notes.add(0,newNote);
};
populateList(itemListLayout, itemNumberId);
}
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_notes);
notesListView = (ListView)findViewById(R.id.notesListView);
notesListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int itemNumber, long id) {
/* Intent editNoteIntent = new Intent(view.getContext(), EditNotesActivity.class);
editNoteIntent.putExtra("Note", notes.get(itemNumber));
startActivityForResult(editNoteIntent, 1);
*/
//editingNoteId = itemNumber;
itemNumberId = itemNumber;
populateList(itemListOnCLickLayout ,itemNumberId);
itemNumberId = -10;
}
});
registerForContextMenu(notesListView);
notes.add(0, new Note("1 Note", "blah blah", new Date()));
notes.add(0, new Note("2 Note", "blah blah", new Date()));
notes.add(0, new Note("3 Note", "blah blah", new Date()));
notes.add(0, new Note("4 Note", "blah blah", new Date()));
notes.add(0, new Note("5 Note", "blah blah", new Date()));
notes.add(0, new Note("6 Note", "blah blah", new Date()));
notes.add(0, new Note("7 Note", "blah blah", new Date()));
notes.add(0, new Note("This should display first", "This should display first!!!!", new Date()));
populateList(itemListLayout, itemNumberId);
}
@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) {
Intent editNoteIntent = new Intent (this, EditNotesActivity.class);
startActivityForResult(editNoteIntent, 1);
return true;
}
public void populateList(int itemRowLayout, int itemNumberId) {
CustomListAdapter customAdapter = new CustomListAdapter(this, itemRowLayout, itemNumberId, notes);
notesListView.setAdapter(customAdapter);
}
自定义适配器:
public class CustomListAdapter extends ArrayAdapter {
Context mContext;
ArrayList<Note> notesR;
int itemRowLayout;
int itemNumber;
public CustomListAdapter (Context context, int itemRowXML, int itemNumberId, ArrayList notes) {
super(context, itemRowXML, itemNumberId, notes);
mContext = context;
notesR = notes;
itemRowLayout = itemRowXML;
itemNumber = itemNumberId;
}
@Override
public int getCount() {
return notesR.size();
}
@Override
public Object getItem(int position) {
return notesR.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
// This method is called to draw each row of the list
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater;
inflater = LayoutInflater.from(parent.getContext());
v = inflater.inflate(itemRowLayout, null);
}
final Note noteModel = notesR.get(position);
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date myNotesDate = noteModel.getDate();
String noteDate = dateFormat.format(myNotesDate);
TextView content = (TextView) v.findViewById(R.id.content);
content.setText(noteModel.getNote());
TextView title = (TextView) v.findViewById(R.id.title);
title.setText(noteModel.getTitle());
TextView date = (TextView) v.findViewById(R.id.date);
date.setText("" + getDate(noteDate));
return v;
}
String monthName;
String dateWithMonthName;
String dayEnding;
// METHOD
public String getDate(String noteDate){
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date newDate = new Date();
String testDateValue = dateFormat.format(newDate);
String[] testDateSplit = testDateValue.split("-");
String testSplitDay = testDateSplit[0];
String testSplitMonth = testDateSplit[1];
String testSplitYear = testDateSplit[2];
int testYearInt = Integer.parseInt(testSplitYear);
String[] noteDateSplit = noteDate.split("-");
String noteSplitDay = noteDateSplit[0];
String noteSplitMonth = noteDateSplit[1];
String noteSplitYear = noteDateSplit[2];
int noteYearInt = Integer.parseInt(noteSplitYear);
int intSplitDay = Integer.parseInt(noteSplitDay);
int intSplitMonth = Integer.parseInt(noteSplitMonth);
if ((intSplitDay == 1) || (intSplitDay == 21) || (intSplitDay == 31)){
dayEnding = "st ";
}
else if ((intSplitDay == 2) || (intSplitDay == 22)){
dayEnding = "nd ";
}
else if ((intSplitDay == 3) || (intSplitDay == 23)){
dayEnding = "rd ";
}
else if ((intSplitDay >= 4) && (intSplitDay <= 20) || (intSplitDay >= 24) && (intSplitDay <= 30) ){
dayEnding = "th ";
}
//Month
if (intSplitMonth == 1){
monthName = "January";
}
else if (intSplitMonth == 2){
monthName = "February";
}
else if (intSplitMonth == 3){
monthName = "March";
}
else if (intSplitMonth == 4){
monthName = "April";
}
else if (intSplitMonth == 5){
monthName = "May";
}
else if (intSplitMonth == 6){
monthName = "June";
}
else if (intSplitMonth == 7){
monthName = "July";
}
else if (intSplitMonth == 8){
monthName = "August";
}
else if (intSplitMonth == 9){
monthName = "September";
}
else if (intSplitMonth == 10){
monthName = "October";
}
else if (intSplitMonth == 11){
monthName = "November";
}
else if (intSplitMonth == 12){
monthName = "December";
}
if (noteYearInt != testYearInt){
dateWithMonthName = noteSplitDay + dayEnding + monthName + " " + noteSplitYear;
}
else if (noteYearInt == testYearInt){
dateWithMonthName = noteSplitDay + dayEnding + monthName;
}
return dateWithMonthName ;
}
有谁知道我将如何更改单击的 listView 项目的 XML 布局文件,看到上面的代码?
编辑1:
这是我现在的适配器:
public class CustomTwoRowAdapter extends ArrayAdapter {
public static final int INITIAL_TYPE = 0;
public static final int CHANGE_TYPE = 1;
private List<Note> notesR;
private LayoutInflater mInflater;
private ArrayList<Integer> mChangedRows = new ArrayList<Integer>();
public CustomTwoRowAdapter(Context context, int id, ArrayList<Note> notes) {
super(context, id, notes);
notesR = notes;
mInflater = LayoutInflater.from(context);
}
public void changeRowLayout(int rowPosition) {
if (mChangedRows.contains(rowPosition)) {
mChangedRows.remove(mChangedRows.indexOf(rowPosition));
} else {
mChangedRows.add(rowPosition);
}
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
if (mChangedRows.size() == 0 || !mChangedRows.contains(position)) {
return INITIAL_TYPE;
} else {
return CHANGE_TYPE;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return notesR.size();
}
@Override
public Object getItem(int position) {
return notesR.get(position);
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case INITIAL_TYPE:
convertView = mInflater.inflate(
R.layout.item_list, parent, false);
holder.mText = (TextView) convertView
.findViewById(R.id.title);
break;
case CHANGE_TYPE:
convertView = mInflater.inflate(
R.layout.item_list_onclick, parent, false);
holder.mText = (TextView) convertView
.findViewById(R.id.title);
holder.mPressMe = (Button) convertView
.findViewById(R.id.date);
holder.mCheckMe = (CheckBox) convertView
.findViewById(R.id.content);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
switch (type) {
case INITIAL_TYPE:
holder.mText.setText(getItem(position));
break;
case CHANGE_TYPE:
holder.mText.setText(getItem(position));
break;
}
return convertView;
}
static class ViewHolder {
TextView mText;
Button mPressMe;
CheckBox mCheckMe;
}
}
我的问题是,在适配器中,我解析了由我的“Note”对象组成的 ArrayList“notes”。
在 getItem 方法中,它只有在我将返回类型设置为“Object”时才有效,但是在 else 语句中,它有“getItem(position)”,它给出一个错误,说“无法解析方法”setText(java. lang.object”。
你知道如何让代码与我的 ArrayList 对象一起工作吗?
再次感谢你的帮助。干杯科里