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

DB연동 CustomList에 뿌려주기 (JSONArray 파싱)

0 추천

처음 실행후 나오는 화면에 CustomList로 DB에있는 내용을 뿌려주고싶은데 CustomList adapters = new CustomList(MainActivity.this)에 CustomList부분이랑 setListAdapter에서 오류가 생겨요 ㅠㅠ

CustomList 에러 메시지 = Cannot resolve symbol 'CustomList'

setListAdapter = Cannot resolve method 'setListAdapter(CustomList)'

(학교과제로 나온건데 ㅠㅠ 아직 초보라서 모르겠습니다!! 그리고 메뉴바는 따로 만들어준 것인데 이렇게 코딩을 해도 DB연동이 되고 메인에 뿌려질까요..?)

MainActiity.java

public class MainActivity extends AppCompatActivity {

    private RelativeLayout mainContent;
    private RelativeLayout loginContent;

    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle drawerListener;

    private ImageView profilePhoto;
    private TextView nicOutput;

    private ImageButton homeBtn;
    private Button loginBtn;
    private Button menuBtn1;
    private Button menuBtn2;
    private Button menuBtn3;
    private ImageButton settingBtn;

    private String TAGLOG = "MainActivityLOG";

    private String data1[] = new String[0];
    private String data2[] = new String[10];
    private String data3[] = new String[30];

    String LoginInfo = null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GetDataFromServer getDataFromServer = new GetDataFromServer();
        getDataFromServer.execute("");
        CustomList adapters = new CustomList(MainActivity.this);
        setListAdapter(adapters);

        mainContent = (RelativeLayout)findViewById(R.id.mainContent);


        homeBtn = (ImageButton) findViewById(R.id.homeBtn);
        homeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
            }
        });

        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        drawerListener = new ActionBarDrawerToggle(this, drawerLayout,
                null, R.string.drawer_open, R.string.drawer_close) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };
        drawerLayout.setDrawerListener(drawerListener);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private class GetDataFromServer extends AsyncTask <String, String, String>{
        JSONArray jsonarray;
        StringBuilder builder;
        @Override
        protected String doInBackground(String... urls) {
            builder = new StringBuilder();
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(urls[0]);
            try{
                HttpResponse response = httpClient.execute(httpPost);
                StatusLine statusLine = response.getStatusLine();
                if(statusLine.getStatusCode() == 200){
                    HttpEntity entity = response.getEntity();
                    InputStream content = entity.getContent();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                    String line;
                    while ((line = reader.readLine()) != null){
                        Log.i(TAGLOG, "line: " + line);
                        builder.append(line);
                    }
                }
            }catch (Exception e){
                Log.i(TAGLOG, "Exception try1: " + e.getStackTrace());
                e.printStackTrace();
            }
            try{
                JSONArray root = new JSONArray(builder.toString());
                jsonarray = root.getJSONArray(Integer.parseInt("results"));
                for(int i=0; i<10; i++){
                    JSONObject jObject = jsonarray.getJSONObject(i);
                    data1[i] = jObject.getString("name");
                    data2[i] = jObject.getString("telNo");
                    data3[i] = jObject.getString("eMail");
                }
            }catch (JSONException e){
                Log.i(TAGLOG, "Exception: try2" + e.getStackTrace());
                e.printStackTrace();
            }
            return builder.toString();
        }
        public class CustomList extends ArrayAdapter<String>{
            private Activity context;
            public CustomList(Activity context){
                super(context, R.layout.list_item, data1);
                this.context = context;
            }
            @Override
            public View getView(int poition, View convertView, ViewGroup parent){
                LayoutInflater Inflater = context.getLayoutInflater();
                View rowView = Inflater.inflate(R.layout.list_item, null, true);
                TextView name = (TextView)rowView.findViewById(R.id.name);
                TextView telNo = (TextView)rowView.findViewById(R.id.telNo);
                TextView eMail = (TextView)rowView.findViewById(R.id.eMail);

                name.setText(data1[poition]);
                telNo.setText(data2[poition]);
                eMail.setText(data3[poition]);
                return rowView;
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(drawerListener.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerListener.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerListener.syncState();
    }

}
부탁드립니다!!ㅠㅠ 급해요!!!
JiYah (120 포인트) 님이 2017년 6월 17일 질문

1개의 답변

0 추천

CustomList class가 GetDataFromServer class 안에 있어서

MainActivity class에서 찾지 못하는 것 입니다.

 

public class CustomList extends ArrayAdapter<String> {
    ...
}

이 부분을 GetDataFromServer class 밖으로 빼서

MainActivity class 안으로 넣어보세요.

디자이너정 (42,810 포인트) 님이 2017년 6월 17일 답변
감사합니다 ㅠㅠ 어떻게 해결은 했습니다!하지만 디비값이 안뿌려져서 다른방법을 찾고있네요~
...