2

I have two java class and two xml file for listview with buttons.I want to pass the data of Corresponding text in the row of listview to the linear layout.

here is the code of my first java class.

  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);
}


}

and the second class code is

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;
    }
  }

the first xml layout is

<?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>

and the second xml layout is

<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>

i want to add textview automatically as i click the add buttons in each row in listview in the LinearLayout whose id is lnear_momo and pass the text in that row of listview to that textview.

4
  • Use addView() to add dynamically. Commented Aug 10, 2015 at 10:04
  • can you plz post the code .. Commented Aug 10, 2015 at 10:07
  • is this both layout are on same activity? Commented Aug 10, 2015 at 11:00
  • Rather adding view dynamically make it static in your xml and hide initially, show it after click on particular position. It will be more easy. Commented Dec 25, 2015 at 11:33

3 Answers 3

1

First get the instance of your 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;
}

Next on your add button click listener, add your textview as follows:

TextView t = new TextView(context);
// Add text, color, size, etc for textview 
ll.addView(t); // Add textview to your linearlayout.
Sign up to request clarification or add additional context in comments.

2 Comments

I tested it. Its working. BTW, why are you setting listview's container width to 500dp? If you test it on smaller dimension phones, the listview covers almost full screen and you won't be able to see the textview.
its for a tablets its not working in mine ... help plz
0

Ok I have changes a bit of your code and added an extra layout which you can remove after..

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>

OutPut

enter image description here

4 Comments

thank you so much but i want to add a new textview every time i clicked a different items's button below the textview. how can i do that ???
If you got what you are looking for, you can mark as answer and sorry I didn't get your last question...
And by the way if you want to add textview you can replace textview from listview (containing textview) and add new position at on click listener.this will add new row to your listview with your data..
to remove textview you have to identify it...but as you have specified final object it will create new instance so you also refer to new object...i.e y u cannot remove it till activity is alive and it will keep on adding new one
0

Is the class name you have posted differ only here or in your code also...

MyCostumAdapter adapter = new MyCostumAdapter(list, this);

and

public class MoMoCostumAdapter extends BaseAdapter implements ListAdapter

3 Comments

ohh thats a silly mistake bu when i changed MycostumAdapter into MoMo customAdapter then it shows an error
i tried you code...you have used android:layout_alignParentTop="true" in both your layout..so further you on result showing linear layout might not showing..
MoMoCostumAdapter adapter = new MoMoCostumAdapter(list, this); // (list,this ) is not being correct this time ListView lView = (ListView) findViewById(R.id.momo_list); lView.setAdapter(adapter);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.