0

I create a custom listview and it extend ListActivity and works fine. But I want to put a ListView in my main activity that extends Activity. How can I do this?

For example, I want to put a custom ListView in my one part of my screen not the screen filled by only ONE ListView that extends ListActivity.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="fill_parent"
android:layout_height="fill_parent">    
<TextView 
    android:id="@+id/lvresult"
    android:paddingTop="2dip"
    android:paddingBottom="3dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" /> 
  </LinearLayout>

and this is MyListActivity:

package Dic.proj.pkg;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MyListActivity extends ListActivity {
EditText et;
TextView tv;
TextView tvresult;
String resulttext;
ArrayList<String> mArrayList = new ArrayList<String>();
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };
    // Use your own layout
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.customlist, R.id.lvresult, values);
    setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    String item = (String) getListAdapter().getItem(position);
    Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
  }
 }

This works fine but I can see only one List view on screen. I want to put this custom Listview in my main activity. How can I do this?

1

2 Answers 2

1

you do like below:-

public class StoreListActivity extends Activity {
    private List<Store> mStores;
    private StoreAdapter mStoreAdapter;
    private ListView mListView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.store_list);
        mListView = (ListView) findViewById(R.id.store_listview);

            mStores = getTheStoresFromSomewhere();
        mStoreAdapter = new StoreAdapter(this, mStores);
        mListView.setAdapter(mStoreAdapter);
    }
Sign up to request clarification or add additional context in comments.

Comments

0
// roadmapscreen.xml  
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="#ffffff"
    android:layout_height="fill_parent"
   >

    <ListView  

      android:cacheColorHint="#00000000"  
 android:id="@+id/roadmaplist"  
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"  
android:listSelector="@android:color/transparent"
 android:divider="@null"/>  

</RelativeLayout>

//roadmap_list_item
<?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:background="#ffffff"



   >

        <TextView 
            android:id="@+id/text"
      android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
       android:text="Hello"
       android:textColor="#000000"
       android:layout_marginTop="12dp"
       android:layout_marginLeft="12dp"
       android:textStyle="bold"
       />

   </RelativeLayout>

//Main Activity
 public class RoadmapActivity extends Activity {
ListView lv;
ArrayList<String> catList;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
       setContentView(R.layout.roadmapscreen);
 catList=new ArrayList<String>();

       catList.add("Work");
       catList.add("Time");
       catList.add("Money");
       catList.add("Health");
       catList.add("Fun & Recreation");
       catList.add("Friends & Relatives");              
       catList.add("Spirituality & Growth");
       catList.add("Home & Housework");
       catList.add("Death & Dying");
       catList.add("Sexual Intimacy");
       catList.add("Marriage/Commitment");
       catList.add("Children");
       catList.add("Miscellaneous");

 lv=(ListView)findViewById(R.id.roadmaplist);
        lv.setAdapter(new CustomAdapter());
}
public class CustomAdapter extends BaseAdapter
{

    public int getCount() {
        // TODO Auto-generated method stub
        return catList.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder holder;
        LayoutInflater  inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        if (convertView == null) 
        {
            convertView=inflater.inflate(R.layout.roadmap_list_item, null);
            holder = new ViewHolder();


        }
        else          
        {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txt1=(TextView)convertView.findViewById(R.id.text);
         convertView.setTag(holder);
         holder.txt1.setText(catList.get(position));
         convertView.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        return convertView;
    }
    class ViewHolder {  

        TextView txt1;

    }

}
}

Comments

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.