我正在尝试使用Textview和复选框创建自定义列表视图,列表将被创建,以便用户通过将其写入自定义对话框将项目添加到列表中。
我的代码如下:
package ayush.lists;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Current extends Activity {
Model list[] = new Model[100];
String s;
int i = 0;
final Context context = this;
Dialog dia = new Dialog(context);
ListView l1 = (ListView) findViewById(R.id.listView1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current);
Button addi = (Button) findViewById(R.id.bt1);
Button sav = (Button) findViewById(R.id.bt2);
addi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StartDialog();
}
});
}
public void StartDialog() {
dia.setTitle("Add Item");
dia.setContentView(R.layout.cust_dialog);
TextView t2 = (TextView) dia.findViewById(R.id.textView1);
t2.setText("Name of the Item:");
Button add = (Button) dia.findViewById(R.id.button1);
Button add_another = (Button) dia.findViewById(R.id.button2);
Button cancel = (Button) dia.findViewById(R.id.button3);
EditText item = (EditText) dia.findViewById(R.id.editText1);
s = item.getText().toString();
list[i++] = new Model(s, 0);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (s.length() >= 0) {
CustList adapter = new CustList(v.getContext(), list);
l1.setAdapter(adapter);
dia.dismiss();
} else
Toast.makeText(getApplicationContext(),
"Please Enter Valid Input", 2000).show();
}
});
add_another.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (s.length() >= 0) {
CustList adapter = new CustList(v.getContext(), list);
l1.setAdapter(adapter);
dia.dismiss();
StartDialog();
} else
Toast.makeText(getApplicationContext(),
"Please Enter Valid Input", 2000).show();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dia.dismiss();
}
});
dia.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.current, 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);
}
}
自定义适配器的代码是
package ayush.lists;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class CustList extends ArrayAdapter<Model> {
Model[] modelItems = null;
Context context;
public CustList(Context context, Model[] resource) {
super(context, R.layout.list_item, resource);
// TODO Auto-generated constructor stub
this.context = context;
this.modelItems = resource;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.list_item, parent, false);
TextView name = (TextView) convertView.findViewById(R.id.textView1);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
name.setText(modelItems[position].getName());
if (modelItems[position].getValue() == 1)
cb.setChecked(true);
else
cb.setChecked(false);
return convertView;
}
}
你能告诉我为什么这不起作用。我最近开始进行android编程,可能会有很多错误,我会很感激所有的反馈。如果可能,请更正代码并回答。
答案 0 :(得分:3)
添加
final Context context = this;
Dialog dia = new Dialog(context);
ListView l1 = (ListView) findViewById(R.id.listView1);
<{1>}方法中的,因为Activity方法中提供了onCreate
。改变
activity context
到
final Context context = this;
Dialog dia = new Dialog(context);
ListView l1 = (ListView) findViewById(R.id.listView1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current);