I have list view:
<ListView android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And I add some items to it:
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < items.length(); ++i) {
try {
list.add(items[i].getString("title"));
} catch (JSONException e) {
e.printStackTrace();
}
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
But now I want to add some TextViews
(at example if(i==5)
) to the list, like a label. It should not be clickable. How can I add it?
listview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3)
{
long pos = arg0.getItemIdAtPosition(arg2);
if(pos != 5)
{
//add textview
}
else
{
//nothing
}
}
});
You should use a custom row layout instead of android.R.layout.simple_list_item_1
, for example a row.xml you have one text view whose visibility is set to gone by default:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/nonclickable_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="@+id/normal_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Put it in res/layout/ folder, then call R.layout.row
instead of android.R.layout.simple_list_item_1
when you create your StableArrayAdapter. In the getView() method
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, parent,false);
}
if(int position==5) {
TextView tv = (TextView)convertView.findViewById(R.id.nonclickable_text);
tv.setVisibility(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.