我想修改布局顶部的按钮,然后在按钮下方创建动态编辑文本。我正在创建自己的适配器。在按钮上单击另一个活动启动并返回要在按钮下方的列表视图项上显示的字符串。我怎么能这样做?
我的XML布局 -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<EditText
android:id="@+id/etList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
</LinearLayout>
我的列表视图适配器 -
package com.example.tasktable;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class TaskAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
Button click;
TextView textView;
public TaskAdapter(Context context, String[] values) {
super(context, R.layout.list_view, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflator.inflate(R.layout.list_view, parent, false);
click = (Button) rowView.findViewById(R.id.button1);
textView = (TextView) rowView.findViewById(R.id.etList);
textView.setText(values[position]);
return rowView;
}
}
我不想用每个编辑文字给按钮充气。
答案 0 :(得分:0)
答案 1 :(得分:0)
//try this way, hope this will help you...
**XML** code
**activity.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:id="@+id/lnrEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
**list_activity.xml**
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
**list_item.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/txtValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btnSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select"/>
</LinearLayout>
**ACTIVITY** code
**MyActivity**
public class MyActivity extends Activity{
private Button btnAdd;
private LinearLayout lnrEdit;
private final int GET_LIST_ITEM = 1;
private HashMap<String,String> listItemMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
btnAdd = (Button) findViewById(R.id.btnAdd);
lnrEdit = (LinearLayout) findViewById(R.id.lnrEdit);
listItemMap = new HashMap<String, String>();
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MyActivity.this,MyListActivty.class);
startActivityForResult(intent,GET_LIST_ITEM);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if(requestCode == GET_LIST_ITEM);
{
String getListValue = data.getStringExtra("value");
int index = data.getIntExtra("index",0);
if(listItemMap.containsKey(String.valueOf(index))){
Toast.makeText(this,"This item already taken !!!",Toast.LENGTH_LONG).show();
} else{
listItemMap.put(String.valueOf(index),getListValue);
EditText editText = new EditText(this);
editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setId(index);
editText.setText(getListValue);
editText.setTag(getListValue);
editText.setFocusable(false);
editText.setFocusableInTouchMode(false);
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value = v.getTag().toString();
Toast.makeText(MyActivity.this,value,Toast.LENGTH_SHORT).show();
}
});
lnrEdit.addView(editText);
}
}
}
}
}
**MyListActivity**
public class MyListActivty extends ListActivity{
private String[] listArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_activity);
listArray = new String[]{"Android4.1","Android4.2","Android4.3","Android4.4","Android4.5"};
setListAdapter(new TaskAdapter(this,listArray));
}
class TaskAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public TaskAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
holder.button = (Button) convertView.findViewById(R.id.btnSelect);
holder.textView = (TextView) convertView.findViewById(R.id.txtValue);
convertView.setTag(holder);
} else{
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(values[position]);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("index",position);
intent.putExtra("value",values[position]);
setResult(RESULT_OK,intent);
finish();
}
});
return convertView;
}
@Override
public int getCount() {
return values.length;
}
}
static class ViewHolder{
Button button;
TextView textView;
}
}