簡體   English   中英

如何從列表視圖傳遞記錄的ID

[英]How to pass the id of a record from a listview

public class searchEvent extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener,
    SearchView.OnQueryTextListener {
private SessionManager session;
ProgressDialog pDialog;
List<DataModel> listData = new ArrayList<DataModel>();
Adapter adapter;
SwipeRefreshLayout swipe;
ListView list_view;


public static final String url_data = "http://10.0.2.2/TriniRec/eventList.php?email=";
public static final String url_cari = "http://10.0.2.2/TriniRec/search.php?email=";

private static final String TAG = searchEvent.class.getSimpleName();

public static final String TAG_ID = "eventID";
public static final String TAG_NAMA = "name";
public static final String TAG_RESULTS = "results";
public static final String TAG_MESSAGE = "message";
public static final String TAG_VALUE = "value";

String tag_json_obj = "json_obj_req";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_event);
    // session manager
    session = new SessionManager(getApplicationContext());

    swipe = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
    list_view = (ListView) findViewById(R.id.list_view);

    adapter = new Adapter(searchEvent.this, listData);
    list_view.setAdapter(adapter);

    swipe.setOnRefreshListener(this);

    swipe.post(new Runnable() {
                   @Override
                   public void run() {
                       swipe.setRefreshing(true);
                       callData();
                   }
               }
    );
    //Adding ListView Item click Listener.
   list_view.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(searchEvent.this,ShowSingleRecordActivity.class);

            // Sending ListView clicked value using intent.
            intent.putExtra("ListViewValue",listData.get(position).toString());

            startActivity(intent);

            //Finishing current activity after open next activity.
            finish();

        }
    });
}


private void callData() {
    listData.clear();
    adapter.notifyDataSetChanged();
    swipe.setRefreshing(true);
    String email = session.getEmail();
    // Creating volley request obj
    JsonArrayRequest jArr = new JsonArrayRequest(url_data + email, new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {
            Log.e(TAG, response.toString());

            // Parsing json
            for (int i = 0; i < response.length(); i++) {
                try {

                    JSONObject obj = response.getJSONObject(i);

                    DataModel item = new DataModel();

                    item.setEventID(obj.getString(TAG_ID));
                    item.setName(obj.getString(TAG_NAMA));


                    listData.add(item);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            // notifying list adapter about data changes
            // so that it renders the list view with updated data
            adapter.notifyDataSetChanged();
            swipe.setRefreshing(false);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.e(TAG, "Error: " + error.getMessage());
            Toast.makeText(searchEvent.this, error.getMessage(), Toast.LENGTH_LONG).show();
            swipe.setRefreshing(false);
        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jArr);
}

@Override
public void onRefresh() {
    callData();
}

@Override
public boolean onQueryTextSubmit(String query) {
    cariData(query);
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    return false;
}

在這里,我正在顯示mysql數據庫中的事件列表,我希望用戶能夠單擊列表記錄,並將它們帶到記錄的所有詳細信息進行下一個活動,為此我需要傳遞記錄id 。 我試圖通過意圖將其傳遞給數組位置,但是我沒有得到預期的結果,我認為我傳遞了錯誤的值,有人可以告訴我我必須在這里傳遞什么。 我是java和android studio的新手,所以很抱歉缺少細節。

您應該在這里做什么:

DataModel model = (DataModel) listData.get(position);
intent.putExtra("ListViewValue", model);
startActivity(intent);

您的DataModel應該implements Serializable

這應該可以解決您的問題。

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             // get postion from arraylist
                modelclass u = arrayList.get(position);
                Intent in = new Intent(currentacticity.this, nextactivity.class);
                in.putExtra("send", u);
                startActivity(in);
                   }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM