안녕하세요? 다름이 아니라 메모장을 만들고 있는데요...
스레드를 이용하여 파일을 로딩한 후 이를 리스트뷰에 출력하려고 하니
리스트뷰에 아무것도 출력이 안되네요...ㅜㅜ
public class Memolist3Activity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arraylist=new ArrayList<memolist>();
createThreadAndDialog();
mylistadapter=new MyListAdapter(this,R.layout.layoutlist,arraylist);
// ListView 생성
lv=(ListView)findViewById(R.id.list);
lv.setAdapter(mylistadapter);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setOnItemClickListener(listener);
}// onCreate
void createThreadAndDialog()
{
progressDialog = ProgressDialog.show(this,"","로딩중",true);
Thread thread=new Thread(new Runnable()
{
public void run()
{
// 파일에 저장된 데이터 불러오기
try
{
FileInputStream fis=openFileInput("data.dat");
ObjectInputStream ois=new ObjectInputStream(fis);
@SuppressWarnings("unchecked")
ArrayList<memolist> readObject=(ArrayList<memolist>) ois.readObject();
arraylist=readObject;
ois.close();
fis.close();
}
catch(Exception e)
{
// Toast.makeText(this,"불러오기 실패"+e.getMessage(),0).show();
}
mylistadapter.notifyDataSetChanged();
mHandler.sendEmptyMessage(0);
}
});
thread.start();
}// createThreadAndDialog
Handler mHandler=new Handler()
{
public void handleMessage(Message msg)
{
// 어댑터와 arraylist연결하기
progressDialog.dismiss();
mylistadapter.notifyDataSetChanged();
lv.setAdapter(mylistadapter);
buttadd.setText("새 메모 추가"+"("+arraylist.size()+")");
}
};
}// class Memolist2Activity
class MyListAdapter extends BaseAdapter
{
LayoutInflater inflater;
ArrayList<memolist> mylist;
int layout;
public MyListAdapter(Context context,int layout,
ArrayList<memolist> mylist)
{
this.inflater=(LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
this.mylist=mylist;
this.layout=layout;
}// MyListAdapter
public int getCount()
{
return mylist.size();
}// getCount
public String getItem(int pos)
{
return mylist.get(pos).sDate;
}// getItem
public long getItemId(int pos)
{
return pos;
}// getItemId
public View getView(int pos,View convertView,ViewGroup parent)
{
if(convertView==null)
{
convertView=inflater.inflate(layout,parent,false);
}
// 시간
TextView textdate=(TextView)convertView.findViewById(R.id.text_date);
// 메모
TextView texttitle=(TextView)convertView.findViewById(R.id.text_memo);
// 다음의 if문은 TextView의 색상을 다르게 적용하기 위해 사용된다.
if(mylist.get(pos).sColor.equals("1"))
{
texttitle.setBackgroundResource(R.drawable.textround);
textdate.setBackgroundResource(R.drawable.textround);
}
if(mylist.get(pos).sColor.equals("2"))
{
texttitle.setBackgroundResource(R.drawable.textround2);
textdate.setBackgroundResource(R.drawable.textround2);
}
if(mylist.get(pos).sColor.equals("3"))
{
texttitle.setBackgroundResource(R.drawable.textround3);
textdate.setBackgroundResource(R.drawable.textround3);
}
if(mylist.get(pos).sColor.equals("4"))
{
texttitle.setBackgroundResource(R.drawable.textround4);
textdate.setBackgroundResource(R.drawable.textround4);
}
textdate.setText(mylist.get(pos).sDate);
texttitle.setText(mylist.get(pos).sMemo);
return convertView;
}// getView
}// MyListAdapter
문제는 빨간색 부분에 있어요. ProgressDialog로 파일을 읽어온후 핸들러에서 ListView를 갱신해주는데...
갱신이 안되네요...에궁...ㅠㅠ




mylistadapter.notifyDataSetChanged();
이것도 아마 UI 건드는거라서... 핸들러 post로 하거나 해야하는걸로 기억하는데요...
아니면 스레드 세이프가 아니던가... 뭐 어쨋든... 그렇습니다.