마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 db 사용중에 리스트뷰 OnClick 메소드 사용하여 에디트뷰에 출력

0 추천

 

사진과 같이 에디트텍스트에 1번 리스트를 클릭하게 되면 aaa/111 이들어가야하는데 ; 

com.example...... 과 같은 패키지에 들어가는 내용이 출력되네요  ;

무엇이 문제인지 모르겠습니다 ㅜ . 

public class MainActivity extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener {

    SQLiteDatabase myDB;
    SimpleAdapter myADT;
    ArrayList<String> aryMBRList;
    ArrayAdapter<String> adtMembers;
    ListView lstView;
    ArrayList<AdepterDTO> arr_list = null;
    DBHandler dbHandler;
    Button btnInsert, btnUpdate, btnSerch, btnDelete,btnSelect;
    EditText edtid,edtCarName, edtCarPower;
    Cursor cursor = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        edtid =(EditText)findViewById(R.id.editid);
        edtCarName = (EditText) findViewById(R.id.editText);
        edtCarPower = (EditText) findViewById(R.id.editText2);

        btnInsert = (Button) findViewById(R.id.btnInsert);
        btnUpdate = (Button) findViewById(R.id.btnupdate);
        btnDelete = (Button) findViewById(R.id.btnDelete);
        btnSerch = (Button) findViewById(R.id.btnSerch);
        btnSelect = (Button) findViewById(R.id.btnSelect);

        btnInsert.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnSerch.setOnClickListener(this);
        btnSelect.setOnClickListener(this);

        lstView = (ListView) findViewById(R.id.list);
        lstView.setOnItemClickListener(this);
        lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        dbHandler = DBHandler.open(this);

        if (v.getId() == R.id.btnInsert) {
            if ( edtCarName.getText().toString().equals("")) {
                Toast.makeText(getApplicationContext(), "�̸� �Է�", Toast.LENGTH_LONG).show();
                return;
            } else if ( edtCarPower.getText().toString().equals("")) {
                Toast.makeText(getApplicationContext(), "��ⷮ �Է�", Toast.LENGTH_LONG).show();
                return;
            }

            long re = dbHandler.insert(
                    edtCarName.getText().toString(),
                   edtCarPower.getText().toString());

            if (re == 0) {
                Toast.makeText(getApplicationContext(), "�߰� ����", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "�߰� ����", Toast.LENGTH_LONG).show();


                cursor = dbHandler.selectAll();

                arr_list = new ArrayList<AdepterDTO>();

                while (cursor.moveToNext()) {
                    String id = cursor.getString(0);
                    String name = cursor.getString(1);
                    String power = cursor.getString(2);

                    AdepterDTO dto = new AdepterDTO(id,name,power);
                    arr_list.add(dto);

                }
                cursor.close();
                invalidate();
            }

        } else if (v.getId() == R.id.btnSelect) {
            cursor = dbHandler.selectAll();
            //     arr = new String[cursor.getCount()];
            arr_list = new ArrayList<AdepterDTO>();
            // int count = 0;
            while (cursor.moveToNext()) {
                String id = cursor.getString(0);
                String name = cursor.getString(1);
                String power = cursor.getString(2);

                AdepterDTO dto = new AdepterDTO(id, name, power);
                arr_list.add(dto);
            }
            cursor.close();
            invalidate();
        }else if(v == btnDelete){

            String id = edtid.getText().toString();

            dbHandler.delete(id);
        }else if(v==btnUpdate){

            String id = edtid.getText().toString();
            String name = edtCarName.getText().toString();
           String power = edtCarPower.getText().toString();

            dbHandler.update(id,name,power);
        }

    }
        private void invalidate() {

        CarAdapter caradapter = new CarAdapter(this, R.layout.itemlist, arr_list);
        lstView.setAdapter(caradapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {

    


           String a = lstView.getItemAtPosition(position).toString();

        edtid.setText(a);
        edtCarName.setText(a);
        edtCarPower.setText(a);


    }

    public class CarAdapter extends ArrayAdapter<AdepterDTO> {


        private ArrayList<AdepterDTO> items;

        public CarAdapter(Context context, int textViewResourceId, ArrayList<AdepterDTO> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

            @Override

            public View getView(int _position, View _convertView, ViewGroup _parent) {
                View view = _convertView;
                if (view == null) {
                    LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = vi.inflate(R.layout.itemlist,null);
                }

                AdepterDTO ref = items.get(_position);
                if (ref != null) {
                    TextView txt_id = (TextView) view.findViewById(R.id.id);
                    TextView txt_name = (TextView) view.findViewById(R.id.name);
                    TextView txt_power = (TextView) view.findViewById(R.id.power);

                    if (txt_id != null)
                        txt_id.setText(ref.getId());
                    if (txt_name != null)
                        txt_name.setText(ref.getName());
                    if (txt_power != null)
                        txt_power.setText(ref.getPower());

                }
                return view;


            }



    }
public class AdepterDTO {

    private String id;
    private String name;
    private String power;


    public AdepterDTO(String id, String name, String power) {
        this.id = id;
        this.name =name;
        this.power =power;

    }

    public String getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public String getPower() {
        return this.power;
    }


}

 

코드첨부 합니다 ㅜ .. 도와주세요 ㅜ 

익명사용자 님이 2015년 6월 11일 질문

1개의 답변

0 추천

@Override

public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) 
{
  String a = lstView.getItemAtPosition(position).toString();
  edtid.setText(a);
  edtCarName.setText(a);
  edtCarPower.setText(a);
}

이 부분을 짜다 말은 것 같네요. 

 

 

--------- 수정 ---------

https://github.com/susemi99/TestDBListView/blob/master/src/kr/mint/testdblistview/MainActivity.java 의 맨 아래쪽을 보세요

쎄미 (162,410 포인트) 님이 2015년 6월 11일 답변
쎄미님이 2015년 6월 12일 수정
어떤 부분을 더 구현 해야될지 ㅜ 힌트라도 주실수있을까요 ㅜ 포지션 불러와서 셋텍스로 넘기면 될줄 알았는데 ㅜ ㅜ
...