1

テキストビューとチェックボックスを備えたリストビューがあります。テキスト値は HashMap に格納されます。データは DB から取得されます。Map キーは ID で、Map 値は場所です。HashMap をアダプターに渡すと、位置 0 (ゼロ) から印刷が開始されます。このため、null テキスト (値なし) を取得し、HashMap で 1 つの値 (最後の値) を見逃しています。HashMap をデータ ソースとして使用できませんか?

編集:ここにコードがあります

public class LocationActivity extends Activity{

myAdapter myA;
Map<Integer,String> locLables;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.locationmain);

    ListView listview = (ListView) findViewById(R.id.listView1);

    String selectQuery = "SELECT  * FROM " + DatabaseHandler.TABLE_LOCATIONLABLES;
    SQLiteDatabase db = new DatabaseHandler(this).getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    locLables = new HashMap<Integer, String>();

    if(cursor != null){
        if (cursor.moveToFirst()) {
            do {

                locLables.put(new Integer(cursor.getString(0)),cursor.getString(1));
                System.out.println(cursor.getString(0)+"  "+ cursor.getString(1));
            } while (cursor.moveToNext());
        }
    }
    cursor.close();db.close(); 
    //System.out.println(locLables);
    myA = new myAdapter(this, locLables, listview);
    listview.setAdapter(myA);



    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            if(arg2 == 0 )
                arg2 = arg2+1;
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), locLables.get(arg2), Toast.LENGTH_SHORT).show();

        }
    });
}

ここにアダプタークラスがあります

class myAdapter extends BaseAdapter{

ListView listView;
Activity cntx;
Map<Integer,String> locations;
List<Integer> locids = new LinkedList<Integer>();

public myAdapter(Activity context,Map<Integer,String> locLables, ListView lv){
    cntx = context;
    locations = locLables;
    listView = lv;
    System.out.println(locations);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return locations.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View row=null;
    final int posi = position;

    LayoutInflater inflater=cntx.getLayoutInflater();
    row=inflater.inflate(R.layout.locationmain_entry, null);

    TextView tv = (TextView)row.findViewById(R.id.textView12);
    final CheckBox cb = (CheckBox)row.findViewById(R.id.checkBox12);

    if(locids.contains(posi))
        cb.setChecked(true);

    //here I get a null value as the first element and misses the last value..

        tv.setText(locations.get(position));

    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if(isChecked){
                System.out.println("Chedked" + posi+ locations.get(posi));
                //cb.setChecked(true);
                if(!locids.contains(posi))
                    locids.add(posi);
                //System.out.println(locids.get(posi));
            }else if(!isChecked){
                //cb.setChecked(false);
                if(locids.contains(posi))
                    locids.remove(new Integer(posi));
                System.out.println("NOt checked" +posi + locations.get(posi));
            }
        }
    });

    return row;
}

Map 内の要素の数だけ実行されます。0 (位置変数による) を使用しているため、最後の値も欠落しています。

4

1 に答える 1

0

あなたができることは

ArrayList<Hashmap<Integer,String>> myList = new ArrayList<Hashmap<Integer,String>>();

 if(cursor != null){
    if (cursor.moveToFirst()) {
        do {
            locLables = new HashMap<Integer, String>();
            locLables.put(new Integer(cursor.getString(0)),cursor.getString(1));
            myList.add(locLables);
           } while (cursor.moveToNext());
    }
}
cursor.close();db.close(); 
myA = new myAdapter(this, myList, listview);
listview.setAdapter(myA);

「Map locLables」の代わりに「ArrayList> myList」を保持するように「myAdapter」アダプターのコンストラクターを変更する必要があります。

これは役に立ちます...問題が解決しない場合はお知らせください..

于 2012-09-12T10:41:55.553 に答える