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

Posted

技术标签:

【中文标题】当我单击列表视图的操作项时,将文本从列表视图传递到另一个活动【英文标题】:Passing Text From Listview to another Activity when I click action item of the listview 【发布时间】:2013-03-10 15:04:59 【问题描述】:

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

这是我的主要活动

公共类 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);

    

联系人适配器:

公共类 ContactsAdapter 扩展 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);
        
    ;


【问题讨论】:

现在你有什么问题 当我单击 quickAction 中存在的操作项之一时,我无法将字符串从列表视图传递到另一个(EditText)活动。 您无法在日志中获取值 明确一点。我已经自定义了列表视图。当我单击列表视图中的图像时,列表视图中的值应该填充到另一个活动中。是否可以通过单击列表视图中的图像来传递该值?跨度> 是的,您必须为列表视图行中的 imageView 设置 onclicklistener 【参考方案1】:
    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);
    
;

只需将字符串传递给您的活动并将函数的参数更改为字符串即可运行

【讨论】:

以上是关于当我单击列表视图的操作项时,将文本从列表视图传递到另一个活动的主要内容,如果未能解决你的问题,请参考以下文章

Flex 在列表项的视图之间传递数据

单击列表视图项时检索的值

单击时如何使列表视图导航到另一个页面

单击时将所有项目从列表视图转移到文本视图

点击 Android/Java 列表视图项时,如何禁用单击声音?

单击列表视图项时从 SQLite 数据库中获取行 ID