使用自定义列表视图

时间:2012-03-07 04:54:32

标签: java android android-listview

我创建了一个自定义列表视图,它扩展了ListActivity并且工作正常。但是我想在我的主要活动中放置一个ListView来扩展Activity。我怎么能这样做?

例如,我想在我的屏幕的一部分中放置一个自定义ListView,而不是仅由一个扩展ListActivity的ListView填充的屏幕。

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

这是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();
  }
 }

这很好但我只能在屏幕上看到一个列表视图。我想把这个自定义Listview放在我的主要活动中。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

你喜欢以下: -

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

答案 1 :(得分: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;

    }

}
}