• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps When click an item ,display the detail for belonging item alone in android listview

krishnaveni

Well-Known Member
Dec 16, 2011
158
0
chennai
Hi.,
I have to develop one android application using fragment.

Here i need the o/p like :

Have to display category name on first fragment on horizontal listview.
Have to display article names of first category on 2nd fragment default .But if i have to click any category on first fragment means have to display article names for belonging category.
How can i develop these ????

Here i have to displayed category name on first fragment on horizontal listview.its done.

But i can 't do the next step..

please give me solution for these ????

I have used following code...

main.xml:

[HIGH]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:eek:rientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearlayout01"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#ccc"
android:layout_weight="1"
android:eek:rientation="vertical">
<fragment android:name="com.example.notch.MainActivity"
android:id="@+id/frag_1"
android:layout_width="fill_parent"
android:layout_height="50dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearlayout02"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#eee"
android:eek:rientation="vertical">
<fragment android:name="com.example.notch.SubCate"
android:id="@+id/frag_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>
[/HIGH]

Home.java:


[HIGH]
public class Home extends FragmentActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
[/HIGH]

MainActivity.java:


[HIGH]
public class MainActivity extends Fragment {
static final String URL = "http://dev2.mercuryminds.com/webservices/new_feed_articls.xml";

static String KEY_CATEGORY = "Categories";

static final String KEY_TITLE = "Category";
LazyAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main,
container, false);


final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(KEY_TITLE);


// looping through all song nodes &lt;song&gt;
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String> ();
Element e = (Element) nl.item(i);
map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute("name"));

// map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
songsList.add(map);
}
HorizontalListView list = (HorizontalListView) view.findViewById(R.id.listView1);
adapter = new LazyAdapter(getActivity(), songsList);
list.setAdapter(adapter);

list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = songsList.get(position);
Intent in = new Intent();
in.setClass(getActivity(), SubCate.class);
// in.putExtra(KEY_TITLE, map.get(KEY_TITLE));

startActivity(in);
}
});
return view;
}

}

[/HIGH]

SubCate.java:

[HIGH]
public class SubCate extends Fragment {
String mTitle,URL;


static String KEY_CATEGORY = "Articles";

static final String KEY_TITLE = "article";

ListViewCustomAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle b = getActivity().getIntent().getExtras();

View view = inflater.inflate(R.layout.subcate,
container, false);


//mTitle = b.getString(KEY_SUBCATE);

URL="http://dev2.mercuryminds.com/webservices/new_feed_articls.xml";

final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(KEY_TITLE);

for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute("title"));
songsList.add(map);
}
GridView lview3 = (GridView) view.findViewById(R.id.title);
adapter = new ListViewCustomAdapter(getActivity(), songsList);
lview3.setAdapter(adapter);
lview3.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = songsList.get(position);
Intent in = new Intent();
in.setClass(getActivity(), Catalogue.class);

in.putExtra(KEY_TITLE, map.get(KEY_TITLE));

startActivity(in);
}

});
return view;

}

}
[/HIGH]
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones