listview重写baseAdapter,两个activity,listview里item有一个textview和edittext显示activity2输入的数据
代码:publicclassMainActivityextendsActivity{privateListViewlistView;Myadapteradapter=ne...
代码:
public class MainActivity extends Activity {
private ListView listView;
Myadapter adapter=new Myadapter();
People people;//我自己写的一个people类,两个属性,姓名,QQ
ArrayList<People> list = new ArrayList<People>();
private Button bt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
bt = (Button) findViewById(R.id.button1);
listView.setAdapter(adapter);
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myintent = new Intent();
myintent.setClass(MainActivity.this, Activity2.class);
MainActivity.this.startActivityForResult(myintent,10);
}});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
people=(People)data.getSerializableExtra("list");
list.add(people);
adapter.notifyDataSetChanged();
}
class Myadapter extends BaseAdapter{
private LayoutInflater mInflater = null;
public int getCount() {
return list.size();
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
//adapter绑定数据
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
convertView = mInflater.inflate(R.layout.listview, null);
}
TextView tv = (TextView)convertView.findViewById(R.id.name_tv);
TextView ed = (TextView)convertView.findViewById(R.id.qq_ed);;
tv.setText(list.get(position).toString());
ed.setText(list.get(position).toString());
return convertView;
}
};
public class Activity2 extends Activity {
Button bt1;
EditText name,qq;
People people;
ArrayList<People> list=new ArrayList<People>();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bt1=(Button)findViewById(R.id.button1);
name=(EditText)findViewById(R.id.editText1);
qq=(EditText)findViewById(R.id.editText2);
qq.setInputType(EditorInfo.TYPE_CLASS_PHONE);
bt1.setOnClickListener(new MyClickListener());
}
class MyClickListener implements OnClickListener
{
public void onClick(View e)
{
people.setName(name.getText().toString());
people.setQq(qq.getText().toString());
list.add(people);
Intent intent=new Intent();
intent.putExtra("list",list);
setResult(0, intent);
finish();
} 展开
public class MainActivity extends Activity {
private ListView listView;
Myadapter adapter=new Myadapter();
People people;//我自己写的一个people类,两个属性,姓名,QQ
ArrayList<People> list = new ArrayList<People>();
private Button bt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
bt = (Button) findViewById(R.id.button1);
listView.setAdapter(adapter);
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myintent = new Intent();
myintent.setClass(MainActivity.this, Activity2.class);
MainActivity.this.startActivityForResult(myintent,10);
}});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
people=(People)data.getSerializableExtra("list");
list.add(people);
adapter.notifyDataSetChanged();
}
class Myadapter extends BaseAdapter{
private LayoutInflater mInflater = null;
public int getCount() {
return list.size();
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
//adapter绑定数据
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
convertView = mInflater.inflate(R.layout.listview, null);
}
TextView tv = (TextView)convertView.findViewById(R.id.name_tv);
TextView ed = (TextView)convertView.findViewById(R.id.qq_ed);;
tv.setText(list.get(position).toString());
ed.setText(list.get(position).toString());
return convertView;
}
};
public class Activity2 extends Activity {
Button bt1;
EditText name,qq;
People people;
ArrayList<People> list=new ArrayList<People>();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bt1=(Button)findViewById(R.id.button1);
name=(EditText)findViewById(R.id.editText1);
qq=(EditText)findViewById(R.id.editText2);
qq.setInputType(EditorInfo.TYPE_CLASS_PHONE);
bt1.setOnClickListener(new MyClickListener());
}
class MyClickListener implements OnClickListener
{
public void onClick(View e)
{
people.setName(name.getText().toString());
people.setQq(qq.getText().toString());
list.add(people);
Intent intent=new Intent();
intent.putExtra("list",list);
setResult(0, intent);
finish();
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询