主页面
package com.example.notepad;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
init();
}
private Button Main_addText;
// 储存数据的list
private ArrayList<String> mNotes;
// 显示笔记列表的控件
private ListView mNoteListView;
// 列表适配器
private ArrayAdapter<String> mlistAdapter;
private void init() {
Main_addText = (Button) findViewById(R.id.Btn_AddMain);
Main_addText.setOnClickListener(MainOnclicklistener);
mNotes = new ArrayList<String>();
mNoteListView = (ListView) findViewById(R.id.listView1);
mlistAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.activity_list_item, mNotes);
mNoteListView.setAdapter(mlistAdapter);
mNoteListView.setOnItemClickListener(ListView_ClickListener);
}
private OnClickListener MainOnclicklistener = new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.Btn_AddMain) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, MenuActivity.class);
startActivityForResult(intent, 0);
}
}
};
private AdapterView.OnItemClickListener ListView_ClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
if (parent.getId() == R.id.listView1) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, MenuActivity.class);
intent.putExtra("position", position);
intent.putExtra("content", mNotes.get(position));
startActivityForResult(intent, 0);
}
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
int position = data.getIntExtra("position", -1);
switch (resultCode) {
case RESULT_CANCELED:
if (position != -1) {
mNotes.remove(position);
mlistAdapter.notifyDataSetChanged();
}
break;
case RESULT_OK:
String content = data.getStringExtra("content");
if (position != -1) {
mNotes.set(position, content);
} else {
mNotes.add(content);
}
mlistAdapter.notifyDataSetChanged();
break;
default:
break;
}
}
};
@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) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
编辑页面
package com.example.notepad;
import org.w3c.dom.Text;
import com.example.notepad.MainActivity.PlaceholderFragment;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MenuActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
init();
}
private EditText mContentEdit;
private Button btn_save;
private Button btn_delete;
private void init() {
mContentEdit = (EditText) findViewById(R.id.Edit_Menu);
Intent intent = getIntent();
String content = intent.getStringExtra("content");
if (!TextUtils.isEmpty(content)) {
mContentEdit.setText(content);
}
btn_save = (Button) findViewById(R.id.button_save);
btn_save.setOnClickListener(mOnClickListener);
btn_delete = (Button) findViewById(R.id.button_delete);
btn_delete.setOnClickListener(mOnClickListener);
}
private OnClickListener mOnClickListener = new OnClickListener() {
public void onClick(View v) {
// TODO 自动生成的方法存根
switch (v.getId()) {
case R.id.button_delete:
setResult(RESULT_CANCELED, getIntent());
finish();
break;
case R.id.button_save:
String content = mContentEdit.getText() + "";
if (!TextUtils.isEmpty(content)) {
Intent intent = getIntent();
intent.putExtra("content", content);
setResult(RESULT_OK, intent);
finish();
}
break;
default:
break;
}
}
};
}
到了编辑页面,点取消可以正常返回.点保存就报错.
求各位大神帮帮忙吧,确实找不到原因. 之前做.net的,现在转安卓,觉得安卓开发真太难了....
------解决思路----------------------
重写BaseAdapter必须要会的,你仔细研究下,其实很简单,如果你的item很复杂的话,ArrayAdapter怎么可能解决问题,现在没空,晚些时候你还没解决的话写一个给你你看看
------解决思路----------------------
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
非法状态意外:数组适配器需要资源ID去构造一个TextView。
Caused by: java.lang.ClassCastException: android.widget.LinearLayout at android.widget.ArrayAdapter.createViewFromResource
(ArrayAdapter.java:340)
引发原因:类强转意外,在你的适配器类中第 340行,createViewFromResource这个方法检查下。
要不把你的适配器代码贴上来。
------解决思路----------------------
忘记说了,你这个错误的根本原因是的你ListView的item的布局引起的,你的item布局是不是线性布局,你在适配器中加载布局的时候还做了些操作,然后导致了类强转意外。换用RelativeLayout相对布局试试,还有错误的话,把适配器代码和item布局贴上来看看。
------解决思路----------------------
像ListView,GridView,Gallery这些个ViewGroup需要一个适配器来给他们的Item填充视图,简单点说就是,你的ListView的每一项都要有个视图,要不然显示不了,但是你不可能去一项一项的填充视图,所以适配器就是为了解决这个而出现的,一般来说,都是自定义一个适配器来继承BaseAdapter,比如:public class mListViewAdapter extends BaseAdapter{.......}
这个适配器可以写成内部类,也可以单独写。
下面这个是我去年回复CSDN上的问题写的一个小DEMO,关于ListView的,不要分,你可以去下载看看,注释比较详细。
http://download.csdn.net/detail/u012137924/8412899
------解决思路----------------------
主界面:
public class Test extends Activity {
private static final String TAG = "Test";
private ListViewAdapter mListViewAdapter;
private ListView mListView;
private List list;
private String[] items = { "我是第一项", "我是第二项", "我是第三项", "我是第四项", "我是第五项",
"我是第六项" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_cycle_life);
list = new ArrayList<String>();
for (int i = 0; i < items.length; i++) {
list.add(items[i]);
}
mListViewAdapter = new ListViewAdapter(this, list);
mListView = (ListView) findViewById(R.id.listView);
mListView.setAdapter(mListViewAdapter);
}
}
适配器:
public class ListViewAdapter extends BaseAdapter {
private Context context;
private List list;
public ListViewAdapter(Context context, List list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewCache viewCache = null;
if (convertView == null) {
viewCache = new ViewCache();
convertView = LayoutInflater.from(context).inflate(
R.layout.item, null);
viewCache.item = (TextView) convertView
.findViewById(R.id.items_info);
convertView.setTag(viewCache);
} else {
viewCache = (ViewCache) convertView.getTag();
}
viewCache.item.setText((CharSequence) list.get(position));
return convertView;
}
class ViewCache {
TextView item;
}
}
适配器布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/items_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:textSize="25sp" />
</RelativeLayout>