【问题标题】:how to send string array from custom adpter to main activity如何将字符串数组从自定义适配器发送到主要活动
【发布时间】:2014-05-12 13:49:17
【问题描述】:

我是一个新的 android 开发人员,我想将字符串数组从自定义 ADapter 发送到主要活动类,但我的代码不起作用,我正在使用 CustomAdapter 类请任何帮助我

我的 ContactList 课程正在关注.....

public class ContactList extends Activity {
    ListView lvContacts;
    List<String> listname;
    List<String> listnumber;
    SimpleCursorAdapter adapter;
    ArrayList<PhoneList> arr;
    ArrayList<PhoneList> arrnumber;
    Button smsbtn;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        smsbtn = (Button) findViewById(R.id.sms);
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER, };
        Cursor c1 = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
                null, null, null);
            listname = new ArrayList<String>();
        listnumber = new ArrayList<String>();
        while (c1.moveToNext()) {
            String name = c1
                    .getString(c1
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number = c1
                    .getString(c1
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            listname.add(name);
            listnumber.add(number);
        }
        c1.close();
        Object[] obj = listname.toArray();
        Object[] objnumber = listnumber.toArray();
        arr = new ArrayList<PhoneList>();
        String[] da = new String[obj.length];
        String[] danumber = new String[objnumber.length];
        for (int i = 0; i < obj.length; i++) {
            danumber[i] = (String) objnumber[i];
            da[i] = (String) obj[i];
            arr.add(new PhoneList(da[i], danumber[i], i));
        }
        ListView listView = (ListView) findViewById(R.id.lvContacts);
        CustomAdapter adpttt = new CustomAdapter(ContactList.this,
                R.layout.contacts_list_item, arr);
        listView.setAdapter(adpttt);
        smsbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = getIntent();
                String[] myStrings = intent.getStringArrayExtra("numberarray");
                for (int i = 0; i < myStrings.length; i++) {
                    Toast.makeText(getApplicationContext(), myStrings[i],
                            Toast.LENGTH_LONG).show();
                }

            }
        });
    }

}

我的自定义适配器类正在关注....

public class CustomAdapter extends ArrayAdapter<PhoneList> {
    int inflatr;
    Context ctxt;
    boolean bulkflag = false;
    static int k=0;
    ArrayList<PhoneList> data=new ArrayList<PhoneList>();
    private static final String[] arr1=new String[256];
    private static String[] arr=new String[256];
        public CustomAdapter(Context context, int resource, ArrayList<PhoneList> arr) {

        super(context, resource, arr);
        this.inflatr = resource;
        this.ctxt = context;
        this.data= arr;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         UserHolder holder = null;
         View row = convertView;
        if(convertView==null)
        {
             LayoutInflater inflater = ((Activity) ctxt).getLayoutInflater();
             row = inflater.inflate(inflatr, parent, false);
             holder = new UserHolder();
             holder.textName=(TextView)row.findViewById(R.id.lblName);
             holder.stnumber=(TextView)row.findViewById(R.id.mobilenum);
             holder.checked=(CheckBox)row.findViewById(R.id.check);

//           holder.btnEdit = (ImageButton) row.findViewById(R.id.atomPay_removePay);
             row.setTag(holder);
        }
        else
        {
             holder = (UserHolder) row.getTag();            
        }
         final PhoneList dta=data.get(position);
         holder.checked.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                 int position = (Integer) v.getTag();
                 data.get(position).setselect(!data.get(position).getSelect());
                 notifyDataSetChanged();
                 arr=dta.getnumberr();
                 arr1[k]=arr[position];
            }
        });

         holder.checked.setChecked(dta.getSelect());

         holder.checked.setTag(position);
        for(int i=0; i<=data.size(); i++)
        {
            holder.textName.setText(dta.getName());
        }
        for(int j=0; j<=data.size(); j++)
        {
            holder.stnumber.setText(dta.getNumber());
        }

//      holder.btnEdit.setOnClickListener(new OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {
//              Toast.makeText(ctxt, "Humayoon    Siddiqueeeeeeeeeeeeeeeeeee"+dta.getName(), Toast.LENGTH_SHORT).show();
//              Intent moreIntent=new Intent(getContext(),ContactList.class);
//              String tName=dta.getName();
//              moreIntent.putExtra("Template",tName);
//              v.getContext().startActivity(moreIntent);
//               // ctxt.startActivity(ctxt,ContactList.class);
//          }
//      });
        Intent ii=new Intent(getContext(),ContactList.class);
        ii.putExtra("numberarray", arr1);
        return row;
    }
    @Override
    public int getCount() {
//      // TODO Auto-generated method stub
        return data.size();
    }

    static class UserHolder {
        TextView textName;
        TextView textAddress;
        TextView textLocation;
        ImageButton btnEdit;
        Button btnDelete;
        TextView stnumber;
        CheckBox checked;
        }


}

我的 xml 布局如下......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@layout/bg_player_header"
    >
    <ListView
        android:id="@+id/lvContacts"
        android:layout_width="match_parent"
        android:layout_height="422dp"
        android:divider="#FFEEEE" >
</ListView>

    <Button
        android:id="@+id/sms"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dip"
        android:layout_marginLeft="109dip"
        android:text="Send Sms"
        android:onClick="btnclicked"
         />

</LinearLayout>

【问题讨论】:

  • 为什么要将 arr1 声明为 final ,删除它。
  • 私有静态 String[] arr1=new String[256];
  • 因为我在数组中存储特定数字,并且多次调用 phonlist 类的 getnumrr 函数调用数组丢失值
  • 为什么在吐司中连续显示这么多字符串?这很糟糕
  • 兄弟仅用于测试而不是应用程序部分

标签: android android-contacts sendmessage custom-adapter


【解决方案1】:

在您的自定义适配器中创建函数以获取您需要的数据。您需要在主活动类中引用适配器。

【讨论】:

  • 我不明白请看我的完整代码我想从自定义适配器发送一个字符串数组太主要活动
  • 你不能发送它,你需要在你的适配器中有像getData()这样的函数,你需要从你的活动中调用那个函数。
  • 这就是他的意思:public String[] getData() { return arr1 }。这将在您的适配器类中。然后你从你的活动课上打电话给getData()。这样,当您调用 adapter.getData() 时,该数组将返回到您的活动中
  • 因为当我在主要活动中创建 customedapter 的引用时,由于自定义适配器构造函数被参数化,并且由于这个空指针异常和代码不起作用而强制我声明 null
  • OP之所以困惑是因为你的解释不存在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
  • 2015-10-19
  • 1970-01-01
相关资源
最近更新 更多