【问题标题】:Get item selected in listview获取列表视图中选择的项目
【发布时间】:2015-02-12 14:33:56
【问题描述】:

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

这是我的活动:

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

}

【问题讨论】:

标签: android sql listview android-listview adapter


【解决方案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);
}

【讨论】:

    【解决方案2】:

    在你的适配器里放这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-03
      相关资源
      最近更新 更多