I\'m trying to create a ListView
(with custom CursorAdapter
) of EditText
items such that EditTexts
appear uneditable at f
OK. Here is your code. hope it suits your needs or at least that's what I've gotten from your post :)
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.Log;
import android.view.*;
import android.widget.*;
import java.util.ArrayList;
import java.util.List;
public class EditListActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(lp1);
ll.setOrientation(1);
ListView lv = new ListView(this);
MyListAdapter adapter = new MyListAdapter(this, null);
lv.setAdapter(adapter);
ll.addView(lv);
setContentView(ll);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
class MyListAdapter extends BaseAdapter implements View.OnLongClickListener, View.OnFocusChangeListener {
private final String TAG = ListActivity.class.getName();
List stringsarray;
private Context context;
public MyListAdapter(Context context, List phonebook) {
this.stringsarray = new ArrayList();
this.stringsarray.add("test1");
this.stringsarray.add("test2");
this.stringsarray.add("test3");
Log.d(TAG, "created list adapter");
this.setContext(context);
}
public int getCount() {
return this.stringsarray.size();
}
public String getItem(int position) {
return this.stringsarray.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LinearLayout rl = new LinearLayout(getContext());
rl.setOrientation(1);
EditText text = new EditText(getContext());
text.setOnLongClickListener(this);
text.setOnFocusChangeListener(this);
text.setText(this.stringsarray.get(position));
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
text.setFocusable(true);
rl.addView(text, lp);
return rl;
}
public void onFocusChange(View view, boolean b) {
EditText et = (EditText) view;
et.setFilters(new InputFilter[]{
new InputFilter() {
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend) {
return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
}
}
});
if (b) {
Log.d(TAG, "hasFocus true called " + et.getText());
et.setText("focused");
et.setSelection(et.length());
} else {
Log.d(TAG, "hasFocus false called " + et.getText());
et.setText("unfocused");
//TODO Save to DB
}
}
public boolean onLongClick(View view) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
final EditText et = (EditText) view;
et.setFilters(new InputFilter[]{
new InputFilter() {
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend) {
return src;
}
}
});
et.setText("editable");
Log.d(TAG, "on long click called " + et.getText());
et.requestFocus();
et.setFocusableInTouchMode(true);
return true;
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
}
}
have fun.