如何从edittext到listview获取文本?

时间:2014-12-03 10:34:22

标签: android listview android-listview

我试图将文本从edittext转到listview。我已在listview中添加了图像和textview。现在我需要知道如何从edittext获取数据并应用于listview。我在下面添加了我的代码:

活动类:

public class MainActivity extends ActionBarActivity {
    int[] image={R.drawable.confidential};

     String[] web = new String[] {"1","2"};
     Integer[] imageId = {
              R.drawable.confidential,R.drawable.confidential1};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText ed =(EditText) findViewById(R.id.editText1);

        Button bt=(Button) findViewById(R.id.button1);



        //final Typeface custom_font = Typeface.createFromAsset(getAssets(), "font/d.ttf");
        // String ts1 = ed.getText().toString();


        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // String ts1 = ed.getText().toString();
                // tx.setText(ts1);
               //  tx.setTypeface(custom_font);
                 CustomList adapter = new
                            CustomList(MainActivity.this, web, imageId);
                 ListView list=(ListView)findViewById(R.id.listView1);
                 list.setAdapter(adapter);

            }
        });


    }}

我的适配器类:

public class CustomList extends ArrayAdapter<String>{
    private final Activity context;
    private final String[] web;
    private final Integer[] imageId;
    public CustomList(Activity context,String[] web, Integer[] imageId) {

    super(context, R.layout.list, web);
    this.context = context;
    this.web = web;
    this.imageId = imageId;
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.list, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imageview);
    txtTitle.setText(web[position]);
    imageView.setImageResource(imageId[position]);
    return rowView;
    }
    }

请帮我提供示例代码.. !!谢谢!

1 个答案:

答案 0 :(得分:0)

如果您想要单一选择,请使用Spinner而不是使用listview。下面列出了所选列表视图项的代码。

list.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long arg3) 
{
        String value = (String)adapter.getItemAtPosition(position); 
        // assuming string and if you want to get the value on click of list item
        // do what you intend to do on click of listview row
   }
  }
 );