利用dialog来编辑listview中的数据

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中.再次点击的时候这个值才会被更新了

2个回答

同步和异步的问题

 LoginDialogFragment dialog = new LoginDialogFragment();                              
            dialog.show(getFragmentManager(), "loginDialog");             //这里是不会等着的,后面代码接着执行。 
            pp.setHeight(height);               
            mAdapter.notifyDataSetChanged();//这句在显示出来的时候已经执行了,所以刷新要放在dialog关闭的方法里面

希望看到你的源码,对于初学者有很大帮助,谢谢

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐