0

The class is an extension of another class and is experiencing an error when I mess with the listview. Can anyone help me? Layout: GridLayout

XML:

<ListView
    android:id="@+id/lvContexts"
    android:layout_width="899dp"
    android:layout_height="356dp"
    android:layout_column="1"
    android:layout_columnSpan="3"
    android:layout_gravity="left"
    android:layout_row="3"
    android:layout_rowSpan="2" >
</ListView>

Class:

public class EditarContext extends Iniciar {
    ....

    lvContext = (ListView) findViewById(R.id.lvContexts);
ArrayList<String> lvList = (ArrayList<String>) contexts;
ArrayAdapter<String> spArrayAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, lvList);
lvContext.setAdapter(spArrayAdapter);
lvContext.setClickable(true);
lvContext.setVisibility(View.VISIBLE);
lvContext.setVisibility(View.GONE);
}

OBS: The contents of the ListView is being picked content spinner.

Errors are these but I do not miss any of them: https://i.stack.imgur.com/Gdsdt.png

Suh
  • 5
  • 2

3 Answers3

0

try this :

public class YourActivity extends Activity 
{
     private ListView lvContext;

     public void onCreate(Bundle saveInstanceState) {
        setContentView(R.layout.your_layout);

        lvContext = (ListView) findViewById(R.id.your_list_view_id);
        // Instanciating an array list
        ArrayList<String> lvList = new ArrayList<String>();
        lvList.add("foo");
        lvList.add("bar");

        // This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
        ArrayAdapter<String> spArrayAdapter =      
        new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, lvList);

        lvContext.setAdapter(spArrayAdapter); 
}

}

Hendy
  • 265
  • 1
  • 6
  • 27
  • I created a method and put it inside. So it worked. Only needed adjustments. Thank you for your help. : D – Suh Nov 11 '13 at 04:15
0

To use findViewByID you should extend the activity. If not, you need to instantiate your activity by passing it as parameter to the constructor.

EditarContext instance = new EditarContext(this);

..

public class EditarContext extends Iniciar {

public Activity activity; 
//.... other attributes 

public EditarContext( Activity _activity){

   this.activity = _activity;
//other initializations...

}
}

Then you can use findViewById like this:

lvContext = (ListView)this.activity.findViewById(R.id.lvContexts); 

Hope this helps.

N20084753
  • 2,130
  • 2
  • 17
  • 12
0
// try this
View  view = LayoutInflater.from(MyActivity.this).inflate(R.layout.XML,null,false);
lvContext = (ListView) view.findViewById(R.id.lvContexts);
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67