List with edit text and text views

Put your problem here if it does not fit any of the other categories.

List with edit text and text views

Postby shailaja » Tue Mar 06, 2012 10:08 am

Hi all
I am trying to get a response from server and devide that into string arraylist. And print that response array list as a a list. I can print that list using list adapter. Now I need to add a text view and next to that an edit text box. under every list element. I did not get any solution for that. If some one have any idea, please guide me to do that.

Thanks
Shailaja.
shailaja
Developer
Developer
 
Posts: 40
Joined: Fri Aug 06, 2010 8:00 am

Top

Re: List with edit text and text views

Postby nuetronprotron » Tue Mar 06, 2012 10:43 am

Hi shailaja,
Sorry i didnot get what are u asking for,Are u asking to catch the response from server and display it or received response to show in a textview.can please be clear of what your requirements are.
nuetronprotron
Developer
Developer
 
Posts: 37
Joined: Tue Jan 10, 2012 10:22 am

Re: List with edit text and text views

Postby Phyll » Tue Mar 06, 2012 12:18 pm

Hi Shailaja,

You need to make your listview with a custom listadapter. That adapter uses a layout that has the widgets layed out the way you want them. This layout needs to be instantiated in the getview method of the listadapter. Here is an example:

A layout for a listview row (call_list_row.xml) that has two textviews and a checkbox:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/txtArea"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="1sp"
    android:weightSum="8">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="2">

    <TextView android:id="@+id/txtName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#ffffff"
        android:background="#00000000"
        />
    <TextView android:id="@+id/txtNumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textColor="#c0c0c0"
        android:background="#00000000"
        />   
   
</LinearLayout>   
   
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:paddingRight="0sp"
        android:layout_weight="6"
        />   

</LinearLayout>       


The listadapter getview method that takes care of instantiating the widgets:

Code: Select all
@Override
public View getView(int position, View convertView, ViewGroup parent) {
     View v = convertView;
     if (v == null) {
         LayoutInflater vi = (LayoutInflater)this.getContext().
             getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         v = vi.inflate(R.layout.call_list_row, null);
         }//inflater if convertView null

     //show it this way
     LinearLayout ll = (LinearLayout)v.findViewById(R.id.txtArea);
     TextView label = (TextView)v.findViewById(R.id.txtName);
     TextView number = (TextView)v.findViewById(R.id.txtNumber);

     String s = this.getItem(position).toString();
     String s1 = s.substring(0, 1);                 //get flag
     s = s.substring(2);                            //rest of string
     String s2 = s.substring(0,s.indexOf(","));     //get name
     s = s.substring(s.indexOf(",")+1,s.length());//get number
     if (selectedPos == position) ll.setBackgroundResource(R.drawable.call_list_selector);
         else ll.setBackgroundColor(Color.BLACK);
     label.setText(s2);
     number.setText(s);

     // Set onclick events here

     return(v);
}


Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: List with edit text and text views

Postby shailaja » Tue Mar 06, 2012 1:28 pm

Hi phyll,

Thanks for your reply.
What exactly i did is
The code i used to get response from my server and adding it into a list is
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static List<String> list = new ArrayList<String>();
  2. public void onCreate(Bundle savedInstanceState) {
  3.  
  4. ..........
  5. ..........
  6.  ok.setOnClickListener(new View.OnClickListener() {
  7.  
  8.          
  9.             public void onClick(View v) {
  10. String response = CustomHttpClient.executeHttpGet("MY_SERVER_URL_HERE");
  11.                                
  12.                             response = response.replace("(", "").replace(");","");
  13.                            
  14.                             JSONObject obj = new JSONObject(response);
  15.  
  16.                              
  17.                             JSONArray array = obj.getJSONArray("items");
  18.                             for(int i = 0 ; i < array.length() ; i++){
  19.                                 list2.add(edit1);
  20.                                 list.add("##"+i+"--"+array.getJSONObject(i).getString("No_")+"__ "+array.getJSONObject(i).getString("Description")
  21.                                                 );
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


So now my list having the array of strings.
next, to display the list in emulator following code i used
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent i = new Intent(getApplicationContext(), ListViewActivity.class);
  2.                             startActivity(i);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


in ListViewActivity i created an adapter and printed the list as follows
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, list));
  2.      
  3.           ListView lv = getListView();
  4.           lv.setTextFilterEnabled(true);
  5.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Here, list_item.xml file having a text view in it.

Up to now am able to display the text in a list view perfectly.
What my present requirement is..
under every list element(i.e we can assume as first element's next line, second element's next line.....) i want to create a text and right to that a edittext box.
Here i tried to add a xml layout having text view and a edit text. But did not get the out put.
If you have any idea, please share it.

Thanks
shailaja
shailaja
Developer
Developer
 
Posts: 40
Joined: Fri Aug 06, 2010 8:00 am

Re: List with edit text and text views

Postby nuetronprotron » Wed Mar 07, 2012 5:24 am

Hi shailaja,
So you need a textview and edittext in every listview element,if that is so then you have to use custom adapter .

So this is your listview structure will be listview_structure.xml
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/txtArea"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<TextView android:id="@+id/responseTextview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textColor="#ffffff"
        />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@+id/txtName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#0044ff"
        />
    <EditText android:id="@+id/txtNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#c0c0c0"
        />   
   
</LinearLayout>   


</LinearLayout>       


then create your own adapter like below

Code: Select all
public class MyAdapter extends BaseAdapter{
    private LayoutInflater inflater;
    private ArrayList<String> data;

    public MyAdapter(Context context, ArrayList<String> data){
    // Caches the LayoutInflater for quicker use
    this.inflater = LayoutInflater.from(context);
    // Sets the events data
    this.data= data;
    }

    public int getCount() {
        return this.data.size();
    }

    public String getItem(int position) throws IndexOutOfBoundsException{
        return this.data.get(position);
    }

    public long getItemId(int position) throws IndexOutOfBoundsException{
        if(position < getCount() && position >= 0 ){
            return position;
        }
    }

    public int getViewTypeCount(){
        return 1;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        String myText = getItem(position);           

        if(convertView == null){ // If the View is not cached
            // Inflates the Common View from XML file
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.listview_structure, null);
        }

     LinearLayout ll = (LinearLayout)convertView.findViewById(R.id.txtArea);
     TextView responseTextHolder = (TextView)convertView.findViewById(R.id.responseTextView);
     TextView name = (TextView)convertView.findViewById(R.id.txtName);
     EditText number = (EditText)convertView.findViewById(R.id.txtNumber);

   //set data to them
   responseTextHolder.setText(data.get(position));
   //your textview and editText can also be provided with data
       
        return convertView;
    }
}     



I hope this will help you
nuetronprotron
Developer
Developer
 
Posts: 37
Joined: Tue Jan 10, 2012 10:22 am

Re: List with edit text and text views

Postby shailaja » Wed Mar 07, 2012 5:45 am

Thanks nuetronprotron,
As you told, by using custom adapter i added those layout elements to the list.

Thanks
shailaja
shailaja
Developer
Developer
 
Posts: 40
Joined: Fri Aug 06, 2010 8:00 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 24 guests