从Android listview获取价值并将其添加到额外的内容中

时间:2015-07-01 23:54:53

标签: android-listview

在我的下面的代码中,我创建了一个方法:我想在putExtra中放一个变量,我可以在新的活动中得到它。问题在于新活动从我列表中的第一个元素给了我第一个值(numtelephone)。我知道因为getItem(0),但是如何从列表中获取每个元素的值(numtelephone)?

public class ListClient extends Activity {
    String myJSON,test; 
  TextView numt;
    private static final String TAG_RESULTS="result";
    private static final String TAG_NOMCLIENT = "nomclient";
    private static String TAG_NUMTELEPHONE ="numtelephone";
    private static final String TAG_DEPART ="depart";
    private static final String TAG_DESTINATION ="destination";
    private static final String TAG_NBRPERSONNE ="nbrpersonne";  
    JSONArray peoples = null;
    ArrayList<HashMap<String, String>> personList;

ListView list;
@SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_client);
        list = (ListView) findViewById(R.id.list);
        personList = new ArrayList<HashMap<String,String>>();
        getData();
    }

    protected void showList(){
            //json parsing etc...

            ListAdapter adapter = new SimpleAdapter(
                    ListClient.this, personList, R.layout.list_item,
                    new String[]{TAG_NOMCLIENT,TAG_NUMTELEPHONE,TAG_DEPART,TAG_DEPART,TAG_NBRPERSONNE},
                    new int[]{R.id.name, R.id.numt, R.id.depart, R.id.destination, R.id.nbrpersonne}
            );
            list.setAdapter(adapter);
    }

    public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String>{

            @Override
            protected String doInBackground(String... params) {
               // json dowload from web, not needed in this question
               return ""; 
            }

            @Override
            protected void onPostExecute(String result){
                myJSON=result;
                showList();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();

    }

    public void GoUpdate(View v ) {
        Intent in = new Intent(getBaseContext(), Update.class);
            in.putExtra("NUMTELEPHONE",((HashMap<String, String>)list.getAdapter().getItem(0)).get("numtelephone"));
        }

        startActivity(in);
    }
}

1 个答案:

答案 0 :(得分:0)

简单而且只有一种解决方案。删除你的xml android:点击列表属性并在你的onCreate中添加list#setOnItemClickListener

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_client);
    list = (ListView) findViewById(R.id.list);
    personList = new ArrayList<HashMap<String,String>>();
    getData();

    //this thing has been added
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(id!=-1){
            Intent in = new Intent(getBaseContext(), Update.class);
            in.putExtra("NUMTELEPHONE", ((HashMap<String, String>) list.getAdapter().getItem((int)id)).get("numtelephone")); //TAG_NUMTELEPHONE

            startActivity(in);
            }
        }
    });
}

将其从onCreate执行:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_client);
        list = (ListView) findViewById(R.id.list);
        personList = new ArrayList<HashMap<String, String>>();
        getData();
        list.setOnItemClickListener(new OnMyListItemClickListener());
    }

    Context context = this;

    private class OnMyListItemClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(id!=-1){
            Intent in = new Intent(context, Update.class);
            in.putExtra("NUMTELEPHONE", ((HashMap<String, String>) list.getAdapter().getItem((int) id)).get("numtelephone")); //TAG_NUMTELEPHONE

            startActivity(in);
            }
        }
    }

经验教训 - 不要创建sucha按钮 - 他们不知道他们是哪个项目,所以你不能提取id - 而只是使用类似AlertDialog的东西

  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(id!=-1){
        AlertDialog.Builder al = new AlertDialog.Builder(context);
        al.setMessage("Are you sure you want to do it?");
        al.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent in = new Intent(context, Update.class);
                in.putExtra("NUMTELEPHONE", ((HashMap<String, String>) list.getAdapter().getItem((int) id)).get(TAG_NUMTELEPHONE));

                startActivity(in);
            }
        });
        al.setNegativeButton(android.R.string.no, null);
        al.show(); 
        }        
    }