package com.example.wightandheight;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class LoginDialogFragment extends DialogFragment
{
private EditText mUsername;
public interface LoginInputListener
{
void onLoginInputComplete(String username);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_login_dialog, null);
mUsername = (EditText) view.findViewById(R.id.id_txt_depict);
builder.setView(view)
// Add action buttons
.setPositiveButton("Sign in",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
LoginInputListener listener = (LoginInputListener) getActivity();
listener.onLoginInputComplete(mUsername
.getText().toString());
}
}).setNegativeButton("Cancel", null);
return builder.create();
}
}
package com.example.wightandheight;
import java.util.ArrayList;
import com.example.wightandheight.ListViewAdapter.ViewHolder;
import com.example.wightandheight.LoginDialogFragment.LoginInputListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity implements LoginInputListener {
private ListView lv;
private ListViewAdapter mAdapter;
private ArrayList persons;
String height="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.list_view);
persons = new ArrayList();
// 為Adapter准備數據
initPersonData();
// 實例化自定義的MyAdapter
mAdapter = new ListViewAdapter(persons, this);
lv.setAdapter(mAdapter);
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Person pp=(Person)mAdapter.getItem(position);
LoginDialogFragment dialog = new LoginDialogFragment();
dialog.show(getFragmentManager(), "loginDialog");
pp.setHeight(height);
mAdapter.notifyDataSetChanged();
}
});
}
@Override
public void onLoginInputComplete(String username) {
// TODO Auto-generated method stub
height=username;
}
// 初始化數據
private void initPersonData(){
Person mPerson;
for(int i=1;i<=40;i++){
mPerson = new Person();
mPerson.setName("Andy"+i);
mPerson.setId(Character.valueOf((char)(i+65))+" ");
mPerson.setHeight(height);
mPerson.setWeight("20kg");
persons.add(mPerson);
}
}
}
以上是部分代碼.問題如下:點擊listview出現diaolog編輯數據,這個數據被保存了,但是沒有及時更新到listview中.再次點擊的時候這個值才會被更新了
同步和異步的問題
LoginDialogFragment dialog = new LoginDialogFragment();
dialog.show(getFragmentManager(), "loginDialog"); //這裡是不會等著的,後面代碼接著執行。
pp.setHeight(height);
mAdapter.notifyDataSetChanged();//這句在顯示出來的時候已經執行了,所以刷新要放在dialog關閉的方法裡面