1

I am working on an application in Android that works with 2 screens. The firsts creen is simple, it has a TextView initialzed to "Not Set" and a Button. When the user clicks on the button, he/she is taken to the 2nd screen which is one big ListView. The ListView is a list of all highest grossing movies of all time. It would be simple if it were to include just the title, but I need to create the listview such that it has multiple lines. The structure being, the title is oriented left, the gross earnings aligned right and the date released is located on a second row.

The important tidbit is that I need to use string arrays declared at the strings.xml. I have already declared all of them, however my problem is that I am stuck on how to make the Java portion. Here is what I have come up so far.

package com.android.rondrich;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Lab5_082588part2 extends ListActivity {

    public static final String KEY = "KEY";
public static final String RETURN = "RETURN";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String[] titleList = getResources().getStringArray(R.array.title_array);
    String[] grossList = getResources().getStringArray(R.array.gross_array);

    setListAdapter(new ArrayAdapter<String>(this,
            R.layout.lab5_082588part2, grossList)); //not sure if     correct to have 2 setListAdapters

    setListAdapter(new ArrayAdapter<String>(this,
            R.layout.lab5_082588part2, titleList));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

                Intent intent = getIntent();
                String title = ((TextView) view).getText().toString();
                intent.putExtra(Lab5_082588part2.RETURN, title);
                setResult(RESULT_OK, intent);
                finish();

            }
        });
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        finish();
    }

}

Some of the snippets I have researched are using this kind of approach (sample code)

private ArrayList<SearchResults> GetSearchResults(){
 ArrayList<SearchResults> results = new ArrayList<SearchResults>();

 SearchResults sr = new SearchResults();
 sr.setName("Justin Schultz");
 sr.setCityState("San Francisco, CA");
 sr.setPhone("415-555-1234");
 results.add(sr);

However this method will prove very tedious for long lists. The question is that:

How do I incorporate using string arrays declared in strings.xml to have multi-line listview without resorting to hardcoding it like above?

2 Answers 2

1

You can't set the adapter two times to show the row like you want instead you have to implement a custom adapter(there are thousands of tutorials out there to look at so I'll leave this part out).

To extract the data from the strings arrays you would(almost) use the snippet you posted:

String[] titleList = getResources().getStringArray(R.array.title_array);
String[] grossList = getResources().getStringArray(R.array.gross_array);
// ...

private ArrayList<SearchResults> GetSearchResults(){
    ArrayList<SearchResults> results = new ArrayList<SearchResults>();
    // make sure the arrays have the same length
    for (int i = 0; i < titleList.length; i++) {
        SearchResults sr = new SearchResults();
        sr.setTitle(titleList[i]);
        sr.setGross(grossList[i]);
        results.add(sr);
   }
   return results;
}

Then you would use the list returned by this method to fill your custom adapter.

A layout example:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="title" />

    <TextView
        android:id="@+id/gross"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/title"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Gross" />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/title"
        android:text="Date" />

</RelativeLayout>
Sign up to request clarification or add additional context in comments.

3 Comments

I understand now, but the only thing I don't is how to format the results (alignment), since just using the snippet will just be stacking them vertically on top of each other.
@Erasmus The alignment is done in the layout file, not in code. See my edited answer.
Well that totally makes a lot of sense. I was quite lost awhile ago when i was coding since I never knew how to not hardcode the entire list. Thank you very much sir, much appreciated help!
0

How to make custom ListView items you can read in this tutorial: http://www.ezzylearning.com/tutorial.aspx?tid=1763429

If you want to get String from your strings.xml use that in your ativity class:

getContext().getResources.getString(R.string.your_text);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.