0

I'm working on android application that shows number and name of persons , using ListView .But the compilers doesn't recognize "champs1" and "champs2".

public class Second extends Activity {
ListView vue;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act2);
    vue=(ListView) findViewById(R.id.listV2);

    String [][] rep=new String[][]{{"1","aaaaaaa"  },
                                   {"2","bbbbbbb"  },
                                   { "3","ccccccc" }     };
    List<HashMap<String,String> > list= new ArrayList<HashMap<String, String>>();
    HashMap<String,String> element;
    for(int i=0 ;i<rep.length;i++ ){
         element =new HashMap<String, String>();
        element.put("chmaps1",rep[i][0]);
        element.put("champs2",rep[i][1]);
        list.add(element);
    }
    ListAdapter adapter=new SimpleAdapter(this,
                                           list,
                                           android.R.layout.simple_list_item_2,
                                           new String[] {"chmaps1","champs2"},
                                           new int[] {android.R.id.champs1, android.R.id.champs2 });

  vue.setAdapter(adapter);
}
}

Error:(39, 71) error: cannot find symbol variable champs1 Error:(39, 71) error: cannot find symbol variable champs2

ghassen92
  • 305
  • 1
  • 2
  • 12

3 Answers3

1

enter image description herechange android.R.id.champs1 to android.R.id.text1

change android.R.id.champs2 to android.R.id.text2

public class Second extends Activity { ListView vue;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_searchable);
    vue = (ListView) findViewById(R.id.listView1);

    String[][] rep = new String[][] { { "1", "aaaaaaa" },
            { "2", "bbbbbbb" }, { "3", "ccccccc" } };
    List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> element;
    for (int i = 0; i < rep.length; i++) {
        element = new HashMap<String, String>();
        element.put("chmaps1", rep[i][0]);
        element.put("champs2", rep[i][1]);
        list.add(element);
    }

    ListAdapter adapter = new SimpleAdapter(this, list,
            android.R.layout.simple_list_item_2, new String[] { "chmaps1",
                    "champs2" }, new int[] { android.R.id.text1,
                    android.R.id.text2 });

    vue.setAdapter(adapter);
       }
}
jeet parmar
  • 868
  • 8
  • 19
0

put this code in for loop

 HashMap<String, String> element = new HashMap<String, String>();

and there is no problem i think android.R.id.champs1 or R.id.champs1 both are correct .

change this one instead of this vue.setAdapter(adapter)

try like this vue.setListAdapter(adapter);

RaMeSh
  • 3,330
  • 2
  • 19
  • 31
0

You are refferring to:

  android.R.id.champs1

instead of:

R.id.champs1

Also be careful that you ar calling variables chmaps1 sometimes and champs1 some other times.

gaugeinvariante
  • 175
  • 1
  • 10