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