0

我创建了一个适配器,将一些消息文本设置为 textView 和一个简单的按钮,但仍然看不到该适配器的 GUI。

我没有任何错误,但在那之前的屏幕上仍然看不到适配器。

适配器代码:

public class messageDisplayAdapter extends ArrayAdapter {

public Context mContext;
private String message;
public AlertDialog alertDialog;
private int id;

public messageDisplayAdapter(Context context, int textViewResourceId, String message) {
    super(context, textViewResourceId);
    mContext = context;
    id = textViewResourceId;
    this.message=message;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (row == null) {
     LayoutInflater inflater =LayoutInflater.from(this.mContext);
     row = inflater.inflate(R.layout.message, parent, false);
    }
   // final int itemPosition = position;
    TextView Item = (TextView) row.findViewById(R.id.textView );
    Button btn = (Button) row.findViewById(R.id.ButtonOk);
    btn.setOnClickListener( new View.OnClickListener() {
    public void onClick(View v) {
  // Toast.makeText(getApplicationContext(), "Toast from button"+                        //Colors[itemPosition], Toast.LENGTH_SHORT).show();    
  }
 });
    return row;

}

以及显示适配器的类:

 protected void onPostExecute(String result) {
    try {
        Dialog.dismiss();
        Intent intent = new Intent(this.context, Class.forName(this.newActivity));


        context.startActivity(intent);
        //finish();}

    } catch (ClassNotFoundException ex) {
      /*  messageDisplayAdapter messageDisplayAdapter = new messageDisplayAdapter(this.context, R.layout.message, "error on loading");
        messageDisplayAdapter.showMessage();
    */

     ListView lv =new ListView(this.context); 
    lv.setTextFilterEnabled(true);
    messageDisplayAdapter  listAdapter= new messageDisplayAdapter(this.context,R.layout.message,"error on loading");
   // listAdpter.setListAdapter(listAdapter); // on test
    lv.setAdapter(listAdapter); // on test

    }

}

感谢帮助!

4

1 回答 1

0

尝试在getView中给textView添加setText,如下

public class messageDisplayAdapter extends ArrayAdapter {

public Context mContext;
private String message;
public AlertDialog alertDialog;
private int id;

public messageDisplayAdapter(Context context, int textViewResourceId, String message) {
  super(context, textViewResourceId);
  mContext = context;
  id = textViewResourceId;
  this.message=message; 
}

public View getView(int position, View convertView, ViewGroup parent) {
  View row = convertView;
  if (row == null) {
   LayoutInflater inflater =LayoutInflater.from(this.mContext);
   row = inflater.inflate(R.layout.message, parent, false);
  }
 // final int itemPosition = position;
  TextView Item = (TextView) row.findViewById(R.id.textView );
  Item.setText(message); // Try This line
  Button btn = (Button) row.findViewById(R.id.ButtonOk);
  btn.setOnClickListener( new View.OnClickListener() {
  public void onClick(View v) {
// Toast.makeText(getApplicationContext(), "Toast from button"+                             //Colors[itemPosition], Toast.LENGTH_SHORT).show();    
  }
});
return row;

}

另外请尝试遵循命名约定。

于 2012-09-25T05:59:43.517 回答