如何用dialog对listview里的数据进行修改

在做一个题库app,想要在listview中选择自己要修改的项并在dialog中对项中的数据进行修改,但因为不知道怎么dialog中的数据传递回选中项中,有点无从下手的感觉。

edit_question.class代码如下:

  public class edit_question extends Activity{
    private DataBaseHelper helper;

    private MyAdapter adapter;
    private MySearchAdapter searchadapter;
    private String id;
    private List<Question> questions;
    private List<Search_Question> search_questions;
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_listview);
        QuestionService questionService=new QuestionService(edit_question.this);
        Intent intent =getIntent();
        id=intent.getStringExtra("id");
        adapter=new MyAdapter();
        listView=(ListView)findViewById(R.id.lv_all);
        questions=questionService.selectAll();
        adapter.notifyDataSetChanged();
        listView.setAdapter(adapter);
        Button search=(Button)findViewById(R.id.btn_search);
        search.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View arg0) {
                 //TODO Auto-generated method stub

                QuestionService questionService=new QuestionService(edit_question.this);
                Intent intent =getIntent();
                id=intent.getStringExtra("id");


                EditText et_search=(EditText)findViewById(R.id.et_search);
                String name=et_search.getText().toString().trim();

                adapter=new MyAdapter();
                questions=questionService.selectByquestionname(name);
                listView.setAdapter(adapter);


            }

        });



        Button back=(Button)findViewById(R.id.show_listview_back);
        back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent back_intent=new Intent(edit_question.this,MainActivity.class);
                startActivity(back_intent);
            }
        });




    }




    public class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return questions.size();
        }

        @Override
        public Object getItem(int postion) {
            // TODO Auto-generated method stub
            return questions.get(postion);
        }

        @Override
        public long getItemId(int postion) {
            // TODO Auto-generated method stub
            return postion;
        }

        @Override
        public View getView(final int postion, View convertView, ViewGroup parent){
            // TODO Auto-generated method stub
            final Question question=questions.get(postion);

            View view;
            if(convertView==null){
                view =View.inflate(edit_question.this, R.layout.edit_question, null);
            }
            else{
                view = convertView;
            }

            TextView tv_question=(TextView)view.findViewById(R.id.tv_edit_question_questionname);
            tv_question.setText(question.getquestionname());



            Button btn_edit=(Button)view.findViewById(R.id.edit);




            return view;


    }

    }
}

布局show_question.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <EditText
                        android:id="@+id/et_search"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:ems="10" >

                        <requestFocus />
                </EditText>

                <Button
                        android:id="@+id/btn_search"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="查找" />

        </LinearLayout>

        <ListView
                android:id="@+id/lv_all"
                android:layout_width="match_parent"
                android:layout_height="354dp" >

        </ListView>

        <Button
                android:id="@+id/show_listview_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="返回" />

</LinearLayout>

布局edit_question.xml:
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:orientation="horizontal" >

<TextView
    android:id="@+id/tv_edit_question_questionname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1.36"
    android:text="TextView" />

<Button
    android:id="@+id/edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="修改" />

1个回答

import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    //activity继承listActivity,重写onListItemClick
    //点击某个item时就进入该方法
    protected void onListItemClick(ListView l, View v, final int position, long id) {

        final EditText edit=new EditText(this);//创建一个edit用来修改
        edit.setText(list.get(position).get("name").toString());//显示点击的item的值
        //创建一个对话框,将edit设置进去并显示,提供一个保存按钮,点击按钮则执行里面的方法
        Dialog dialog=new AlertDialog.Builder(NameInstead.this)
        .setTitle("称呼修改").setView(edit)
        .setPositiveButton("保存", new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                //点击保存时,将listView的对应position的map替换为当前修改的 
                HashMap<String ,Object>map=new HashMap<String, Object>();
                map.put("name", edit.getText().toString());
                list.set(position, map);
                // 然后刷新一下
                notifyDataSetChanged();

                }
            }
        }).setNegativeButton("取消", null).show();
        super.onListItemClick(l, v, position, id);
    }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问