android笔试添加自定义服务,Android之Listview(item为单选题)自定义adapter,像考试时前面的10几道单选题的实现...
用于展現重復性的東西,Listview比較好用,看了別人的自定義Adapter(item是EditText,能夠很好地獲取到每一個item的EditText值)。又由于在做項目的需要,故特制了一個item包含RadioGroup的Listview的自定義Adapter。
主要功能:
1.將任意數目的單選題(題干和4個選項)展現在界面上;
2.每一題都選擇后,提交,可以講每一題的答案獲取到
涉及到兩個entity:
public class TopicItem implements Serializable {
private String questionId;//題目id
private String question;//題干
private String answerId;
private String userAnswerId; //選擇的答案選項
private List optionList;
public String getQuestionId() {
return questionId;
}
public void setQuestionId(String questionId) {
this.questionId = questionId;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getAnswerId() {
return answerId;
}
public void setAnswerId(String answerId) {
this.answerId = answerId;
}
public String getUserAnswerId() {
return userAnswerId;
}
public void setUserAnswerId(String userAnswerId) {
this.userAnswerId = userAnswerId;
}
public List getOptionList() {
return optionList;
}
public void setOptionList(List optionList) {
this.optionList = optionList;
}
}
public class OptionItem implements Serializable {
private String answerOption;//選項序號
private String answer;//選項文字
public String getAnswerOption() {
return answerOption;
}
public void setAnswerOption(String answerOption) {
this.answerOption = answerOption;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String anwer) {
this.answer = anwer;
}
}
自定義的adapter:
public class TopicAdapter extends BaseAdapter {
private static final String TAG = "TopicAdapter" ;
String KEY = "list_topic_item";
private LayoutInflater mInflater;
private Context context;
private List> mData;
public TopicAdapter(Context context, List> data) {
mData = data;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
init();
}
private void init() {
mData.clear();
}
@Override
public int getCount() {
int count = mData == null ? 0 : mData.size();
return count ;
}
@Override
public Object getItem(int position) {
/*Object obj = records != null && records.size() > position ? records.get(position) : null;
return obj;*/
return null;
}
@Override
public long getItemId(int position) {
//return position;
return 0;
}
private Integer index = -1;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder = null;
if( convertView == null ){
convertView = mInflater.inflate(R.layout.topic_item, null);
holder = new Holder() ;
holder.question = (TextView)convertView.findViewById(R.id.topic_item_question);
holder.option = (RadioGroup) convertView.findViewById(R.id.topic_item_option);
holder.option1 = (RadioButton) convertView.findViewById(R.id.topic_item_option1);
holder.option2 = (RadioButton) convertView.findViewById(R.id.topic_item_option2);
holder.option3 = (RadioButton) convertView.findViewById(R.id.topic_item_option3);
holder.option4 = (RadioButton) convertView.findViewById(R.id.topic_item_option4);
holder.option.setTag(position);
holder.option.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
index = (Integer) v.getTag();
}
return false;
}
});
class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
public MyOnCheckedChangeListener(Holder holder) {
mHolder = holder;
}
private Holder mHolder;
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
int position = (Integer) mHolder.option.getTag();
TopicItem item = mData.get(position).get(KEY);
int RadioButtonId = mHolder.option.getCheckedRadioButtonId();
group.check(RadioButtonId);
RadioButton rb = (RadioButton)mHolder.option.findViewById(RadioButtonId);
if(RadioButtonId==mHolder.option1.getId()){
item.setUserAnswerId(item.getOptionList().get(0).getAnswerOption().trim());
}else if(RadioButtonId==mHolder.option2.getId()){
item.setUserAnswerId(item.getOptionList().get(1).getAnswerOption().trim());
}else if(RadioButtonId==mHolder.option3.getId()){
item.setUserAnswerId(item.getOptionList().get(2).getAnswerOption().trim());
}else if(RadioButtonId==mHolder.option4.getId()){
item.setUserAnswerId(item.getOptionList().get(3).getAnswerOption().trim());
}
mData.get(position).put(KEY, item);
}
}
holder.option.setOnCheckedChangeListener(new MyOnCheckedChangeListener(holder));
convertView.setTag(holder);
}else{
holder = (Holder)convertView.getTag();
holder.option.setTag(position);
}
TopicItem item = (TopicItem)mData.get(position).get(KEY);
if( item != null ){
holder.question.setText(item.getQuestion());
holder.option1.setText(item.getOptionList().get(0).getAnswer().toString());
holder.option2.setText(item.getOptionList().get(1).getAnswer().toString());
holder.option3.setText(item.getOptionList().get(2).getAnswer().toString());
holder.option4.setText(item.getOptionList().get(3).getAnswer().toString());
if(item.getUserAnswerId().trim().equalsIgnoreCase(item.getOptionList().get(0).getAnswerOption().trim())){
holder.option.check(holder.option1.getId());
}
else if(item.getUserAnswerId().trim().equalsIgnoreCase(item.getOptionList().get(1).getAnswerOption().trim())){
holder.option.check(holder.option2.getId());
}else if(item.getUserAnswerId().trim().equalsIgnoreCase(item.getOptionList().get(2).getAnswerOption().trim())){
holder.option.check(holder.option3.getId());
}else if(item.getUserAnswerId().trim().equalsIgnoreCase(item.getOptionList().get(3).getAnswerOption().trim())){
holder.option.check(holder.option4.getId());
}
}
holder.option.clearFocus();
if (index != -1 && index == position) {
holder.option.requestFocus();
}
return convertView;
}
private class Holder{
private TextView question;
private RadioGroup option;
private RadioButton option1;
private RadioButton option2;
private RadioButton option3;
private RadioButton option4;
}
}
應用步驟:
1.定義一些變量,為后面做準備
//Adapter需要該Activity的context
public Context context;
private ListView topic_listview;
private Button button_sent;
private BaseAdapter mAdapter;
//需要將題目數據存在Map中,多個Map組成list,并傳給Adapter
//一道題對應一個Map
private List> data = new ArrayList>();
//模擬http獲取到的Json類型的數據
JSONObject data_json;
//Map存入數據時的key
String KEY = "list_topic_item";
2.初始化題目數據,這里直接本地編了幾道題,可以從服務器上獲取
private void initData(){
try {
data_json = new JSONObject();
data_json.put("result", "success");
JSONArray array = new JSONArray();
for(int i=0;i<3;i++){
JSONObject object = new JSONObject();
object.put("questionId", "1");
object.put("question", "康熙是乾隆的誰?");
object.put("answerId", "");
//一開始選擇好第三項
object.put("userAnswerId", "3");
JSONArray sarray = new JSONArray();
JSONObject sobject1 = new JSONObject();
sobject1.put("answerOption", "1");
sobject1.put("answer", "兒子");
sarray.put(sobject1);
JSONObject sobject2 = new JSONObject();
sobject2.put("answerOption", "2");
sobject2.put("answer", "爺爺");
sarray.put(sobject2);
JSONObject sobject3 = new JSONObject();
sobject3.put("answerOption", "3");
sobject3.put("answer", "父親");
sarray.put(sobject3);
JSONObject sobject4 = new JSONObject();
sobject4.put("answerOption", "4");
sobject4.put("answer", "孫子");
sarray.put(sobject4);
object.put("optionList", sarray);
array.put(object);
}
data_json.put("list", array);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3.將Listview與自定義的TopicAdapter關聯起來
this.context = this;
//獲取Listview
topic_listview = (ListView)findViewById(R.id.topic_quest_answer);
//data是題目的數據,放在Map里
mAdapter = new TopicAdapter(context, data);
topic_listview.setAdapter(mAdapter);
4.將Json中的題目數據解析并放入data,更新給Adapter,這樣關聯的Listview此時才將數據顯示出來
private void getData(){
System.out.println(data_json.toString());
data.clear();
if(data_json.optString("result").equals("success")){
if(data_json.optJSONArray("list")!=null){
for(int i=0;i
JSONObject object = data_json.optJSONArray("list").optJSONObject(i);
TopicItem topic = new TopicItem();
topic.setAnswerId(object.optString("answerId"));
topic.setQuestionId(object.optString("questionId"));
topic.setQuestion(object.optString("question"));
topic.setUserAnswerId(object.optString("userAnswerId"));
List optionList = new ArrayList();
for(int j=0;j
JSONObject object_option = object.optJSONArray("optionList").optJSONObject(j);
OptionItem option = new OptionItem();
option.setAnswerOption(object_option.optString("answerOption"));
option.setAnswer(object_option.optString("answer"));
optionList.add(option);
}
topic.setOptionList(optionList);
Map list_item = new HashMap();
list_item.put(KEY, topic);
data.add(list_item);
}
//提醒Adapter更新數據,緊接著Listview顯示新的數據
mAdapter.notifyDataSetChanged();
}
}
}
5.用戶答題,提交題目的答案時,我們可以看到所有的題目及選擇的選項:
button_sent = (Button) findViewById(R.id.topic_sent_comment);
button_sent.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String t="";
for (int i = 0; i < data.size(); i++) {
t = t+"第 "+(i+1)+" 題 ----------->您選擇了【選項"
+data.get(i).get(KEY).getUserAnswerId().trim()+"】 \n";
}
Toast.makeText(context, t, Toast.LENGTH_SHORT).show();
}
});
接下來,我們來看具體運行情況:
a.
每一題我設選項都是第三項,object.put(“userAnswerId”, “3”);
b.
接下來人工選擇,分別為4,3,2項
c.提交,可以看到提取結果正確
這里想提供一個demo,不知道怎么上傳。
demo地址
以前不知道怎么傳,現在補上
總結
以上是生活随笔為你收集整理的android笔试添加自定义服务,Android之Listview(item为单选题)自定义adapter,像考试时前面的10几道单选题的实现...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: react安装_前端大牛进阶---gt;
- 下一篇: java类的理解_Java类该怎么理解?