在列表视图中选择项目

时间:2014-12-14 09:44:15

标签: android sql listview android-listview adapter

我开发了一个应用程序来从远程sql中检索数据。一切都很好。但是,如果我想在Toast消息中显示所选项目怎么办?换句话说,当用户单击单元格时,toast message会显示单元格内的文本。

这是我的活动:

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

    }
}
}

我的适配器:

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

}

2 个答案:

答案 0 :(得分:1)

试试这个..

   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+" ");
      }
   });

或试试这个..

GetAllCustomerListView.setOnItemClickListener(new OnItemClickListener(){


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

ItemClicked item = adapter.getItem(position);


}


});

在适配器的getItem中编写

public ItemClicked getItem(int position){

return items.get(position);
}

答案 1 :(得分:0)

在你的适配器中放这个

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

在你的活动中

 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


        }
    });