简体   繁体   中英

Android logo on top of Listview

I have a activity which takes data from server and add to a array list. I have an xml layout "list_item". I want to set the logo on top of that list. But if I added logo on that xml file, the logo shows with every list item. But I need the logo only one(on top of the list).

Here is the activity:

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

Here is the 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>

Here is my problem:

问题清单

But the logo should be the first one not with every list item. Please help me.

Please remove ImageView and write it out of the linear layout which you used. as you said that only once you need this imageview. beside this what you can do is make a custom adapter and custom listview and fix it that only for position index 0 you need to show imageview.

Add your logo as a header view to the list. Try following-

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

In the header layout you can create an imageView for your logo.

this is another way :

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

On your get View method of custom adapter do like this

Suppose your logo is assigned on some ImageView and your ImageView id is img

on xml by default put Visiblity of Logo to GONE

so you can make it's Visiblity VISIBLE at run time as below, for the first position of list view, it's very simple

 if(position==0){

    img.setVisiblity(View.VISIBLE);
 }

try this code :

//Create a class extend View example MyHeader

MyHeader header = new MyHeader();

listview.addHeader(header );

This may help you....

Create another layout(ie xml file) and insert image view first and then list view , now in on create method call this layout and by using hash map bind the above explained xml/layout file.

Note : Don't forget to remove image view from above explained layout.

Have a look at the SO link below, lots of nice links to implement listview Headers.

How to add Header to List View In Android

Try in this manner:

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

After that in your code find the position of first list view in this manner:

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

img.setVisiblity(View.VISIBLE);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM