Listview顶部的Android徽标

时间:2014-01-08 06:07:06

标签: android xml listview

我有一个从服务器获取数据并添加到数组列表的活动。我有一个xml布局“list_item”。我想在该列表的顶部设置徽标。但是,如果我在该xml文件上添加了徽标,则徽标会显示每个列表项。但我只需要一个徽标(在列表的顶部)。

以下是活动:

public class CampaignActivity extends ListActivity {

Button sub;
NotificationManager nm;
TextView campaign;
static final int uniqueId=12;
static InputStream is = null;

private static final String TAG_ID = "_id";
private static final String TAG_NAME = "name";
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    StrictMode.ThreadPolicy policy = new StrictMode.
            ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy); 
    //setContentView(R.layout.campaign);

    //campaign = (TextView) findViewById(R.id.textView1);
    //sub = (Button) findViewById(R.id.button1);

    //new CreateUser().execute();

    //sub.setOnClickListener(this);
    //nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    //nm.cancel(uniqueId);

            DefaultHttpClient httpClient = new DefaultHttpClient();
            String url = "http://54.228.199.162/api/campaigns";
            try {
                String res;
                HttpGet httpget = new HttpGet(url);
                HttpResponse httpResponse = httpClient.execute(httpget);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "UTF-8"), 8000);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                res = sb.toString();
                Log.d("um1", res);
                JSONArray jsonArray = new JSONArray(res);
                //campaign.setText(res);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String id = jsonObject.getString(TAG_ID);

                    String utfid = URLDecoder.decode(id, "UTF-8");
                    Constant.idusr = utfid;
                    //String name = jsonObject.getString(TAG_NAME);
                   // String utfname = URLDecoder.decode(name, "UTF-8");
                    String utfname = URLDecoder.decode(jsonObject.getString(TAG_NAME), "UTF-8");
                    Log.d("IDDDDDDD", id);
                    Log.d("Nameeeeeee", utfname);
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, utfid);
                    map.put(TAG_NAME, utfname);
                    //Object contactList;
                    contactList.add(map);
                  }
                HttpResponse httpresponse = httpClient.execute(httpget);
                int responsecode = httpresponse.getStatusLine().getStatusCode();
                Log.d("responsenummmm", "um11111"+responsecode);
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            };
            //this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, contactList));

            ListAdapter adapter = new SimpleAdapter(this, contactList,
                    R.layout.list_item,
                    new String[] { TAG_NAME,TAG_ID}, new int[] {
                            R.id.label, R.id.id});

            setListAdapter(adapter);

            // selecting single ListView item
            ListView lv = getListView();

            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // getting values from selected ListItem
                    String idof = ((TextView) view.findViewById(R.id.id)).getText().toString();
                    String nameof = ((TextView) view.findViewById(R.id.label)).getText().toString();

                    // Starting new intent
                    Intent in = new Intent(getApplicationContext(), DonateActivity.class);
                    in.putExtra(TAG_NAME, nameof);
                    in.putExtra(TAG_ID, idof);
                    startActivity(in);
                }
            });
        }
}

这是xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingTop="10dip" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/firstscreenimage" />

<TextView
    android:id="@+id/id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/relativeLayout1"
    android:text="bd" />

<TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/id"
    android:text="bd"
    android:textColor="#C20000"
    android:textSize="14sp"
    android:textStyle="bold" />

</LinearLayout>

这是我的问题:

problem list

但是徽标应该是第一个不是每个列表项的徽标。请帮帮我。

8 个答案:

答案 0 :(得分:1)

请删除ImageView并将其写出您使用的线性布局。正如你所说,只有你需要这个imageview。除此之外,你可以做的是制作一个自定义适配器和自定义列表视图并修复它,只有位置索引0你需要显示imageview。

答案 1 :(得分:1)

将徽标作为标题视图添加到列表中。试试以下 -

LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, myListView, false);
myListView.addHeaderView(header, null, false);

在标题布局中,您可以为徽标创建imageView

答案 2 :(得分:1)

这是另一种方式:

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_plusone_medium_off_client" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

答案 3 :(得分:1)

在自定义适配器的get方法中,这样做

假设您的徽标已在某些ImageView上分配,并且您的ImageView ID为img

默认情况下,在xml上将Logo的Visiblity设置为GONE

所以你可以在运行时使它Visiblity VISIBLE如下所示,对于列表视图的第一个位置,它非常简单

 if(position==0){

    img.setVisiblity(View.VISIBLE);
 }

答案 4 :(得分:0)

试试这段代码:

//创建类扩展视图示例MyHeader

MyHeader header = new MyHeader();

listview.addHeader(header );

答案 5 :(得分:0)

这可能对你有帮助....

创建另一个布局(即xml文件)并首先插入图像视图然后列出视图,现在在create方法中调用此布局并使用哈希映射绑定上面解释的xml / layout文件。

注意:不要忘记从上面说明的布局中删除图像视图。

答案 6 :(得分:0)

看看下面的SO链接,有很多很好的链接来实现listview标题。

How to add Header to List View In Android

答案 7 :(得分:0)

以这种方式尝试:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingTop="10dip" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"

     android:visibility="gone"

    android:src="@drawable/firstscreenimage" />

<TextView
    android:id="@+id/id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/relativeLayout1"
    android:text="bd" />

<TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/id"
    android:text="bd"
    android:textColor="#C20000"
    android:textSize="14sp"
    android:textStyle="bold" />    
</LinearLayout>

之后在代码中以这种方式找到第一个列表视图的位置:

if(****find the list*** position==0){

img.setVisiblity(View.VISIBLE);

}