意图putextra不适用于适配器

时间:2015-01-31 20:41:20

标签: android android-intent

我是Android编程新手。 当我想将数据发送到我的listview适配器中的新活动的意图putextra时,我遇到了问题。 这段代码我的适配器

    package com.app.amusa.library;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.app.amusa.FragOrangtua;
import com.app.amusa.R;
import com.app.amusa.ViewDetailAnak;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FroListAnakAdapter extends BaseAdapter {
    Activity activity;
    Context context;
    LayoutInflater inflater;
    List<Anak> AnakList;
    private String id_anak;

    public FroListA

nakAdapter(Activity activity, List<Anak> AnakList) {
        this.activity = activity;
        this.AnakList = AnakList;
    }

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

@Override
public Object getItem(int location) {
    return AnakList.get(location);
}

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

public View getView(final int position, View convertView, ViewGroup parent) {
    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row_anak, null);
    TextView nama = (TextView) convertView.findViewById(R.id.nama);
    TextView username = (TextView) convertView.findViewById(R.id.username);
    TextView anak_ke = (TextView) convertView.findViewById(R.id.anak_ke);

    final Anak a = AnakList.get(position);

    nama.setText(a.getnama());
    username.setText(a.getusername());
    anak_ke.setText("Anak Ke : " + String.valueOf(a.getanak_ke()));
    final int idanak = a.getid_anak();

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(activity, ViewDetailAnak.class);
            intent.putExtra("id_anak",String.valueOf(idanak));
            activity.startActivity(new Intent(intent));

        }
    });
    return convertView;
    }

}

这里是ViewDetailAnak Oncreate

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_item_anak);
    final ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    streamer = new Streamer();
    Intent i = getIntent();
    idanak = i.getStringExtra("id_anak");
    //id
    Toast.makeText(getApplicationContext(), idanak, Toast.LENGTH_LONG).show();
    new Asyncronous().execute();
}

用于nama,username和anak_ke,在TextView中成功填写的值。 也许在idanak成功填补了idanak。 但是在ViewDetailAnak中。 idanak返回0 我试图用getanak_ke()改变idanak的值 但当getanak_ke的值为1时,该值仍为0

如果我不得不把它放在主要活动中?如果有,怎么样? 或者我的代码是否有问题?

1 个答案:

答案 0 :(得分:0)

可能你得到idanak的最后一行值,因为当getView方法调用创建ListView行时,idanak只保留在最后一次调用getView方法时分配的值。

要根据行获取值,请单击使用setTag-getTag方法:

convertView.setTag(idanak);
convertView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            int id_idanak=Integer.parseInteger(arg0.getTag().toString());
            Intent intent = new Intent(activity, ViewDetailAnak.class);
            intent.putExtra("id_anak",String.valueOf(id_idanak));
            activity.startActivity(intent);
        }
    });