0

i am new to android development, and i just learnt about list views and array adapters etc. I have a project in android studio that has multiple activities and in one i have a array adapter and a list view, but every time i run the app and switch to the activity that has the list view in it the app does not respond and it closes. I have checked the code and it is the same as the code in the several tutorials i have seen. I think that it has something to do with the multiple activities i have, can someone help me?

public class StoreActivity extends ActionBarActivity {

String[] storeList = {"Ship 1", "Ship 2", "Ship 3"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_store);

    ListView store;
    store = (ListView) findViewById(R.id.list);

    ArrayAdapter<String> adapter = new ArrayAdapter(this, R.layout.row_layout, storeList);
    store.setAdapter(adapter);
}
7
  • 1
    check ur logs that will give u idea
    – KOTIOS
    Aug 14, 2014 at 16:45
  • 3
    Can you post the exception from the logcat?
    – ariets
    Aug 14, 2014 at 16:46
  • 1
    Also let us see associated XML layouts in case logcat is not enough. Aug 14, 2014 at 17:04
  • 1
    sorry i cant post the errors from the logcat its 5000 characters long :l (sorry if i sound like a moron)
    – Don Grey
    Aug 14, 2014 at 17:07
  • Post only the messages with level "E" (error) the red ones :)
    – Jorgesys
    Aug 14, 2014 at 17:08

2 Answers 2

1

Change this line

    ArrayAdapter<String> adapter = new ArrayAdapter(this, R.layout.row_layout, storeList);

to this:

ArrayAdapter<String> adapter = new ArrayAdapter(this, R.layout.row_layout, R.id.TextViewofRow_layoutstoreList, storeList);

You need to say in which textview you will put your data.

0
1

If you are unsing an ArrayAdapter this is the correct way to initialize it:

  ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, storeList);

for example:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_store);

    ListView store;
    store = (ListView) findViewById(R.id.list);

    //ArrayAdapter<String> adapter = new ArrayAdapter(this, R.layout.row_layout, storeList);
     ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, storeList);
    store.setAdapter(adapter);
}

but supossing that you want to load data inside your custom row layout (row_layout.xml), add the id of the textView (i think this is what you need) :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_store);

    ListView store;
    store = (ListView) findViewById(R.id.list);

    //ArrayAdapter<String> adapter = new ArrayAdapter(this, R.layout.row_layout, storeList);
     ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.row_layout,  R.id.myTextView, storeList);
    store.setAdapter(adapter);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.