게시판과 같이 글을 쓰면 php를 통해 웹호스팅DB에 저장하고 리스트뷰에 저장된 내용을 가져와 글쓴후 바로 쓴내용 볼수 있
도록 소스를 짜봣는데.. 이게 잘될떄 있고.. 연속 적으로 글을 쓰거나... 그냥 웹DB에서 가져와 리스트뷰에 쓴내용을 보려할
때 늦게 가져와서 리스트뷰가 텅비어잇더군요... 종종그러긴한데..
웹DB에 있는 내용을 가져와 리스트뷰에 쓰여지면 그때 화면을 보여주도록 하고싶은데... 지연시간이나 이런것등.. 어떻게 하
면 될지 잘모르겠습니다. 도와주세요.
일단 간단히 제가 작성한 소스 입니다.
간단한 원리
1. 특정 이름에 대해 한줄 게시판
2. 이름을 누르면 그 이름을 putExtra로 한줄게시판 액티비티로 보냄
3. 이름을 DB로 보내 그이름에 대한 게시글만 가져와서 리스트뷰에 띄움
4. 글쓰기 버튼 누르면 글쓰는 다이얼로그가 뜨며 작성버튼누르면 웹DB로 작성글을 보내고 그 작성글을 다시가져와
그 이름에 대한 한줄게시판에 추가 작성되어 리스트뷰에 띄움
대충 이런 방식입니다....
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.numberinfo);
listview = (ListView)findViewById(R.id.listView1);
list = new ArrayList<String>();
Intent intent = getIntent();
str = intent.getStringExtra("name");
TextView text = (TextView)findViewById(R.id.textView1);
text.setText(str);
new TestTask().execute();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
listview.setDivider(new ColorDrawable(Color.BLACK));
listview.setDividerHeight(2);
listview.setAdapter(adapter);
infoWrite();
}
private void infoWrite() {
btnWrite = (Button)findViewById(R.id.btnWrite);
btnExit = (Button)findViewById(R.id.btnExit);
builder = new AlertDialog.Builder(this);
btnWrite.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LayoutInflater inflater = null;
layout = null;
Context mContext = nameinfo.this;
inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.reportdialog,(ViewGroup) findViewById(R.id.layout_root));
builder.setTitle(str);
builder.setView(layout);
builder.setPositiveButton("작성", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
editText = (EditText)layout.findViewById(R.id.editText1);
info = editText.getText().toString();
new TestTask2().execute();
}
});
builder.setNegativeButton("취소", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
});
btnExit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
}
class TestTask extends AsyncTask<String, String, String>{
protected void onPostExecute(String result) {
}
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(웹호스팅 DB에서 이름에 대한 게시글만 가져오기);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (Build.VERSION.SDK_INT < 14)
{
conn.setDoOutput(true);
}
BufferedReader rd = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String line;
char s;
String temp = "";
line = rd.readLine();
for (int i = 0; i < line.length(); i++) {
if((s = line.charAt(i)) != '/') temp = temp + s;
else {
list.add(temp);
temp = "";
}
}
rd.close();
} catch (Exception e) {
}
return null;
}
}
public class TestTask2 extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL(웹호스팅 DB로 게시글 보내기);
url.openStream();
} catch(Exception e) {
}
return null;
}
protected void onPostExecute(String result) {
Intent i = new Intent(nameinfo.this,nameinfo.class);
i.putExtra("name", str);
startActivity(i);
}
}