[英]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".
我有一个xml布局“ 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.
但是,如果我在该xml文件上添加了徽标,则该徽标会显示在每个列表项中。 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: 这是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. 请删除ImageView并将其写出您使用的线性布局。 as you said that only once you need this imageview.
如您所说,只有一次您需要此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.
除此之外,您可以做的是制作一个自定义适配器和自定义列表视图,并对其进行修复,使其仅对于位置索引0才需要显示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. 在标题布局中,您可以为徽标创建一个
imageView
。
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 在自定义适配器的get View方法上,像这样
Suppose your logo is assigned on some ImageView
and your ImageView
id is img 假设您的徽标已分配到某些
ImageView
并且您的ImageView
ID为img
on xml by default put Visiblity of Logo to GONE 默认情况下,在xml上将徽标的可见性设置为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 因此,您可以在运行时按如下所示将其
Visiblity VISIBLE
,对于列表视图的第一个位置,这非常简单
if(position==0){
img.setVisiblity(View.VISIBLE);
}
try this code : 试试这个代码:
//Create a class extend View example MyHeader //创建一个类扩展视图示例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. 创建另一个布局(即xml文件),然后首先插入图像视图,然后插入列表视图,现在在create方法中调用此布局,并通过使用哈希映射绑定上述的xml / layout文件。
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. 看看下面的SO链接,有很多不错的链接来实现listview标头。
How to add Header to List View In Android 如何在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);
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.