1

I have a customised row layout containing a Imageview and TextView for a List view. I can add a array of strings representing each row to the ArrayAdater but am having trouble populating the row ImageViews.

I have tried creating a ImageView array and added to ArrayAdaptor as follows (as I could not see a ArrayAdpter constructor that takes two Arrays as arguments):

// a string array holding the list items
    String classes[] = { "My Profile", "Splash", "Log A Dive", "Search Dives",
            "Weather: Magic Seaweed", "Irish Tides", "International Tides", "Dive PLanner" };// each class is a menu Actiitem

    ImageView images[]={(ImageView) findViewById(R.drawable.logdive),(ImageView) findViewById(R.drawable.logdive2),
            (ImageView) findViewById(R.drawable.logdive3),
            (ImageView) findViewById(R.drawable.search),(ImageView) findViewById(R.drawable.weather),
            (ImageView) findViewById(R.drawable.tides),(ImageView) findViewById(R.drawable.tides)
            ,(ImageView) findViewById(R.drawable.logdive3)};

ArrayAdapter listadaptor = new ArrayAdapter<String>(MainMenu.this,
                R.layout.single_list_row, R.id.title, classes);
        listadaptor.add(images);
setListAdapter(cutsomAdaptor);

Logcat Stack track:

01-02 05:10:45.480: E/AndroidRuntime(891): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mooney.diveapp/com.mooney.diveapp.MainMenu}: java.lang.NullPointerException

2
  • What is customAdaptor and what is listAdaptor ?? Commented Jan 2, 2014 at 10:29
  • And in your cutsomAdaptor what you have added??? And Clarify about cutsomAdaptor and listAdaptor Commented Jan 2, 2014 at 10:35

2 Answers 2

1

The point of an adapter is to link a set of data to a set of views (which are created by the AdapterView i.e. ListView) - you don't give it an array of views to use.

In this case, you need to override your ArrayAdapter's getView method which populates the layout. You need a data set which can represent both a String and an int (for the drawable resource ID), something like a Pair (not ideal but for simplicity):

ArrayList<Pair<String, Integer>> classes = new ArrayList<Pair<String, Integer>>();
classes.add(Pair.create("My Profile", R.drawable.myProfile));
// add all items
ArrayAdapter listadaptor = new ArrayAdapter<Pair<String, Integer>>(MainMenu.this,
            R.layout.single_list_row, R.id.title, classes) {
    public View getView(int position, View convertView, ViewGroup container) {
        if(convertView == null) {
            convertView = LayoutInflator.from(getContext()).inflate(R.layout.single_list_row, container, false);
        }
        TextView title = (TextView) convertView.findViewById(R.id.title);
        Pair<String, Integer> item = getItem(position);
        title.setText(item.first);
        title.setCompoundDrawables(item.second, 0, 0, 0);
        return convertView;
    }
 };
Sign up to request clarification or add additional context in comments.

1 Comment

That's allot clearer now, I get the link between the adapter and the list view, and also array list could represent a paired data set !
1

I am sharing my code it will be help to solve your problem.. for that i created three class first one is Nameing class.

public class Nameing {

int image1;
String name;

public void setImage1(int image1) {
    this.image1 = image1;
}

public int getImage1() {
    return image1;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

second class is Adapter class

public class Adapter extends BaseAdapter {

ArrayList<Object> item;
public Activity context;
public LayoutInflater inflater;
Context c;

public Adapter(ArrayList<Object> item, Activity context) {

    this.item = item;
    this.context = context;
    this.inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return item.size();
}

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

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

class ViewHolder {
    ImageView image1;
    TextView name;
}

public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    holder = new ViewHolder();
    convertView = inflater.inflate(R.layout.activity_main, null);
    holder.image1 = (ImageView) convertView.findViewById(R.id.image1);
    holder.name = (TextView) convertView.findViewById(R.id.name);

    convertView.setTag(holder);

    holder = (ViewHolder) convertView.getTag();
    final Nameing n = (Nameing) item.get(position);
    holder.image1.setImageResource(n.getImage1());
    holder.name.setText(n.getName());

    holder.name.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(context, n.getName(), Toast.LENGTH_SHORT).show();

        }
    });

    return convertView;

}

}

and third class is MainActivity.

public class MainActivity extends Activity {

private ListView list;

private ArrayList<Object> item = new ArrayList<Object>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    list = (ListView) findViewById(R.id.list1);

    Arraylist();

    Adapter ad = new Adapter(item, MainActivity.this);
    list.setAdapter(ad);

}

private void Arraylist() {
    AddOjectToList1(R.drawable.image1, "name");
    AddOjectToList1(R.drawable.image2, "name");
    AddOjectToList1(R.drawable.image3, "name");
    AddOjectToList1(R.drawable.image4, "name");
    AddOjectToList1(R.drawable.image5, "name");
    AddOjectToList1(R.drawable.image6, "name");
    AddOjectToList1(R.drawable.image7, "name");

}

void AddOjectToList1(int image1, String name) {

    Nameing n = new Nameing();
    n.setImage1(image1);
    n.setName(name);
    item.add(n);
}

}

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.