开发者

Android passing an array from an activity to a customadapter class

In MainAcitivty.java i populate an Arraylist with items. I need these items in my customadapter class because i need them to set the colour of certain rows in a textview.

I have a dynamic array: storage [], whose size is set during the process:

 storage = new int[db];

This array has the following items e.g.: 35,56,67

The arraylist "myArr3" has the same items but type of the elements are string.

This is the definition of the myArr3 arraylist:

  ArrayList<String> myArr3 = new ArrayList<String>();

I am looking for a solution to manages these items in the other class whether it is an integer type array or a string type arraylist.

In MyCustomBaseAdapter2.java i set the layout of the Adapter:

package com.bfarago.nevnap;

import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MyCustomBaseAdapter2 extends BaseAdapter {
     private static ArrayList<SearchResults> searchArrayList;

 private LayoutInflater mInflater;

 public MyCustomBaseAdapter2(Context context, ArrayList<SearchResults> results) {
  searchArrayList = results;
  mInflater = LayoutInflater.from(context);
 }

public MyCustomBaseAdapter2(OnClickListener onClickListener,
        ArrayList<SearchResults> searchResults) {
    // TODO Auto-generated constructor stub
}

public int getCount() {
  return searchArrayList.size();
 }

 public Object getItem(int position) {
  return searchArrayList.get(position);
 }

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

 public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder;
  if (convertView == null) {
   convertView = mInflater.inflate(R.layout.row2, null);开发者_运维百科
   holder = new ViewHolder();


       holder.txtName = (TextView) convertView.findViewById(R.id.left);
       holder.txtCityState = (TextView) convertView.findViewById(R.id.right);


   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }

  holder.txtName.setText(searchArrayList.get(position).getName());
  holder.txtCityState.setText(searchArrayList.get(position).getCityState());



  return convertView;
 }

static class ViewHolder {
  TextView txtName;
  TextView txtCityState;

 }
}


You can change the integer array to a string ArrayList and then use the ArrayList in your Adapter

You can change your integer array to string array to like this

 int storage [] = new int[db];
 int stringstorage[] = new String[db];
 for (int i = 0; i < db; i++) {
 stringstorage[i] =storage[i] ;
}

Now Change the stringstorage to your ArrayList

  List<String> l = Arrays.asList(stringstorage);
  ArrayList<String> list = new ArrayList<String>(l);

Now use the list as your need.

Hope this will help you.


The solution is to use intents to pass an array from one activity to another activity.

The answer can be found in this

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜