Reputation: 165
So I tried all the other post and it's not working, can anyone tell me why the button gets pu into the listview?
The code for my layout is:
<FrameLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#5eff6c"
tools:context=".FriendActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="@+id/listView"
android:layout_gravity="center_horizontal|top"
android:background="#5eff6c" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_gravity="center_vertical"
android:textSize="16dp"
android:textIsSelectable="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="center" />
</FrameLayout>
Now it looks like this: https://i.sstatic.net/RU1Fb.png
I want this: https://i.sstatic.net/m7b6j.png
Edit: Java code:
public class FriendActivity extends ListActivity {
private static final String TAG_NAME = "name";
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
ArrayList<HashMap<String, String>> names = null;
try {
String test = new sendData().execute().get();
JSONArray jArray = new JSONArray(test);
Log.d("NOOO", String.valueOf(test));
names = new ArrayList<HashMap<String, String>>();
JSONObject object;
for (int i = 0; i < jArray.length(); i++) {
object = jArray.getJSONObject(i);
Log.d("YES", String.valueOf(object));
String name = object.getString(TAG_NAME);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_NAME, name);
names.add(map);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, names,
R.layout.activity_friend,
new String[]{TAG_NAME}, new int[]{
R.id.name});
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
}
Thanks for the help
Upvotes: 0
Views: 103
Reputation: 11211
The problem is that you are passing that layout to each of your ListView
's layout here:
ListAdapter adapter = new SimpleAdapter(this, names,
R.layout.activity_friend,
new String[]{TAG_NAME}, new int[]{
R.id.name});
And what you want to do is to set that layout this way:
setContentView(R.layout.activity_friend);
That way, it will be set to the activity and your activity will look like what you made in your xml.
Also make another simple layout with a single TextView
in it and pass that to your adapter, this way your ListView
will have also a text in it. Or you could use this layout from android: android.R.layout.simple_list_item_1
.
Also for the button gravity I think you should use:
android:layout_gravity="center_horizontal|bottom"
And don't forget to remove the textview from the layout you posted above as you won't need it anymore..
Upvotes: 1