从GridView获取文本

时间:2015-09-14 11:58:12

标签: android gridview textview

我有一些问题从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();
        }

    }

3 个答案:

答案 0 :(得分:0)

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

FlowLayoutPanel

现在你可以更进一步......

答案 1 :(得分:0)

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

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();
                }
             });

答案 2 :(得分:0)

请检查此代码

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");
    }