上下文 ** 我是 Android 新手。当我将文本设置为 holder.column1.other 列时,getView 方法出错。我评论这两行。这是我的代码 - ** MulticolumnListViewCustomAdapter.java
public class MulticolumnListViewCustomAdapter extends ArrayAdapter<Feedback>{
public static final String TAG = MulticolumnListViewCustomAdapter.class.getSimpleName();
private ArrayList<Feedback> data;
private LayoutInflater mInflater;
private int layoutResource;
final public static int SORT_CUST_NAME_ASCENDING = 1;
final public static int SORT_CUST_NAME_DESCENDING = 2;
final public static int SORT_CUST_EMAIL_ASCENDING = 3;
final public static int SORT_CUST_EMAIL_DESCENDING = 4;
final public static int SORT_CUST_SEX_ASCENDING = 5;
final public static int SORT_CUST_SEX_DESCENDING = 6;
final public static int SORT_CUST_FEEDTYPE_ASCENDING = 7;
final public static int SORT_CUST_FEEDTYPE_DESCENDING = 8;
final public static int SORT_CUST_FEEDBACK_ASCENDING = 9;
final public static int SORT_CUST_FEEDBACK_DESCENDING = 10;
private HashMap<Integer, Boolean> checkedList = new HashMap<Integer, Boolean>();
private CheckBox checkAllBox;
private Boolean cancelSetCheckedAll = false;
public MulticolumnListViewCustomAdapter(Context context, int textViewResourceId, ArrayList<Feedback> data, CheckBox checkAllBox, int sortMode) {
super(context, textViewResourceId, data);
constructor(context, textViewResourceId, data, checkAllBox, (sortMode<1||sortMode>10)?SORT_CUST_NAME_ASCENDING:sortMode);
}
public MulticolumnListViewCustomAdapter(Context context, int textViewResourceId, ArrayList<Feedback> data, CheckBox checkAllBox) {
super(context, textViewResourceId, data);
constructor(context, textViewResourceId, data, checkAllBox, SORT_CUST_NAME_ASCENDING);
}
public void constructor(Context context, int textViewResourceId, ArrayList<Feedback> data, CheckBox checkAllBox, int sortMode) {
this.checkAllBox = checkAllBox;
this.data = data;
switch(sortMode) {
default:
case SORT_CUST_NAME_ASCENDING:
sortByCustNameAscending(false);
break;
case SORT_CUST_NAME_DESCENDING:
sortByCustNameDescending(false);
break;
case SORT_CUST_EMAIL_ASCENDING:
sortByCustEmailAscending(false);
break;
case SORT_CUST_EMAIL_DESCENDING:
sortByCustEmailDescending(false);
break;
case SORT_CUST_SEX_ASCENDING:
sortByCustSexAscending(false);
break;
case SORT_CUST_SEX_DESCENDING:
sortByCustSexDescending(false);
break;
case SORT_CUST_FEEDTYPE_ASCENDING:
sortByCustFeedTypeAscending(false);
break;
case SORT_CUST_FEEDTYPE_DESCENDING:
sortByCustFeedTypeDescending(false);
break;
case SORT_CUST_FEEDBACK_ASCENDING:
sortByCustFeedbackAscending(false);
break;
case SORT_CUST_FEEDBACK_DESCENDING:
sortByCustFeedbackDescending(false);
break;
}
this.mInflater = LayoutInflater.from(context);
this.layoutResource = textViewResourceId;
checkAllBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!cancelSetCheckedAll)setCheckedAll(isChecked); else cancelSetCheckedAll=false;
}
});
}
public void setCheckedAll(Boolean state) {
checkedList.clear();
for (Feedback item : this.data) {
checkedList.put(Integer.valueOf(item.getListId()), state);
}
refreshList(true);
}
public void sortByCustNameAscending(Boolean refreshList) {
this.data = SortFeedbackResults.sortByCustNameAscending(this.data);
refreshList(refreshList);
}
public void sortByCustNameDescending(Boolean refreshList) {
this.data = SortFeedbackResults.sortByCustNameDescending(this.data);
refreshList(refreshList);
}
public void sortByCustEmailAscending(Boolean refreshList) {
this.data = SortFeedbackResults.sortByCustEmailAscending(this.data);
refreshList(refreshList);
}
public void sortByCustEmailDescending(Boolean refreshList) {
this.data = SortFeedbackResults.sortByCustEmailDescending(this.data);
refreshList(refreshList);
}
public void sortByCustSexAscending(Boolean refreshList) {
this.data = SortFeedbackResults.sortByCustSexAscending(this.data);
refreshList(refreshList);
}
public void sortByCustSexDescending(Boolean refreshList) {
this.data = SortFeedbackResults.sortByCustSexDescending(this.data);
refreshList(refreshList);
}
public void sortByCustFeedTypeAscending(Boolean refreshList) {
this.data = SortFeedbackResults.sortByCustFeedTypeAscending(this.data);
refreshList(refreshList);
}
public void sortByCustFeedTypeDescending(Boolean refreshList) {
this.data = SortFeedbackResults.sortByCustFeedTypeDescending(this.data);
refreshList(refreshList);
}
public void sortByCustFeedbackAscending(Boolean refreshList) {
this.data = SortFeedbackResults.sortByCustFeedbackAscending(this.data);
refreshList(refreshList);
}
public void sortByCustFeedbackDescending(Boolean refreshList) {
this.data = SortFeedbackResults.sortByCustFeedbackDescending(this.data);
refreshList(refreshList);
}
public void sortByCustNameAscending() {
sortByCustNameAscending(true);
}
public void sortByCustNameDescending() {
sortByCustNameDescending(true);
}
public void sortByCustEmailAscending() {
sortByCustEmailAscending(true);
}
public void sortByCustEmailDescending() {
sortByCustEmailDescending(true);
}
public void sortByCustSexAscending() {
sortByCustSexAscending(true);
}
public void sortByCustSexDescending() {
sortByCustSexDescending(true);
}
public void sortByCustFeedTypeAscending() {
sortByCustFeedTypeAscending(true);
}
public void sortByCustFeedTypeDescending() {
sortByCustFeedTypeDescending(true);
}
public void sortByCustFeedbackAscending() {
sortByCustFeedbackAscending(true);
}
public void sortByCustFeedbackDescending() {
sortByCustFeedbackDescending(true);
}
public void refreshList(Boolean really) {
if (really) {
notifyDataSetChanged();
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View v = convertView;
if(v == null){
v = mInflater.inflate(layoutResource, null);
holder = new ViewHolder();
holder.id = (TextView)v.findViewById(R.id.custid);
holder.column1 = (CheckBox)v.findViewById(R.id.column1);
holder.column2 = (TextView)v.findViewById(R.id.column2);
holder.column3 = (TextView)v.findViewById(R.id.column3);
holder.column4 = (TextView)v.findViewById(R.id.column4);
holder.column5 = (TextView)v.findViewById(R.id.column5);
holder.column6 = (TextView)v.findViewById(R.id.column6);
holder.column7 = (TextView)v.findViewById(R.id.column7);
holder.column1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = Integer.valueOf((String) holder.id.getText());
checkedList.remove(id);
checkedList.put(id, isChecked);
if(isChecked==false&&checkAllBox.isChecked()){cancelSetCheckedAll=true;checkAllBox.setChecked(false);}
}
});
v.setTag(holder);
}else{
holder = (ViewHolder)v.getTag();
}
Feedback c = data.get(position);
if(c != null){
//holder.id.setText(c.getListId());
//holder.id.setVisibility(View.VISIBLE);
int id = Integer.valueOf(c.getListId());
if(checkedList.containsKey(id)){
holder.column1.setChecked(checkedList.get(id));
}else{
holder.column1.setChecked(false);
}
holder.column1.setVisibility(View.VISIBLE);
holder.column2.setText(c.getName());
holder.column2.setVisibility(View.VISIBLE);
holder.column3.setText(c.getEmail());
holder.column3.setVisibility(View.VISIBLE);
holder.column4.setText(c.getSex());
holder.column4.setVisibility(View.VISIBLE);
holder.column5.setText(c.getFeedbackType());
holder.column5.setVisibility(View.VISIBLE);
holder.column6.setText(c.getFeedback());
holder.column6.setVisibility(View.VISIBLE);
holder.column7.setText(c.getEmailRes());
holder.column7.setVisibility(View.VISIBLE);
}else{
holder.id.setVisibility(View.GONE);
holder.column1.setVisibility(View.GONE);
holder.column2.setVisibility(View.GONE);
holder.column3.setVisibility(View.GONE);
holder.column4.setVisibility(View.GONE);
holder.column5.setVisibility(View.GONE);
holder.column6.setVisibility(View.GONE);
}
return v;
}
private static class ViewHolder {
TextView id;
CheckBox column1;
TextView column2;
TextView column3;
TextView column4;
TextView column5;
TextView column6;
TextView column7;
}
private static class SortFeedbackResults {
public static ArrayList<Feedback> sortByCustNameAscending(ArrayList<Feedback> list){
Collections.sort(list, new SortFeedbackResultsByCustNameAscending());
return list;
}
private static class SortFeedbackResultsByCustNameAscending implements Comparator<Feedback>{
public int compare(Feedback lhs, Feedback rhs) {
if(lhs.getName().equals(rhs.getName())){
return lhs.getEmail().compareTo(rhs.getEmail());
}
return lhs.getName().compareTo(rhs.getName());
}
}
public static ArrayList<Feedback> sortByCustNameDescending(ArrayList<Feedback> list){
Collections.sort(list, new SortFeedbackResultsByCustNameDescending());
return list;
}
private static class SortFeedbackResultsByCustNameDescending implements Comparator<Feedback>{
public int compare(Feedback lhs, Feedback rhs) {
if(lhs.getName().equals(rhs.getName())){
return lhs.getEmail().compareTo(rhs.getEmail());
}
int result = lhs.getName().compareTo(rhs.getName());
return (result>0)?-result:-0-result;
}
}
public static ArrayList<Feedback> sortByCustEmailAscending(ArrayList<Feedback> list){
Collections.sort(list, new SortFeedbackResultsByCustEmailAscending());
return list;
}
private static class SortFeedbackResultsByCustEmailAscending implements Comparator<Feedback>{
public int compare(Feedback lhs, Feedback rhs) {
return lhs.getEmail().compareTo(rhs.getEmail());
}
}
public static ArrayList<Feedback> sortByCustEmailDescending(ArrayList<Feedback> list){
Collections.sort(list, new SortFeedbackResultsByCustEmailDescending());
return list;
}
private static class SortFeedbackResultsByCustEmailDescending implements Comparator<Feedback>{
public int compare(Feedback lhs, Feedback rhs) {
int result = lhs.getEmail().compareTo(rhs.getEmail());
return (result>0)?-result:-0-result;
}
}
public static ArrayList<Feedback> sortByCustSexAscending(ArrayList<Feedback> list){
Collections.sort(list, new SortFeedbackResultsByCustSexAscending());
return list;
}
private static class SortFeedbackResultsByCustSexAscending implements Comparator<Feedback>{
public int compare(Feedback lhs, Feedback rhs) {
if(lhs.getSex().equals(rhs.getSex())){
return lhs.getEmail().compareTo(rhs.getEmail());
}
return lhs.getSex().compareTo(rhs.getSex());
}
}
public static ArrayList<Feedback> sortByCustSexDescending(ArrayList<Feedback> list){
Collections.sort(list, new SortFeedbackResultsByCustSexDescending());
return list;
}
private static class SortFeedbackResultsByCustSexDescending implements Comparator<Feedback>{
public int compare(Feedback lhs, Feedback rhs) {
if(lhs.getSex().equals(rhs.getSex())){
return lhs.getEmail().compareTo(rhs.getEmail());
}
int result = lhs.getSex().compareTo(rhs.getSex());
return (result>0)?-result:-0-result;
}
}
public static ArrayList<Feedback> sortByCustFeedTypeAscending(ArrayList<Feedback> list){
Collections.sort(list, new SortFeedbackResultsByCustFeedTypeAscending());
return list;
}
private static class SortFeedbackResultsByCustFeedTypeAscending implements Comparator<Feedback>{
public int compare(Feedback lhs, Feedback rhs) {
if(lhs.getFeedbackType().equals(rhs.getFeedbackType())){
return lhs.getEmail().compareTo(rhs.getEmail());
}
return lhs.getFeedbackType().compareTo(rhs.getFeedbackType());
}
}
public static ArrayList<Feedback> sortByCustFeedTypeDescending(ArrayList<Feedback> list){
Collections.sort(list, new SortFeedbackResultsByCustFeedTypeDescending());
return list;
}
private static class SortFeedbackResultsByCustFeedTypeDescending implements Comparator<Feedback>{
public int compare(Feedback lhs, Feedback rhs) {
if(lhs.getFeedbackType().equals(rhs.getFeedbackType())){
return lhs.getEmail().compareTo(rhs.getEmail());
}
int result = lhs.getFeedbackType().compareTo(rhs.getFeedbackType());
return (result>0)?-result:-0-result;
}
}
public static ArrayList<Feedback> sortByCustFeedbackAscending(ArrayList<Feedback> list){
Collections.sort(list, new SortFeedbackResultsByCustFeedbackAscending());
return list;
}
private static class SortFeedbackResultsByCustFeedbackAscending implements Comparator<Feedback>{
public int compare(Feedback lhs, Feedback rhs) {
if(lhs.getFeedback().equals(rhs.getFeedback())){
return lhs.getEmail().compareTo(rhs.getEmail());
}
return lhs.getFeedback().compareTo(rhs.getFeedback());
}
}
public static ArrayList<Feedback> sortByCustFeedbackDescending(ArrayList<Feedback> list){
Collections.sort(list, new SortFeedbackResultsByCustFeedbackDescending());
return list;
}
private static class SortFeedbackResultsByCustFeedbackDescending implements Comparator<Feedback>{
public int compare(Feedback lhs, Feedback rhs) {
if(lhs.getFeedback().equals(rhs.getFeedback())){
return lhs.getEmail().compareTo(rhs.getEmail());
}
int result = lhs.getFeedback().compareTo(rhs.getFeedback());
return (result>0)?-result:-0-result;
}
}
}
}
**MainActivity----**
public class MulticolumnListViewTest extends Activity {
private Boolean column2State = true;
private Boolean column3State = true;
private Boolean column4State = true;
private Boolean column5State = true;
private Boolean column6State = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multicolumn_list_view_test);
this.getApplicationContext();
final ListView list = (ListView) findViewById(R.id.mylist);
ArrayList<Feedback> myListData = new ArrayList<Feedback>();
for(int i=1; i<=100; i++){
Feedback feedback = new Feedback();
String iString = Integer.toString(i);
feedback.setName("Name"+i);
feedback.setEmail("Email"+i);
feedback.setSex("Sex"+i);
feedback.setFeedbackType("FeedType"+i);
feedback.setFeedback("Feedback"+i);
feedback.setEmailRes("Res"+i);
feedback.setListId(iString);
myListData.add(feedback);
}
final MulticolumnListViewCustomAdapter multicolumnListViewCustomAdapter = new MulticolumnListViewCustomAdapter(this, R.layout.activity_multicolumn_list_view_test, myListData, (CheckBox)findViewById(R.id.column1));
list.setAdapter(multicolumnListViewCustomAdapter);
final LinearLayout column2 = (LinearLayout) findViewById(R.id.column2_container);
final LinearLayout column3 = (LinearLayout) findViewById(R.id.column3_container);
final LinearLayout column4 = (LinearLayout) findViewById(R.id.column4_container);
final LinearLayout column5 = (LinearLayout) findViewById(R.id.column5_container);
final LinearLayout column6 = (LinearLayout) findViewById(R.id.column6_container);
final TextView column2Arrow = (TextView)column2.getChildAt(1);
final TextView column3Arrow = (TextView)column3.getChildAt(1);
final TextView column4Arrow = (TextView)column4.getChildAt(1);
final TextView column5Arrow = (TextView)column5.getChildAt(1);
final TextView column6Arrow = (TextView)column6.getChildAt(1);
column2.setOnTouchListener(columnContainerOnTouchListener(column2));
column3.setOnTouchListener(columnContainerOnTouchListener(column3));
column4.setOnTouchListener(columnContainerOnTouchListener(column4));
column5.setOnTouchListener(columnContainerOnTouchListener(column5));
column6.setOnTouchListener(columnContainerOnTouchListener(column6));
column2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("COLUMN 2 CLICK");
if(column2State){
column2State = false;
column2Arrow.setText(R.string.solid_down);
column3Arrow.setText("");
column4Arrow.setText("");
column5Arrow.setText("");
column6Arrow.setText("");
multicolumnListViewCustomAdapter.sortByCustNameDescending();
}else{
column2State = true;
column3Arrow.setText("");
column4Arrow.setText("");
column5Arrow.setText("");
column6Arrow.setText("");
multicolumnListViewCustomAdapter.sortByCustNameAscending();
}
}
});
column3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("COLUMN 3 CLICK");
if(column3State){
column3State = false;
column3Arrow.setText(R.string.solid_down);
column2Arrow.setText("");
column4Arrow.setText("");
column5Arrow.setText("");
column6Arrow.setText("");
multicolumnListViewCustomAdapter.sortByCustEmailDescending();
}else{
column3State = true;
column2Arrow.setText("");
column4Arrow.setText("");
column5Arrow.setText("");
column6Arrow.setText("");
multicolumnListViewCustomAdapter.sortByCustEmailAscending();
}
}
});
column4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("COLUMN 4 CLICK");
if(column4State){
column4State = false;
column4Arrow.setText(R.string.solid_down);
column3Arrow.setText("");
column2Arrow.setText("");
column5Arrow.setText("");
column6Arrow.setText("");
multicolumnListViewCustomAdapter.sortByCustSexDescending();
}else{
column4State = true;
column3Arrow.setText("");
column2Arrow.setText("");
column5Arrow.setText("");
column6Arrow.setText("");
multicolumnListViewCustomAdapter.sortByCustSexAscending();
}
}
});
column5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("COLUMN 5 CLICK");
if(column5State){
column5State = false;
column5Arrow.setText(R.string.solid_down);
column3Arrow.setText("");
column4Arrow.setText("");
column2Arrow.setText("");
column6Arrow.setText("");
multicolumnListViewCustomAdapter.sortByCustFeedTypeDescending();
}else{
column5State = true;
column3Arrow.setText("");
column4Arrow.setText("");
column2Arrow.setText("");
column6Arrow.setText("");
multicolumnListViewCustomAdapter.sortByCustFeedTypeAscending();
}
}
});
column6.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("COLUMN 2 CLICK");
if(column6State){
column6State = false;
column6Arrow.setText(R.string.solid_down);
column3Arrow.setText("");
column4Arrow.setText("");
column5Arrow.setText("");
column2Arrow.setText("");
multicolumnListViewCustomAdapter.sortByCustFeedbackDescending();
}else{
column6State = true;
column3Arrow.setText("");
column4Arrow.setText("");
column5Arrow.setText("");
column2Arrow.setText("");
multicolumnListViewCustomAdapter.sortByCustFeedbackAscending();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_multicolumn_list_view_test, menu);
return true;
}
public OnTouchListener columnContainerOnTouchListener(final LinearLayout column){
return new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(MotionEvent.ACTION_DOWN == event.getAction()){
column.setBackgroundColor(Color.argb(10, 0, 0, 0));
}else if(MotionEvent.ACTION_UP == event.getAction()){
column.setBackgroundColor(Color.TRANSPARENT);
}
return false;
}
};
}
}