在做一个题库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="修改" />