Android:想要在我的mainactivity中访问视图中的textview

时间:2013-08-22 13:08:19

标签: java android android-listview android-adapter

我很抱歉我的英语不好,但这是我的问题:

我使用像ArrayAdapter这样的自定义ListView,并在我的主Activity中创建我的ArrayAdapter作为实习类:

    /*This is my Main Activity*/
    public class  KhassaideActivity extends Activity {

    ViewHolder holder;
    KhassaideAdapterbi listadapter ;
    ListView l ; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {

      l= (ListView) findViewById(R.id.listDieuzbou);
      listadapter = new KhassaideAdapterbi(this, R.layout.affichage_khassaide, rowItems);
      l.setAdapter(listadapter);
    }

    /*This is my intern class ArrayAdapter*/
    private class KhassaideAdapterbi  extends ArrayAdapter<RowItem>{

    Context context;
    public KhassaideAdapterbi(Context context, int resourceId,
            List<RowItem> items) {

        super(context, resourceId, items);
        this.context = context;
    }

    public class ViewHolder {
    ImageView imageView;
    TextView trad;
    TextView trans;
    TextView number;
}

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


        RowItem rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

 holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.affichage_khassaide, null);

            holder.number = (TextView) convertView.findViewById(R.id.number);
            holder.trad = (TextView) convertView.findViewById(R.id.traduction);
            holder.trans = (TextView) convertView.findViewById(R.id.transcription);
            holder.imageView = (ImageView) convertView.findViewById(R.id.arabe);
            convertView.setTag(holder);
        }
        else
            holder = (ViewHolder) convertView.getTag();

        holder.trad.setText(rowItem.getTitle());
        holder.trans.setText(rowItem.getDesc());
        holder.imageView.setImageResource(rowItem.getImageId());
        holder.number.setText(rowItem.getNumber());
        return convertView;
    }



}

所以在我的主Activity中,当用户点击这样的菜单项时,我想访问并共享两个TextView的值:

    case R.id.action_share:

        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, listadapter.holder.trad.getText());
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));

但是我在这一行得到了一个N​​ullPointerException listadapter.holder.trad.getText()

有人有解决方案吗?先感谢您。对于这个糟糕的英语再次感到遗憾

1 个答案:

答案 0 :(得分:2)

你应该改变这个....

private class KhassaideAdapterbi  extends ArrayAdapter<RowItem>{

Context context;
List<RowItem> Rowitems;
public KhassaideAdapterbi(Context context, int resourceId,
        List<RowItem> items) {

    super(context, resourceId, items);
    this.context = context;
    this.Rowitems = items;
}

现在从Rowitems获取数据......