Hi!
I was faced a problem to generate dynamic horizontal list view in fragment.I search the help with Google too. But their is no more help to fulfill my need.
The Problems are :
- Their is no default horizontal list view in Android (the default list view is vertical),
- Even though we implement the customize Horizontal list view, It is too heard to implementing with Fragment,
- Implementing the "on click" Listener on the dynamic items too.
I try and found the solution for the problem and happy to share with developers.
ArrayList for my business class
ArrayList<CatagoryB> clist;
return the view for the list and execute the Async class
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_list_of_category,container);
clist = new ArrayList<CatagoryB>();
new CatagoryList().execute();
return view;
}
store the list item into the array list
clist.add(new CatagoryB(lcid, lcname));
initialize the dynamic text view
final TextView myid[] = new TextView[clist.size()];
final TextView mynm[] = new TextView[clist.size()];
initialize the layout too
LinearLayout lout = (LinearLayout) getActivity().findViewById(
R.id.catlout);
create dynamic object for the textview
myid[i] = new TextView(getActivity());
mynm[i] = new TextView(getActivity());
set it on layout
myid[i].setLayoutParams(new LayoutParams(0, 0));
assign the parameters
myid[i].setText(clist.get(i).getCid());
mynm[i].setText(clist.get(i).getCname());
assign the id
mynm[i].setId(i);
myid[i].setId(i);
set onclick listener
mynm[i].setOnClickListener(new OnClickListener()
can get the relevent item by id
int iid = v.getId();
String actual_id = clist.get(iid).getCid();
Here is over view of my code
//@SuppressLint("NewApi")
@SuppressLint("ResourceAsColor")
public class CategoryList extends Fragment {
private CategoryToSubCategory connecter;
String Cat_Url = "onsite link for your database query";
ArrayList<CatagoryB> clist;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_list_of_category,
container);
clist = new ArrayList<CatagoryB>();
new CatagoryList().execute();
return view;
}
public interface CategoryToSubCategory {
public void List_SubCategory(int item);
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
if (activity instanceof CategoryToSubCategory) {
connecter = (CategoryToSubCategory) activity;
} else {
throw new ClassCastException();
}
}
class CatagoryList extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... arg0) {
JSONParser jparse = new JSONParser();
List<NameValuePair> pair = new ArrayList<NameValuePair>();
String JSon = jparse.makeHttpRequest(Cat_Url, "GET", pair);
try {
JSONArray jaray = new JSONArray(JSon);
if (jaray != null) {
Log.d("JSon", JSon);
for (int i = 0; i < jaray.length(); i++) {
JSONObject j = jaray.getJSONObject(i);
String lcid = j.getString("categorykey");
String lcname = j.getString("stockcategoryname");
clist.add(new CatagoryB(lcid, lcname));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
final TextView myid[] = new TextView[clist.size()];
final TextView mynm[] = new TextView[clist.size()];
LinearLayout lout = (LinearLayout) getActivity().findViewById(
R.id.catlout);
for (int i = 0; i < clist.size(); i++) {
myid[i] = new TextView(getActivity());
mynm[i] = new TextView(getActivity());
myid[i].setLayoutParams(new LayoutParams(0, 0));
mynm[i].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
myid[i].setTextSize(30);
mynm[i].setTextSize(30);
mynm[i].setPadding(5, 1, 5, 1);
myid[i].setTextColor(Color.WHITE);
mynm[i].setBackgroundResource(R.drawable.b);
mynm[i].setTextColor(Color.WHITE);
myid[i].setText(clist.get(i).getCid());
mynm[i].setId(i);
myid[i].setId(i);
mynm[i].setText(clist.get(i).getCname());
lout.addView(myid[i]);
lout.addView(mynm[i]);
mynm[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int iid = v.getId();
String actual_id = clist.get(iid).getCid();
connecter.List_SubCategory(Integer.valueOf(actual_id));
}
});
}
}
}
}
Layout xml file for the Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:widget="http://schemas.android.com/apk/res-auto"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/hesdbg" >
<LinearLayout
android:id="@+id/catlout"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
No comments:
Post a Comment