wilbomc
wilbomc

Reputation: 183

ListView and Adapter Guidance

At a crossroads with ListView and Adapter issues. The idea is to display a list of soccer teams followed by the goals they scored and points they attained in a ListView that were brought in (as arrays) to the Activity using an Intent.

However, I can only display club names and having issues with displaying integer arrays that contain the points and goals scored. I have looked around and found topics pertaining to custom adapters but I am new to Android development and do not fully understand them.

I have tried converting the integer arrays to strings with no success. I do not know where to go from here so any advice would be welcome. My code is:

package com.example.wmcpapersoccer;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ResultsActivity extends Activity 
{
TextView txtShow;
TextView txtGF;
ListView listview;

String team_0;
String team_1;
String team_2;
String team_3;
String team_4;
String team_5;
String team_6;
String team_7;

int goalsFor_0 = 0;
int goalsFor_1 = 0;
int goalsFor_2 = 0;
int goalsFor_3 = 0;
int goalsFor_4 = 0;
int goalsFor_5 = 0;
int goalsFor_6 = 0;
int goalsFor_7 = 0;

int points_0 = 0;
int points_1 = 0;
int points_2 = 0;
int points_3 = 0;
int points_4 = 0;
int points_5 = 0;
int points_6 = 0;
int points_7 = 0;



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

    Intent intent_team_data = getIntent();
    String[] club_names = intent_team_data.getStringArrayExtra("footballClubs");
    int[] team_points = intent_team_data.getIntArrayExtra("clubPoints");
    int[] club_goals = intent_team_data.getIntArrayExtra("clubGoals");

    listview = (ListView)findViewById(R.id.listClubs);
    ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.custom_list_view, club_names);
    listview.setAdapter(myAdapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.results, menu);
    return true;
}

}

Updated with custom_list_view xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listClubs"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#ffffff"
/>

Upvotes: 0

Views: 92

Answers (3)

Coderji
Coderji

Reputation: 7745

Based on your layout.. you are using almost same layout that Android provided as a default, however the android:id is critical in placing the text from array to the view.

so to fast fix this, I would use Android layout directly and remove your custom layout. to use the default layout change the setting adapter line to the following:

 ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.simple_list_item_1, club_names);

android.R.simple_list_item_1 this is the path to the default layout.

if you still want to use your layout, then you need to change the android:id to

android:id="@android:id/text1"

hope this works for you.

Upvotes: 1

Karakuri
Karakuri

Reputation: 38585

It would make more sense to create an object that encapsulates the data pertaining to a single team -- name, goals scored, pointe earned, etc. -- rather than to have multiple disparate arrays that you must coordinate and maintain separately.

Assuming you had such an object, you could easily extend ArrayAdapter and provide that as the parameterized type (e.g. ArrayAdapter<TeamInfo>). Or, you could go a little further and write a custom adapter implementation by extending BaseAdapter.

I recommend you watch The World of ListView as it will explain everything you need to understand about working with ListViews and adapters.

Upvotes: 1

zhenghuiyan
zhenghuiyan

Reputation: 324

What you need is creating a adapter which extends BaseAdapter . In this way,You can custom the ListView item's layout.

A simple example is:

/** a custom Adapter */  
public class MyAdapter extends BaseAdapter {  
    private LayoutInflater mInflater; 
    private List<Model> list;

    public MyAdapter(Context context,List<Model> list) {  
        this.mInflater = LayoutInflater.from(context);
        this.list = list;
    }  

    @Override  
    public int getCount() {  
        return mData.size();// size of data 
    }  

    @Override  
    public Object getItem(int position) {  
        return list.get(position);  
    }  

    @Override  
    public long getItemId(int position) {  
        return position;  
    }  

    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        convertView = mInflater.inflate(R.layout.list_item, null);//get your custom item layout  

        //get item's views.set the data 
        your code here.

        return convertView;  
    }  
}  

Upvotes: 1

Related Questions