单击列表视图的操作项时,将文本从Listview传递到另一个活动

时间:2013-03-21 07:37:32

标签: android-listview

我在我的主要活动中有一个列表视图。在每一行我有一个图像视图,当我点击该图像视图时会出现QuickAction(如ios中的popover)。我的请求是,我想将listText中的文本设置为当我单击快速操作中的操作项时,另一个Activity的edittext。请帮助..

这是我的主要活动

公共类ExampleActivity扩展了Activity {

private static final int ID_UP = 1;
private static final int ID_DOWN = 2;
private static final int ID_SEARCH = 3;
private static final int ID_INFO = 4;

private QuickAction quickAction;
private ActionItem nextItem;
private ActionItem prevItem;
private ActionItem searchItem;

private ListView view;
private ContactsAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    view = (ListView) findViewById(R.id.listView1);
    final Weather weather_data[] = new Weather[] { new Weather(R.drawable.icon, "Cloudy"),
            new Weather(R.drawable.icon, "Showers"),
            new Weather(R.drawable.icon, "Snow"),
            new Weather(R.drawable.icon, "Storm"),
            new Weather(R.drawable.icon, "Sunny")

    };
    adapter = new ContactsAdapter(this, R.layout.main1,
            weather_data);          
}


void functiontorun(View view1) {
     quickAction = new QuickAction(this,QuickAction.HORIZONTAL);
    nextItem = new ActionItem(ID_DOWN, "Next", getResources().getDrawable(
            R.drawable.menu_down_arrow));
    prevItem = new ActionItem(ID_UP, "Prev", getResources().getDrawable(
            R.drawable.menu_up_arrow));
    searchItem = new ActionItem(ID_SEARCH, "Find", getResources()
            .getDrawable(R.drawable.menu_search));

    // use setSticky(true) to disable QuickAction dialog being dismissed
    // after an item is clicked
    prevItem.setSticky(true);
    nextItem.setSticky(true);

    // add action items into QuickAction
    quickAction.addActionItem(nextItem);
    quickAction.addActionItem(prevItem);
    quickAction.addActionItem(searchItem);
    // Set listener for action item clicked
     final int position1 = view.getPositionForView(view1);

    quickAction
            .setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
                @Override
                public void onItemClick(QuickAction source, int pos,
                        int actionId) {
                    ActionItem actionItem = quickAction.getActionItem(pos);

                    if (actionId == ID_SEARCH) {
            Intent i=new Intent(ExampleActivity.this,Second.class);
                        i.putExtra("position",position1 );
                        Log.v("position","po" +position1);
                        startActivity(i);

                    } else if (actionId == ID_INFO) {
                        Toast.makeText(getApplicationContext(),
                                "I have no info this time",
                                Toast.LENGTH_SHORT).show();

                    } else {
                        Toast.makeText(getApplicationContext(),
                                actionItem.getTitle() + " selected",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });

     quickAction.show(view1);

}

}

我的另一项活动

public class Second扩展Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
    EditText d1=(EditText) findViewById(R.id.editText1);
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        final int position = (Integer) bundle.get("position");
        Log.v("position","po1  " +position);
        ArrayList<Weather> array = new ArrayList<Weather>();
        Log.v("position","po1  " +array);

    }
}

}

联系人适配器:

public class ContactsAdapter extends ArrayAdapter {

    Context context; 
    int layoutResourceId;    
    Weather data[] = null;
    public ContactsAdapter(Context context, int layoutResourceId, Weather[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imageView1);
            holder.imgIcon.findViewById(R.id.imageView1).setOnClickListener(mBuyButtonClickListener);
            holder.txtTitle = (TextView)row.findViewById(R.id.textView1);
            //((ImageView) row.findViewById(R.id.im)).setOnClickListener(mBuyButtonClickListener);
            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }

        Weather weather = data[position];
        holder.txtTitle.setText(weather.title);
        holder.imgIcon.setImageResource(weather.icon);
        return row;
    }

    static class WeatherHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }
    private OnClickListener mBuyButtonClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            ((ExampleActivity) context).functiontorun(v);
        }
    };

}

1 个答案:

答案 0 :(得分:0)

    private OnClickListener mBuyButtonClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
          WeatherHolder  holder = (WeatherHolder) v.getTag;
          String string =  holder.txtTitle .getText();

        ((ExampleActivity) context).functiontorun(string);
    }
};

只需将字符串传递给您的活动,并将functiontorun的参数更改为字符串