English 中文(简体)
如何在ListView中 编程关于Android的无线电组
原标题:how to program RadioGroup in the ListView on Android

I have a problem with the handle RadioGrup. The application gets the string from the OCR, which is cut and thrown into the adapter on the list of listview with 4 RadioButtons and it is already running. http://dl.dropbox.com/u/158361/screenshot-1336249818620.png

As shown in the screenshot are 4 EditText to which I return an item within a list according to which radiobuton was pressed. How to send the string after selecting raiobutton?

我的代码:

public class ocrResults extends Activity implements OnClickListener {
public static final int CAPTURE_ACTIVITY_REQUEST_CODE = 1;
public ListaAdapterResults listaAdapterRes;
public ListView id_list_tokens;     
public String[] splittedArray;
String tag = "Events";

public EditText etNazwa;
public EditText etAdres;
public EditText etGodziny;
public EditText etTelefon;
public RadioGroup radioGroup;

private View.OnClickListener onSave=new View.OnClickListener() {
   public void onClick(View v) {
      RadioSelect rs = new RadioSelect();
      rs.setItem(etNazwa.getText().toString());
      rs.setItem(etAdres.getText().toString());
      rs.setItem(etGodziny.getText().toString());
      rs.setItem(etTelefon.getText().toString());

      radioGroup=(RadioGroup)findViewById(R.id.group);

      switch (radioGroup.getCheckedRadioButtonId()) {
       case R.id.rbName:
           rs.setType("rbName");
           //etNazwa.setText(splittedArray[pos]);
           break;
       case R.id.rbAdres:
           rs.setType("rbAdres");
           break;
       case R.id.rbGodziny:
           rs.setType("rbGodziny");
           break;
       case R.id.rbTelefon:
           rs.setType("rbTelefon");
           break;
      }
      etNazwa.setText("");
      etAdres.setText("");
      etGodziny.setText("");
      etTelefon.setText("");
      radioGroup.clearCheck();
      Log.d(tag," potwierdz");
  }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.ocrresultsactivity);
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

   id_list_tokens = (ListView)findViewById(R.id.id_list_tokens);
   etNazwa  =(EditText)findViewById(R.id.EtResNazwa);
   etAdres  =(EditText)findViewById(R.id.EtResAdres);
   etGodziny=(EditText)findViewById(R.id.EtResGodziny);
   etTelefon=(EditText)findViewById(R.id.EtResTelefon);

   View btn_Zatw1 = findViewById(R.id.btnZatw1);
    btn_Zatw1.setOnClickListener(onSave);//this
   View btn_OCRmake1 = findViewById(R.id.btnOCRmake1);
    btn_OCRmake1.setOnClickListener(this);
   View btn_backtomain1 = findViewById(R.id.btnbacktomain1);
    btn_backtomain1.setOnClickListener(this);
   }
   @Override
   public void onClick(View v) {
  switch (v.getId()){
     case R.id.btnOCRmake1:
        Intent myIntent = new Intent(ocrResults.this, CaptureActivity.class);
        startActivityForResult(myIntent, CAPTURE_ACTIVITY_REQUEST_CODE);
        break;
     case R.id.btnbacktomain1:
        startActivity(new Intent(ocrResults.this, and4ph.class));
        finish();
        break;
  }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (resultCode == RESULT_OK) {
        if (CAPTURE_ACTIVITY_REQUEST_CODE == requestCode) {
            String response = data.getStringExtra(edu.sfsu.cs.orange.ocr.CaptureActivity.RESPONSE);
            insertResponse(response);
        }
   }
    }
    private void insertResponse(String response) {
    String strResult = response;
    splittedArray = strResult.split(" ");   //(strResult,    );
    Context context = getApplicationContext();

    initListViewResp();
    }
    void initListViewResp() {           //Inicjalizacja ListView
    listaAdapterRes = new ListaAdapterResults(this, splittedArray);
    id_list_tokens.setAdapter(listaAdapterRes);

    //      if (mbutton[0] == true) { 
    //      mbutton[0]=holder.rb_Name;
    //       } else {
    //             // Get the ViewHolder back to get fast access to the ImageView.
    //             holder = (ViewHolder) convertView.getTag();
    //             holder.dealImage.setBackgroundColor(Color.WHITE);
    //             dealImage = holder.dealImage;
    //         }


    id_list_tokens.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
          Toast.makeText(getApplicationContext(), splittedArray[pos], Toast.LENGTH_SHORT).show();
        }
    });
    //getCheckedRadioButtonId();
    updateListView();
}
private void updateListView() {
    listaAdapterRes.notifyDataSetChanged();
}    
    }

和我的适应器:

public class ListaAdapterResults extends ArrayAdapter<String> {
    String tag = "Events";
    private Activity context;
    private String[] splittedArray;

    public ListaAdapterResults(Activity context, String[] splittedArray){
        super(context, R.layout.wiersz_tokenu, splittedArray);
        this.context        = context;
        this.splittedArray  = splittedArray;
    }
    public long getItemId(int position) {
        return position;
    }
    static class ViewHolder {
        private TextView     tv_Token;
        private RadioButton rb_Name;
        private RadioButton rb_Adres;
        private RadioButton rb_Godziny;
        private RadioButton rb_Telefon;
        private RadioGroup rgroup;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(tag," 3");
        View rowView = convertView;
        ViewHolder holder= null;
        RadioButton[] mbutton = null;

        if(rowView == null) {
          LayoutInflater inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          rowView = inflator.inflate(R.layout.wiersz_tokenu, parent, false);
          holder = new ViewHolder();
          holder.rgroup     = (RadioGroup) rowView.findViewById(R.id.group);
          holder.tv_Token   = (TextView) rowView.findViewById(R.id.tvToken);

          holder.rb_Name    = (RadioButton)rowView.findViewById(R.id.rbName);
          holder.rb_Adres   = (RadioButton)rowView.findViewById(R.id.rbAdres);
          holder.rb_Godziny = (RadioButton)rowView.findViewById(R.id.rbGodziny);
          holder.rb_Telefon = (RadioButton)rowView.findViewById(R.id.rbTelefon);

          mbutton=new RadioButton[4];
            mbutton[0]=holder.rb_Name;
            mbutton[1]=holder.rb_Adres;
            mbutton[2]=holder.rb_Godziny;
            mbutton[3]=holder.rb_Telefon;

          for(int i=0;  i<4; i++) {
              mbutton[i]=new RadioButton(context);
              //mbutton[l].setText("test"+l);
              //holder.rgroup.addView(mbutton[i]);
          }

          holder.rgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
               public void onCheckedChanged(RadioGroup mRadioGroup, int checkedId) {

                   for(int i=0; i<mRadioGroup.getChildCount(); i++) {

                       RadioButton rbtn = (RadioButton) mRadioGroup.getChildAt(i);
                       //int t=table.indexOfChild(table_row);   //System.out.println(t);

                       int t = mRadioGroup.getId();
                       System.out.println(t);

                       if(rbtn.getId() == checkedId) {
                           String text = rbtn.getText().toString();
                           // do something with text
                           //etNazwa.setText(text);

                           RadioSelect rs = new RadioSelect();
//                        rs.setItem(etNazwa.getText().toString());
//                        rs.setItem(etAdres.getText().toString());
//                        rs.setItem(etGodziny.getText().toString());
//                        rs.setItem(etTelefon.getText().toString());
//                        mRadioGroup=(RadioGroup)findViewById(R.id.radioGroupTypET);

                          switch (mRadioGroup.getCheckedRadioButtonId()) {
                           case R.id.rbName:
                               rs.setType("rbName");
                                     //String text = rbtn.getText().toString();
                                     //etNazwa.setText(text);
                               break;
                           case R.id.rbAdres:
                               rs.setType("rbAdres");
                               break;
                           case R.id.rbGodziny:
                               rs.setType("rbGodziny");
                               break;
                           case R.id.rbTelefon:
                               rs.setType("rbTelefon");
                               break;
                          }
                          Log.d(text," radioButtonSelect");
                          return;
                        }
                    }
                }
          });
          rowView.setTag(holder);
          Log.d(tag,"me");
       } else {
          holder = (ViewHolder) convertView.getTag();
//        mbutton = holder.rgroup;
          Log.d(tag,"meeee");
       }
       ViewHolder holder1 = (ViewHolder) rowView.getTag();
       holder1.tv_Token.setText(splittedArray[position]);
       Log.d(tag," event7");

       holder.tv_Token.setText(splittedArray[position]);
       holder.rb_Name.setTag(new Integer(position));
       holder.rb_Adres.setTag(new Integer(position));
       holder.rb_Godziny.setTag(new Integer(position));
       holder.rb_Telefon.setTag(new Integer(position));
//      public void onCheckedChanged(RadioGroup rgroup,int checkedId){  }
      return rowView;
    }
}
最佳回答

In your getView method in holder.rgroup.setOnCheckedChangeListener set value for editText. e.g.

  String text = rbtn.getText().toString();
     etNazwa.setText(text);

看来您有这个代码被注释过, 但参数错误 。 您有 etNazwa. setText( 响应); , 应该是 etNazwa. setText( 文本);

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...