【问题标题】:Want to Implements View.OnItemClickListener想要实现 View.OnItemClickListener
【发布时间】:2016-06-07 01:08:04
【问题描述】:

我想在我的编码中实现这个方法,但我不知道怎么做,有人可以给我一些建议吗?

下面是我要使用的编码截图。

View.ONItemClickListener

这是我的编码。 ViewOrder.java

public class ViewOrder extends AppCompatActivity implements ListView.OnItemClickListener {
public static final String JSON_URL = "http://dashberry.com/strack/mobile/viewOrder.php";
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_order);
    listView = (ListView) findViewById(R.id.listView);
    sendRequest();
}

private void sendRequest(){

    StringRequest stringRequest = new StringRequest(JSON_URL, new Response.Listener<String>() {
        public void onResponse(String response) {
            showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(ViewOrder.this,error.getMessage(),Toast.LENGTH_LONG).show();

                }
        });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void showJSON(String json){

    ParseJSON pj = new ParseJSON(json);
    pj.parseJSON();
    CustomList cl = new CustomList(this, ParseJSON.itemName,ParseJSON.origin,ParseJSON.destination);
    listView.setAdapter(cl);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(this, ItemDetail.class);
    //ParseJSON<String> map = (ParseJSON)parent.getItemAtPosition(position);
    //HashMap<String,String> map = (HashMap)parent.getItemAtPosition(position);
   // String empId = map.get(ParseJSON.itemName).toString();
   // intent.putExtra("item_id", empId);
    startActivity(intent);
 }

}

ParseJSON.java

public class ParseJSON {
public static String[] itemName;
public static String[] origin;
public static String[] destination;

public static final String JSON_ARRAY = "result";
public static final String KEY_NAME = "itemName";
public static final String KEY_ORIGIN = "origin";
public static final String KEY_DESTINATION = "destination";

private JSONArray users = null;

private String json;



public ParseJSON(String json){

    this.json = json;
}

protected void parseJSON(){
    JSONObject jsonObject=null;
    try {
        jsonObject = new JSONObject(json);
        users = jsonObject.getJSONArray(JSON_ARRAY);

        itemName = new String[users.length()];
        origin = new String[users.length()];
        destination = new String[users.length()];

        for(int i=0;i<users.length();i++){
            JSONObject jo = users.getJSONObject(i);
            itemName[i] = jo.getString(KEY_NAME);
            origin[i] = jo.getString(KEY_ORIGIN);
            destination[i] = jo.getString(KEY_DESTINATION);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

}

所以基本上我想要的是当用户单击列表中的项目时,它会将用户带到带有一些数据(项目名称)的下一个活动。

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

    实现 onItemClick 点赞

            @Override
        public void onItemClick(AdapterView<?> l, View v, int position, long id) {
                    //start a new Activity via Intent
                    Intent intent = new Intent();
                    intent.setClass(this, ItemDetail.class);
    
    //you can putExtra only position or id
                    intent.putExtra("position", position);
                    intent.putExtra("id", id);
                    startActivity(intent);
            }
    

    您应该创建名为 ItemDetail 的 Anther Activity,您可以通过在 onCreate() 中添加此代码来获取 ID 和位置,以了解单击了哪个项目

    Intent intent = getIntent();
    String position = intent.getStringExtra("position");
    String id = intent.getStringExtra("id");
    

    【讨论】:

    • 感谢您对我的问题发表评论,这真的帮助我思考。这是否意味着我不可能携带列表中的数据之一?例如,我只想携带我们单击的列表的项目名称。 @NgimaSherpa
    • 是的,这是可能的。您可以使用 putExtra() 方法从一个活动发送数据,并可以在其他活动中获取从前一个活动发送的数据。
    猜你喜欢
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 2014-01-29
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多