在上篇中用到是GridView实现的,但是Listview是不是不能实现了呢?呵呵,用点时间去看看,貌似是可以实现的,看代码
package com.example.listvieweditdemo;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private ListView listview;
private Adapter adapter;
private List<Goods> list=new ArrayList<Goods>();
private int selectPosition=-1; //记录选中的edtitext
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
this.listview=(ListView) findViewById(R.id.listview);
this.adapter=new Adapter();
this.listview.setAdapter(adapter);
for(int i=0;i<25;i++){
Goods good=new Goods();
good.setGoodsName("android"+i);
good.setGoodsCount("");
list.add(good);
}
adapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public class Adapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// ViewHolder holder=null;
// if(null == convertView){
convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
// holder=new ViewHolder();
TextView textView=(TextView) convertView.findViewById(R.id.textView1);
EditText editText=(EditText) convertView.findViewById(R.id.editText1);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
// convertView.setTag(holder);
// }else{
// holder=(ViewHolder) convertView.getTag();
// }
textView.setText(list.get(position).getGoodsName());
String str=list.get(position).getGoodsCount();
if(str.equalsIgnoreCase("")){
editText.setHint("请输入");
}else{
editText.setText(str);
}
//获取焦点
if(selectPosition == position){
editText.requestFocus();
}
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
selectPosition=position;
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
//在这里做事
list.get(position).setGoodsCount(s.toString());
}
});
return convertView;
}
}
public static class ViewHolder{
private TextView textView;
private EditText editText;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.listvieweditdemo.MainActivity"
tools:ignore="MergeRootFrame" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
运行一下看看效果: