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

CursorAdapter를 만들어봤는데 오류가 납니다. [closed]

0 추천
package com.example.gradecal;

import 생략(자동 import사용)

public class ScdFrag extends Fragment {

	DBHelper dbHelper;
	SQLiteDatabase db;
	MyAdapter adapter;
	ListView list;
	Cursor cursor;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootview = inflater.inflate(R.layout.sub_2, container, false);

		dbHelper = new DBHelper(getActivity());
		db = dbHelper.getWritableDatabase();

		list = (ListView) rootview.findViewById(R.id.grade_lst);

		cursor = db.rawQuery("SELECT * FROM grade", null);

		if (cursor.getCount() > 0) {
			getActivity().startManagingCursor(cursor);
			adapter = new MyAdapter(getActivity(), cursor);
			list.setAdapter(adapter);
		}

		// getActivity().startManagingCursor(cursor);

		// adapter = new MyAdapter(getActivity(), cursor);

		// String[] from = {"_id", "sco"};
		// int[] to = {android.R.id.text1, android.R.id.text2};

		// SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
		// android.R.layout.simple_list_item_2, cursor, from, to);

		// list.setAdapter(adapter);

		return rootview;
	}

	class MyAdapter extends CursorAdapter {

		@SuppressWarnings("deprecation")
		public MyAdapter(Context context, Cursor c) {
			super(context, c);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			// TODO Auto-generated method stub

			TextView txt1 = (TextView) view.findViewById(R.id.no);
			TextView txt2 = (TextView) view.findViewById(R.id.tot);
			TextView txt3 = (TextView) view.findViewById(R.id.sco);
			TextView txt4 = (TextView) view.findViewById(R.id.gra);

			// String no = cursor.getString(cursor.getColumnIndex("_id"));
			// String tot = cursor.getString(cursor.getColumnIndex("tot"));
			// String sco = cursor.getString(cursor.getColumnIndex("sco"));
			// String gra = cursor.getString(cursor.getColumnIndex("gra"));

			txt1.setText("번호 : " + context.getString(0));
			txt2.setText("학점 : " + context.getString(1));
			txt3.setText("점수 : " + context.getString(2));
			txt4.setText("등급 : " + context.getString(3));

		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			// TODO Auto-generated method stub

			LayoutInflater inflater = LayoutInflater.from(context);
			// LayoutInflater inflater = (LayoutInflater) mContext
			// .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View v = inflater.inflate(R.id.grade_lst, parent, false);
			return v;

		}

	}

}

프레그먼트에 커서어댑터 만들어서 커스텀 리스트뷰에 뿌리려는데 잘 안되네요 ㅠㅠ 

왜 그런 걸까요;

 
 
질문을 종료한 이유: 스스로 고쳐서 성공했습니다 ㅠㅠ
생초보개발자준비생 (250 포인트) 님이 2014년 12월 14일 질문
생초보개발자준비생님이 2014년 12월 15일 closed
안녕하세요! 저도 이방법으로 하고있는데 혹시 어떻게 수정했는지 알 수 있을까요?

1개의 답변

0 추천
public class DataListView extends ListActivity {
    
    private ArrayList<String> results = new ArrayList<String>();
    private String tableName = DBHelper.tableName;
    private SQLiteDatabase newDB;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        openAndQueryDatabase();
        
        displayResultList();
        
        
    }
    private void displayResultList() {
        TextView tView = new TextView(this);
        tView.setText("This data is retrieved from the database and only 4 " +
                "of the results are displayed");
        getListView().addHeaderView(tView);
        
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results));
        getListView().setTextFilterEnabled(true);
        
    }
    private void openAndQueryDatabase() {
        try {
            DBHelper dbHelper = new DBHelper(this.getApplicationContext());
            newDB = dbHelper.getWritableDatabase();
            Cursor c = newDB.rawQuery("SELECT FirstName, Age FROM " +
                    tableName +
                    " where Age > 10 LIMIT 4", null);

            if (c != null ) {
                if  (c.moveToFirst()) {
                    do {
                        String firstName = c.getString(c.getColumnIndex("FirstName"));
                        int age = c.getInt(c.getColumnIndex("Age"));
                        results.add("Name: " + firstName + ",Age: " + age);
                    }while (c.moveToNext());
                } 
            }           
        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        } finally {
            if (newDB != null) 
                newDB.execSQL("DELETE FROM " + tableName);
                newDB.close();
        }

    }
    
}


바로 어뎁터로 연결하시지 말고 하나씩 추가(add) 하세요


doridori2013@nate.com

 

 

익명사용자 님이 2014년 12월 14일 답변
...