안드로이드 초보라 고수님들의 도움을 좀 받고자 합니다.
다른 Activity에서 입력받은 정보를 Intent로 가져와서 리스트를 추가하려고 하는데 추가가 되질 않네요.
리스트 Ativity내에서 따로 이벤트를 만들어야 할까요? 아님 입력받는 Activity에서의 이벤트로 리스트 Activity의 리스트 추가에 관여할 수 있나요? 아님 다른 방법이 있을까요? DB는 사용하지 말아야합니다.
중요 소스만 올릴테니 도움좀 부탁드리겠습니다.
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnok:
String name = edtname.getText().toString();
String phone = edtphone.getText().toString();
String start = edtstart.getText().toString();
String end = edtend.getText().toString();
String money = edtmoney.getText().toString();
Intent send = getIntent();
send.putExtra("name", name);
send.putExtra("phone", phone);
send.putExtra("start", start);
send.putExtra("end", end);
send.putExtra("money", money);
setResult(RESULT_OK, send);
send.setClass(TradeAdd.this, TradeReady.class );
startActivity(send);
finish();
break;
위 소스는 입력받은 정보를 리스트 Activity로 보냄과 동시에 Activity를 이동하는 클릭 이벤트 부분입니다.
listview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linear1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/listname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"/>
<TextView
android:id="@+id/listdate"
android:layout_toRightOf="@id/listname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"/>
<TextView
android:id="@+id/listmoney"
android:layout_below="@id/listname"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="30dp"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
하나의 리스트를 구성하기 위한 xml입니다.(TextView 3개)
중요 소스..
public class TradeReady extends Activity {
ListView lv;
String name;
String phone;
String startdate;
String enddate;
Button btn67;
String money;
boolean dataset = false;
view v;
ListAdapter l_adapter;
int string_count;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tradeready);
// TODO Auto-generated method stub
lv = (ListView)findViewById(R.id.list1);
ArrayList<view> arrlist = new ArrayList<view>();
l_adapter = new ListAdapter(this, R.layout.listview, arrlist);
string_count =l_adapter.getCount();
lv.setAdapter(l_adapter);
l_adapter.notifyDataSetChanged();
if(dataset == true){
l_adapter.insert(new view(getApplicationContext(),name, startdate, money),string_count);
l_adapter.notifyDataSetChanged();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
name = getIntent().getStringExtra("name");
phone = getIntent().getStringExtra("phone");
startdate =getIntent().getStringExtra("start");
enddate = getIntent().getStringExtra("end");
money = getIntent().getStringExtra("money");
dataset = true;
}
class view{
private String listname;
private String listdate;
private String listmoney;
public view(Context context, String _listname, String _listdate, String _listmoney){
_listname = listname;
_listdate = listdate;
_listmoney = listmoney;
}
public String getlistname(){
return listname;
}
public String getlistdate(){
return listdate;
}
public String getlistmoney(){
return listmoney;
}
}
public class ListAdapter extends ArrayAdapter<view>{
public ArrayList<view> item;
public ListAdapter(Context context, int textViewResourceId, ArrayList<view> item) {
super(context, textViewResourceId, item);
this.item = item;
// TODO Auto-generated constructor stub
}
public View getview(int position, View convertView, ViewGroup parent){
View view = null;
if(v==null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.listview, null);
}
else{
view = convertView;
}
final view vv = this.getItem(position);
if(vv != null)
{
TextView t1 = (TextView) view.findViewById(R.id.listname);
TextView t2 = (TextView) view.findViewById(R.id.listdate);
TextView t3 = (TextView) view.findViewById(R.id.listmoney);
t1.setText(vv.getlistname());
t2.setText(vv.getlistdate());
t3.setText(vv.getlistmoney());
}
return view;
}
}
}
처음에 테이터가 넘어오면 onActivityResualt에서 dataset이 true로 바뀌어 리스트가 추가 디겠거니 했는데 되질 않네요..
고수님들 제발 도움 부타그리겠습니다.. 이것땜에 며칠동안 진도를 못나가고 있습니다.
공지사항을 다 읽었음
리스트 뷰에서 뿌릴 데이터를 담을 클래스를 싱글톤으로 만듭니다.
A액티비티에서 getInsatnce()해서 리스트 가져다가 자료를 추가하거나 삭제합니다.
B액티비티의 onCreate에서 리스트뷰 아답터 셋팅하고 리스트를 가져다가 셋팅합니다.
필요하면 onResume에서 리스트를 갱신합니다. 끗