我有两个java类和两个带按钮的listview的xml文件。我想将listview行中对应文本的数据传递给线性布局。
这是我的第一个java类的代码。
public class Items_momo extends Activity {
LinearLayout myLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_items_momo);
ArrayList<String> list = new ArrayList<String>();
list.add("Veg Steam Momo");
list.add("Veg C-Momo");
list.add("Veg Fry Momo");
list.add("Veg Kothey Momo");
list.add("Buff Steam Momo");
list.add("Buff Fry Momo");
list.add("Buff Kothey Momo");
list.add("Buff C-Momo");
list.add("Chicken Steam Momo");
list.add("Chicken Fry Momo");
list.add("Chicken Kothey Momo");
list.add("Chicken C-Momo");
list.add("Paneer Steam Momo");
list.add("Paneer Fry Momo");
list.add("Paneer Kothey Momo");
list.add("Paneer C-Momo");
MyCostumAdapter adapter = new MyCostumAdapter(list, this);
ListView lView = (ListView) findViewById(R.id.momo_list);
lView.setAdapter(adapter);
}
}
,第二类代码是
public class MoMoCostumAdapter extends BaseAdapter implements ListAdapter {
LinearLayout myLayout;
private ArrayList<String> list =new ArrayList<String>();
private Context context;
public MoMoCostumAdapter(ArrayList<String> list,Context context){
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@Override
public long getItemId(int pos) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.costum_momolayout, null);
}
final TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
Button addBtn = (Button)view.findViewById(R.id.add_btn);
myLayout=(LinearLayout)view.findViewById(R.id.lnear_momo);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return view;
}
}
第一个xml布局是
<?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/list_item_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="Delete" />
<Button
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/delete_btn"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="Add" />
</RelativeLayout>
,第二个xml布局是
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#b2b3b7"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.efas.admin.efasrestaurant.Items_momo"
android:id="@+id/relate_momo">
<LinearLayout
android:orientation="vertical"
android:layout_width="500dp"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/linearLayout">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/momo_list" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/linearLayout"
android:layout_toEndOf="@+id/linearLayout"
android:id="@+id/lnear_momo"></LinearLayout>
</RelativeLayout>
我想自动添加textview,因为我点击了listL中的listview中每行的添加按钮,其id为lnear_momo,并将listview的那行中的文本传递给该textview。
答案 0 :(得分:1)
首先获取linearlayout的实例:
private ArrayList<String> list =new ArrayList<String>();
private Context context;
private LinearLayout ll;
public MoMoCostumAdapter(ArrayList<String> list,Context context, LinearLayout ll){
this.list = list;
this.context = context;
this.ll = ll;
}
接下来,在添加按钮上单击侦听器,按如下方式添加textview:
TextView t = new TextView(context);
// Add text, color, size, etc for textview
ll.addView(t); // Add textview to your linearlayout.
答案 1 :(得分:0)
您发布的班级名称仅在此处或您的代码中有所不同......
MyCostumAdapter adapter = new MyCostumAdapter(list, this);
和
public class MoMoCostumAdapter extends BaseAdapter implements ListAdapter
答案 2 :(得分:0)
好的我已经更改了一些代码并添加了一个额外的布局,您可以在..
之后删除<强> Items_momo.java 强>
public class Items_momo extends Activity {
int counter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_items_momo);
ArrayList<String> list = new ArrayList<String>();
list.add("Veg Steam Momo");
list.add("Veg C-Momo");
list.add("Veg Fry Momo");
list.add("Veg Kothey Momo");
list.add("Buff Steam Momo");
list.add("Buff Fry Momo");
list.add("Buff Kothey Momo");
list.add("Buff C-Momo");
list.add("Chicken Steam Momo");
list.add("Chicken Fry Momo");
list.add("Chicken Kothey Momo");
list.add("Chicken C-Momo");
list.add("Paneer Steam Momo");
list.add("Paneer Fry Momo");
list.add("Paneer Kothey Momo");
list.add("Paneer C-Momo");
CustomAdapter Adapter = new CustomAdapter(list,this);
ListView lView = (ListView) findViewById(R.id.momo_list);
lView.setAdapter(Adapter);
}
public class CustomAdapter extends BaseAdapter{
private ArrayList<String> list =new ArrayList<String>();
private Context context;
public CustomAdapter(ArrayList<String> list,Context context){
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@Override
public long getItemId(int pos) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.group_items, parent,false);
}
TextView listItemText = (TextView)view.findViewById(R.id.text);
listItemText.setText(list.get(position));
final String text = list.get(position);
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
Button addBtn = (Button)view.findViewById(R.id.add_btn);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter=0;
counter++;
LinearLayout lv=(LinearLayout)findViewById(R.id.lnear_layout);
lv.addView(createNewTextView(text));
}
public TextView createNewTextView(String text) {
final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(context);
textView.setLayoutParams(lparams);
textView.setMovementMethod(new ScrollingMovementMethod());
textView.setText("Added Item " + text + " Quantity = " + counter);
return textView;
}
});
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return view;
}
}
<强> activity_main.xml中强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relate_momo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b2b3b7"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.efas.admin.efasrestaurant.Items_momo" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="400dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ListView
android:id="@+id/momo_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/linearLayout"
android:layout_toRightOf="@+id/linearLayout"
android:orientation="vertical" >
<TextView
android:id="@+id/lnear_momo"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
</LinearLayout>
</RelativeLayout>
<强> main.xml中强>
<?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/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="Delete" />
<Button
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/delete_btn"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="Add" />
</RelativeLayout>
<强>输出强>