简体   繁体   English

获取在列表视图中选择的项目

[英]Get item selected in listview

I have developed an app to retrieve data from remote sql. 我已经开发了一个应用程序,可以从远程sql检索数据。 Everything works great. 一切正常。 But how if I want to display the selected item in Toast message? 但是,如果我想在Toast消息中显示所选项目,该怎么办? In other words, when the user click on the cell, toast message display the text inside the cell. 换句话说,当用户单击单元格时,祝酒消息将在单元格内显示文本。

this is my activity: 这是我的活动:

package com.hani.exdatabase;


import android.content.Intent;
import android.os.AsyncTask;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ListView;


import org.json.JSONArray;


public class MainActivity extends ActionBarActivity {
private ListView GetAllCustomerListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.GetAllCustomerListView = (ListView) this.findViewById(R.id.GetAllCustomers);
    new GetAllCustomerTask().execute(new ApiConnector());
    this.GetAllCustomerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



        }
    });
}

public void setListAdapter (JSONArray jsonArray){
this.GetAllCustomerListView.setAdapter(new GetAllCustomerListViewAdapter(jsonArray,this));
}
private class GetAllCustomerTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
    @Override

    protected JSONArray doInBackground(ApiConnector... params) {

        return params[0].GetAllCustomers();

    }

    @Override
    protected void onPostExecute(JSONArray jsonArray) {
        setListAdapter(jsonArray);

    }
}
}

My adapter: 我的适配器:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GetAllCustomerListViewAdapter extends BaseAdapter {

private JSONArray dataArray;
private Activity activity;

private LayoutInflater inflater = null;

public GetAllCustomerListViewAdapter(JSONArray jsonArray, Activity a)
{
    this.dataArray = jsonArray;
    this.activity = a;

    inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return this.dataArray.length();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    ListCell cell;
    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.get_all_customer_list_view_cell, null);
        cell = new ListCell();

        cell.FullName = (TextView) convertView.findViewById(R.id.customer_name);
        cell.Age = (TextView) convertView.findViewById(R.id.customer_age);

        convertView.setTag(cell);
    }
    else {
        cell = (ListCell) convertView.getTag();
    }

    try{


    JSONObject jsonObject = this.dataArray.getJSONObject(position);
    cell.FullName.setText(jsonObject.getString("news"));
    cell.Age.setText(jsonObject.getString("date"));
    }
    catch (JSONException e){
        e.printStackTrace();
    }
    return convertView;
}

private class ListCell {
    private TextView FullName;
    private TextView Age;
}

}

Try with this.. 试试这个..

   GetAllCustomerListView.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
            String value = (String)adapter.getItemAtPosition(position); 
            Log.d("position",value+" ");
      }
   });

or try this.. 或尝试这个。

GetAllCustomerListView.setOnItemClickListener(new OnItemClickListener(){


@Override
public void onItemClick(AdapterView<?>adapter,View v, int position){

ItemClicked item = adapter.getItem(position);


}


});

in your adapter's getItem you write 在您适配器的getItem中编写

public ItemClicked getItem(int position){

return items.get(position);
}

In your adapter put this 在您的适配器中放这个

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return jsonArray.get(position); // just return the object at the position index
}

And in your activity 在你的活动中

 this.GetAllCustomerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


  Object currentItem=adapter.getItem(position); (replace adapter with your adapter name)
(JsonArray) array=(JsonArray)currentItem; 
// do what you want with your json array


        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM