【问题标题】:Get text from GridView从 GridView 获取文本
【发布时间】:2015-09-14 11:58:11
【问题描述】:

我在从 gridview 获取文本时遇到一些问题,并最终以新的意图使用 putextra 将它们传递给另一个活动,但每次我尝试获取文本应用程序时都会停止。这是代码

    public class MainActivity extends ActionBarActivity {
    String cJSON;
    private ProgressDialog pDialog;
    GridView gvColl;

    private static final String TAG_RESULTS = "result";
    private static final String TAG_ID = "ID";
    private static final String TAG_NAME = "Nome";
    JSONArray worker_lst = null;
    ArrayList<HashMap<String, String>> tableColl = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gvColl = (GridView) this.findViewById(R.id.gvCollab);
        tableColl = new ArrayList<HashMap<String,String>>();
        getData();
    }

    protected void showList() {
        try {
            JSONObject jsonObj = new JSONObject(cJSON);
            worker_lst = jsonObj.getJSONArray(TAG_RESULTS);

            for (int i = 0; i < worker_lst.length(); i++) {
                JSONObject c = worker_lst.getJSONObject(i);
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                HashMap<String, String> persons = new HashMap<String, String>();
                persons.put(TAG_NAME, name);
                persons.put(TAG_ID, id);
                tableColl.add(persons);
            }

            gvColl = (GridView) findViewById(R.id.gvCollab);
            gvColl.setAdapter(new JAdapter(MainActivity.this, tableColl));

            gvColl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// this work properly. I don’t be able to get id and name from grid

                      Toast.makeText(getApplicationContext(), "Collaboratore Scelto : " + position, Toast.LENGTH_LONG).show();



                }
             });


            }catch (JSONException e) {
            e.printStackTrace();
        }

    }

【问题讨论】:

  • 能否请您出示您的日志猫
  • 您的应用程序在加载网格数据之前停止?请分享您遇到的例外情况

标签: android gridview textview


【解决方案1】:

通过替换它在您的活动中使用此代码...

gvColl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {
                    // this work properly. I don’t be able to get id and name
                    // from grid

                    Toast.makeText(getApplicationContext(),
                            "Collaboratore Scelto : " + position,
                            Toast.LENGTH_LONG).show();
                String currentName; tableColl.get(position).get(TAG_NAME);
                    String currentId=tableColl.get(position).get(TAG_ID);

                }
            });

现在您可以进一步使用两个字符串...

【讨论】:

  • 好!它工作正常!所以,(我认为),我可以在我的意图中使用 putextra 将 currentID 和 currentName 传递给另一个活动。我希望它是正确的。但是代码是对的!谢谢
  • 欢迎您...只需投票,您会发现答案工作正常...在左侧
  • 啊,显然对于像我这样的新手来说,每一个新程序都会有问题!但是,找了很多帖子和大家的建议,我真的学到了很多!谢谢大家。
【解决方案2】:

我认为你应该通过替换你的方法来尝试这个 -

gvColl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// this work properly. I don’t be able to get id and name from grid

                      Toast.makeText(getApplicationContext(), "Collaboratore Scelto : " + position, Toast.LENGTH_LONG).show();

HashMap<String, String> person=tableColl.get(position);
        String name=person.get(TAG_NAME);
        String id=person.get(TAG_ID);
Toast.makeText(getApplicationContext(), "Name : " + name+" ,Id :"+id, Toast.LENGTH_LONG).show();
                }
             });

【讨论】:

    【解决方案3】:

    请检查此代码

    gvColl.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                public void onItemClick(AdapterView<?> parent, View v, int position, long id)
                {
    
                    String dataFromGrid;
                    dataFromGrid = ((TextView)(p_view.findViewById(R.id.ulr_textview))).getText().toString();
    
                    Intent intent = new Intent(this,Activity2.class);
                    intent.putExtra("PassData",dataFromGrid);
                    startActivity(intent);
                }
            });
    

    在您的新活动中使用以下代码

    String m_data;
    
        if(getIntent().getExtras() != null)
        {
            m_data = getIntent().getExtras().getString("PassData");
        }
    

    【讨论】:

    • 谢谢,上面发布的快速学习者的代码可以正常工作。我刚刚使用了你的代码,但我对(p_view 等)有一些问题......我不明白什么?
    • 将 p_view 替换为 v 。如果以上代码对您有用,请接受并投票
    • 是的,我已经用 v 替换了 p_view,(作为 onitemclick 的参数,但我不明白什么是指 ulr_textview...这是我的 gvcoll?
    • 它是 gridview 的行布局文本视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多