我在GridView
内使用自定义ScrollView
。我知道这不是正确的方法。我没有任何其他方法来完成我的应用程序布局。
我想在item id == row_id
中获取GridView
数据库,但它不会返回正确的ID。而不是给row_id
它给了我与数据库row_id
不匹配的项的位置ID
我知道适配器中的位置id是row_id
,我不知道为什么id与数据库不匹配。
以下是我在布局
上使用的GridView
<info.androidhive.slidingmenu.MyGridView
android:id="@+id/gridview11"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="278dp"
android:numColumns="2"
android:verticalSpacing="2dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:isScrollContainer="false"
android:focusable="false"
android:focusableInTouchMode="false"
/>
这是自定义MyGridview
代码。我在Github
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;
/**
* ScrollViewの中のGridViewでも高さを可変にする<br>
* https://stackoverflow.com/questions/8481844/gridview-height-gets-cut
*/
public class MyGridView extends GridView
{
boolean expanded = false;
public MyGridView(Context context)
{
super(context);
}
public MyGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
最后,onlcikListener
代码无法正常运行
try {
// OnClick
best_product_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get position index of item here.
String indexid = String.valueOf(position);
Log.d("Index of Selected ", indexid);
// calling function
globalVariable.product_id = position;
((MainActivity)getActivity()).product();
}
});
}
catch (Exception e)
{
Log.d("Error BestPrduct View","" + e);
}
这是Adapter类
package info.androidhive.slidingmenu.AdpaterClass;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.HashMap;
import info.androidhive.slidingmenu.R;
import info.androidhive.slidingmenu.library.Function_List;
import info.androidhive.slidingmenu.library.Global_JsonTags;
import info.androidhive.slidingmenu.library.Global_Variable;
/**
* Created by clint19 on 1/21/2016.
*/
public class Best_Product extends BaseAdapter {
View gridView;
private Context context;
/* private DisplayImageOptions options;*/
private LayoutInflater inflater;
// josn vaules from json file library
Global_JsonTags jsontags = new Global_JsonTags();
Function_List lib_function = new Function_List();
Global_Variable globalVariable = new Global_Variable();
private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();
public Best_Product(Context c,ArrayList<HashMap<String, String>> json_value) {
// TODO Auto-generated method stub
context = c;
MyArr = json_value;
}
public int getCount() {
// TODO Auto-generated method stub
Log.d("size of item", "" + MyArr.size());
return MyArr.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
TextView product_name;
TextView product_price;
ImageView imageView;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (view == null) {
view = new View(context);
view = inflater.inflate(R.layout.home_best_collection, parent, false);
holder = new ViewHolder();
assert view != null;
imageView = (ImageView) view.findViewById(R.id.imageView26);
// holder.imageView = (ImageView) view.findViewById(R.id.imageView26);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
product_name = (TextView) view.findViewById(R.id.textView21);
product_price = (TextView) view.findViewById(R.id.textView23);
String name = MyArr.get(position).get("product_name");
int namelength = name.length();
if(namelength >= 33)
{
product_name.setText(MyArr.get(position).get("product_name").substring(0, 29) + "...");
}
else{product_name.setText(MyArr.get(position).get("product_name"));}
product_price.setText(MyArr.get(position).get("product_price"));
String product_status = MyArr.get(position).get("product_status");
if(product_status.equals("false"))
{
Button add_to_cart = (Button) view.findViewById(R.id.add_to_cart1);
add_to_cart.setVisibility(View.GONE);
View background_height = (View) view.findViewById(R.id.bestproduct1);
}
else
{
Button add_to_cart = (Button) view.findViewById(R.id.add_to_cart1);
add_to_cart.setVisibility(View.VISIBLE);
}
Log.d("setting up text", "setting up text");
Picasso.with(context)
.load(MyArr.get(position).get("product_image"))
.placeholder(R.drawable.ic_stub) // optional
.error(R.drawable.ic_error) // optional
.into(imageView);
//view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
return view;
}
static class ViewHolder {
ImageView imageView;
ProgressBar progressBar;
}
}
我想从2小时内获得身份但没有运气。 我关注了这些问题How to add gridview setOnItemClickListener和Android gridview OnItemClickListener以及此问题Get ID of View in GridView 可能更多。
谁能告诉我有什么问题。
答案 0 :(得分:1)
我试图获取GridView数据库的项目ID == row_id但是它 不正确的身份
这是因为您没有正确覆盖getItemId
中的Adapter
。假设您为适配器提供的数据集由bean类组成,您需要做的是覆盖getItemId(int position)
,它返回long
,并让它返回正确的id。 E.g。
@Override
public long getItemId(int position) {
return getItem(position).getId();
}
正确的值作为long id
的最后一个参数onItemClick
传递。
如果你是CursorAdapter
(或其具体实现的一个)的子类,你应该,因为你有一个基于游标的解决方案,
你不需要覆盖它。 CursorAdapter
已经返回了位置光标的_id
列的值。
修改强>
@Override
public long getItemId(int position) {
HashMap<String, String> mapAtPostion = MyArr.get(position);
return Long.valueOf(mapAtPostion.get("product_id"));
}
答案 1 :(得分:1)
您正在获取该项目的位置,因为这是您从方法inside 1st if ,total= 0
after making false total=1
after making false total=1
after making false total=1
inside 3rd if ,total= 1
true true true
false true true
false true false
返回的内容。您需要从该方法返回数据库getItemId
。
像这样改变你的方法
row_id
答案 2 :(得分:-1)
@Override
public long getItemId(int position) {
return position;
}
CustmeAdapter示例
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}