0

I have developed an app to retrieve data from remote sql. Everything works great. But how if I want to display the selected item in Toast message? 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;
}

}
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
Hani
  • 45
  • 1
  • 7
  • welcome and your issue answered multiple times , just search for it : http://stackoverflow.com/search?q=[android]+listview+selected+item – Arash GM Dec 14 '14 at 09:48
  • i found [this][1] answer about getting selected item from list view. [1]: http://stackoverflow.com/a/13627960/1928342 – Liran Peretz Dec 14 '14 at 12:05
  • take a look on [this][1] answer.... [1]: http://stackoverflow.com/a/13627960/1928342 – Liran Peretz Dec 14 '14 at 12:07

2 Answers2

1

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

public ItemClicked getItem(int position){

return items.get(position);
}
Amitabha Biswas
  • 3,281
  • 1
  • 11
  • 23
0

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


        }
    });
Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94