Android 将数组从活动传递到自定义适配器类

发布于 2024-10-19 12:01:53 字数 2287 浏览 1 评论 0原文

在 MainAcitivty.java 中,我用项目填充 Arraylist。我的自定义适配器类中需要这些项目,因为我需要它们来设置文本视图中某些行的颜色。

我有一个动态数组:storage [],其大小在过程中设置:

 storage = new int[db];

该数组具有以下项目,例如:35,56,67

数组列表“myArr3”具有相同的项目,但元素类型是字符串。

这是 myArr3 arraylist 的定义:

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

我正在寻找一种解决方案来管理另一个类中的这些项目,无论它是整数类型数组还是字符串类型数组列表。

在 MyCustomBaseAdapter2.java 中,我设置了适配器的布局:

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;

 }
}

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;

 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面犯桃花 2024-10-26 12:01:53

您可以将整数数组更改为字符串ArrayList,然后在Adapter中使用ArrayList

您可以将整数数组更改为字符串数组,就像现在

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

stringstorage 更改为您的 ArrayList

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

现在根据您的需要使用 list

希望这会对您有所帮助。

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.

仲春光 2024-10-26 12:01:53

解决方案是使用意图将数组从一个活动传递到另一个活动。

答案可以在这个中找到

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

The answer can be found in this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文