0

我在尝试从我的 Listview 获取数据时遇到问题,然后在用户从列表中选择产品后在 EditText 中设置该数据字符串。我需要做什么来执行此代码才能实现这一目标?提前致谢。

(另外,对不起,如果这是一个愚蠢的问题,我是 android 和 Java 的新手)

使用 EditText 的活动:

EditText CPU = (EditText) findViewById(R.id.autoCompleteTextView4);
    CPU.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent CPUList = new Intent(getApplicationContext(),
                    CPUList.class);
            startActivityForResult(CPUList, RESULT_OK);
        }

    });

列出活动

final ListView listViewCPU = (ListView) findViewById(R.id.listViewCPU);
    listViewCPU.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    String CPUList[] = {

    "CPU's go here", "CPU's go here", "CPU's go here", "CPU's go here" };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, CPUList);

    listViewCPU.setAdapter(adapter);

     listViewCPU.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> myAdapter, View myView, int  pos, long mylng) {
            String CPU =(listViewCPU.getItemAtPosition(pos).toString());

        }
    });

}

}
4

1 回答 1

0

The ArrayAdapter contains your data. If you grab a reference to your adapter, you can fetch the string from that position.

The other thing to note is that in your OnItemClick listener you named your AdapterView myAdapter, the truth is that the adapter view is just a view that implements an adapter, so in this case the adapter view would be your listview.

Below is how you would get your string from the click listener:

listViewCPU.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> listview, View myView, int  pos, long mylng) {
        String CPU = (String) listview.getAdapter().getItem(pos);

    }
});
于 2013-08-06T15:48:48.237 回答