在我的应用程序中,有一个包含一些数据的列表视图。在同一个活动中有一个edittext和一个按钮。当我在edittext中输入内容时,我想将该文本作为新项添加到按钮上的ListView中点击。怎么做?请帮忙......
以下是我的代码示例...
这是我的customList适配器
public class MainActivity extends Activity
{
ListView list;
String[] web = {
"Google Plus",
"Twitter",
"Windows",
"Bing",
"Itunes",
"Wordpress",
"Drupal"
} ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText et = (EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "Button clicked...", Toast.LENGTH_SHORT).show();
}
});
CustomList adapter = new CustomList(MainActivity.this, web);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
这是我的适配器类
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] web;
public CustomList(Activity context, String[] web) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
txtTitle.setText(web[position]);
return rowView;
}
}
答案 0 :(得分:4)
声明
final ArrayList<String> dataset = new ArrayList<String>();
Button b=(Button)findViewById(R.id.button1);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
final CustomList adapter = new CustomList(MainActivity.this, dataset, imageId);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0)
{
dataset.add(et.getText().toString());
adapter.notifyDataSetChanged();
}
});
然后
private ArrayList<String> web;
public CustomList(Activity context, ArrayList<String> web, Integer[] imageId) {
super(context, R.layout.list_single, web);
在getView中
txtTitle.setText(web.get(position));
编辑:
public class MainActivity extends Activity
{
EditText ed;
CustomList adapter;
ArrayList<String> list = new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) this.findViewById(R.id.button1);
ListView lv = (ListView) this.findViewById(R.id.listView1);
ed= (EditText) this.findViewById(R.id.editText1);
adapter = new CustomList(this,list);
lv.setAdapter(adapter);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
list.add(ed.getText().toString());
adapter.notifyDataSetChanged();
}
});
}
}
activity_main.xml中;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/button1"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1" >
</ListView>
</RelativeLayout>
CustomList
public class CustomList extends ArrayAdapter<String>{
private ArrayList<String >web;
LayoutInflater mInflater;
public CustomList(Context context, ArrayList<String> web) {
super(context, 0, web);
mInflater= LayoutInflater.from(context);
this.web = web;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View rowView= mInflater.inflate(R.layout.list_item,parent,false);
TextView txtTitle = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(web.get(position));
return rowView;
}
}
LIST_ITEM
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="TextView" />
</RelativeLayout>
对齐
答案 1 :(得分:0)
在 arraylist / list / Array 中添加项目,然后使用以下通知您的列表:
adapterObject.notifyDataSetChanged()
答案 2 :(得分:0)
你可以试试这个
ArrayList<String> web=new ArrayList<String>();
b.setOnClickListener(new View.onClickListener(){
@Override
public void onClick(View arg0)
{
web.add(et.getText().toString());
adapter.notifyDataSetChanged();
}
});
答案 3 :(得分:0)
更新网络和ImageId后。
您必须在适配器上调用notifyDataSetChanged。
尝试使用更改此代码:
EditText et = (EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
CustomList adapter = new CustomList(MainActivity.this, web, imageId);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0)
{
//update the data on the web and imageID
Toast.makeText(MainActivity.this, "Button clicked...",
Toast.LENGTH_SHORT).show();
//call the notifydatasetChanged on the adapter
adapter.notifyDataSetChanged();
}
});
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});