0

I need to output data from lines cm1 and cm2 in a ListView, every time when I press the button. Line should be shown in various TextView.

Code TEST.java:

public String cm1;
public String cm2;

public class TEST extends Activity {
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MyList = (ListView) this.findViewById(R.id.listStrings);
         setListAdapter();

        // button
        Button setup = (Button) this.findViewById(R.id.send);
        setup.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
        cm1 = "cm1text";
        cm2 = "cm2text";
        //xxxx
        }
        });
}

// adapter
private void setListAdapter() {
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    R.layout.row //idknow what im must writehere to set cm1 string to cm1text tv
    List.setAdapter(adapter);
    }

Code TEST.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<ListView
    android:id="@+id/listStrings"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1" />

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send" />
</LinearLayout>

Code row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:id="@+id/cm1tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/cm2tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

Should look ListView after 2 clicks:

cm1
cm2
cm1
cm2
test2
  • 11
  • 2
  • I did a video similar to what you asked, may helpe. http://www.youtube.com/watch?v=61WvMzaihwU (Is is a simple app, one EditText, Button and ListView. When press button add text to the list). It may be only one text not two, but you may be able to adjust. – soynerdito Dec 19 '12 at 13:42
  • can u upload sources? pls man :) – test2 Dec 19 '12 at 13:46

1 Answers1

0

Maybe what you should/can do is have this;

String cm = cm1 + "\n" + cm2

Which should output;
cm1
cm2

Then you change the xml to just have one textview and the adapter code as follows;

ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.row, R.id.textViewCM, listItems);
yourListView.setAdapter(aa);

Then, whenever you press the button;

// ...code to initialise/change cm
listItems.add(cm);
aa.notifyDataSetChanged(); // notifies listView to update it's view

That should do what you're after unless you've got to have the two individual textViews, in which case, you'll need to write a custom adapter, I've done a bit of a tutorial on my website if you follow that link.

Trent
  • 1,595
  • 15
  • 37
  • FizzBuzz on ur site its example of Custom adapter? Yes IM NEEDED TWO TV because im need to set their background and change text size and etc actions what im can do with simple TV. Thanx for answer bro! xD srry for my bad eng. Im try and later commit answer, wait for more maybe. – test2 Dec 19 '12 at 14:02
  • Correct, I give a fairly thorough run down of how to implement one. Probably a bit big to go on here as an answer. The custom adapter will be the answer to your troubles. – Trent Dec 19 '12 at 14:10
  • FizzBuzz maybe to avoid 2 TV u can show how to add one string but text in them (text on line and 2) is formatted like html or just color and bold.. if its possible.. im trying use standart TV props like underline in this example http://stackoverflow.com/questions/2394935/can-i-underline-text-in-an-android-layout but its dont working(used Html.fromHtml and String.format). – test2 Dec 19 '12 at 14:11
  • Try this example; http://stackoverflow.com/questions/8033316/to-draw-an-underline-below-the-textview-in-android/8033336#8033336 A custom adapter is pretty easy to implement though and is probably a better solution than one textview with multiple lines. – Trent Dec 19 '12 at 14:16
  • :) big thanx im try customadapter and this too. – test2 Dec 19 '12 at 14:18
  • Im post 1st comment in ur blog, about it) – test2 Dec 19 '12 at 20:18