我正在做一个培训计划,其中有一个可以回答是或否的问题列表。根据答案文本,问题会改变颜色。结果应显示在单独的活动中。我建议使用片段来保存文本问题的颜色但我有一个错误二进制XML文件行#22:错误膨胀类片段
以下是代码:
MainActivity:
openconnection()
cmd = New SqlCommand("UPDATE products SET quantity = '" + TextBox3.Text + "' WHERE bar_code = '" & ComboBox1.Text & "'", conn)
For Each i As ListViewItem In
ListView1.SelectedItems
cmd.Parameters.AddWithValue("@quantity", Val(TextBox5.Text) + Val(TextBox3.Text))
cmd.Parameters.AddWithValue("@barcode", TextBox3.Text)
ListView1.Items.Remove(i)
ListView1.Items.Remove(i)
dr = cmd.ExecuteReader()
Next
End Sub
RVAdapter:
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// public static String TAG = "MYLOG_main_activity";
public static String KEY = "KEY";
RecyclerView mRecyclerView;
LinearLayoutManager mLinearLayoutManager;
Context mContext;
Button mResult;
public static int[] iResult;
public static void MyReceiver(int[] result)
{
iResult = result;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null)
{
iResult = savedInstanceState.getIntArray(KEY);
}
mResult = (Button) findViewById(R.id.result);
mResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View asv) {if(iResult != null) {
/* Intent intent1 = new Intent("Intent");
intent1.putExtra("message", iResult);
intent1.setAction("broadcast");
sendBroadcast(intent1);
*/
Intent intent = new Intent(MainActivity.this, Result.class);
startActivity(intent);
Result.receiver(iResult);
}
}
});
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mLinearLayoutManager = new LinearLayoutManager(mContext);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
ArrayList<Ask> asks = new ArrayList<>();
asks.add(0,new Ask(0,R.drawable.image1,R.string.one));
asks.add(1,new Ask(1,R.drawable.image2,R.string.two));
asks.add(2,new Ask(2,R.drawable.image3,R.string.three));
asks.add(3,new Ask(3,R.drawable.image4,R.string.four));
asks.add(4,new Ask(4,R.drawable.image5,R.string.five));
//iResult = new int[asks.size()];
RVAdapter adapter = new RVAdapter(asks);
mRecyclerView.setAdapter(adapter);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putIntArray(KEY, iResult);
}
}
TextFragment:
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by 1 on 2.09.2016.
*/
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.CardViewHolder>{
public static final String TAG = "MYLOG_RVAdapter";
List<Ask> asks;
int[] iResult;
RVAdapter(List<Ask> asks) {
this.asks = asks;
iResult = new int[asks.size()];
for (int i = 0; i < asks.size(); i++) {
iResult[i] = 0;
}
}
@Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false); //На эту строку ругается
Log.d(TAG,"onCreateViewHolder");
return new CardViewHolder(v);
}
@Override
public int getItemCount() {
return asks.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
Log.d(TAG,"onAttachedToRecyclerView");
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public void onBindViewHolder(final CardViewHolder holder, final int position) {
holder.mButtonNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.mTextView.setBackgroundResource(R.color.No);
iResult[position] = 2;
MainActivity.MyReceiver(iResult);
}
});
holder.mButtonYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.mTextView.setBackgroundResource(R.color.Yes);
iResult[position] = 1;
MainActivity.MyReceiver(iResult);
}
});
switch (position) {
case 0:
holder.mTextView.setText(R.string.one);
holder.mImageView.setImageResource(R.drawable.image1);
break;
case 1:
holder.mTextView.setText(R.string.two);
holder.mImageView.setImageResource(R.drawable.image2);
break;
case 2:
holder.mTextView.setText(R.string.three);
holder.mImageView.setImageResource(R.drawable.image3);
break;
case 3:
holder.mTextView.setText(R.string.four);
holder.mImageView.setImageResource(R.drawable.image4);
break;
case 4:
holder.mTextView.setText(R.string.five);
holder.mImageView.setImageResource(R.drawable.image5);
break;
}
Log.d(TAG,"onBindViewHolder");
Log.d(TAG, String.valueOf(position));
}
public static class CardViewHolder extends RecyclerView.ViewHolder {
ImageView mImageView;
TextView mTextView;
CardView mCardVew;
Button mButtonYes;
Button mButtonNo;
public CardViewHolder(View itemView) {
super(itemView);
mButtonNo = (Button) itemView.findViewById(R.id.No);
mButtonYes = (Button) itemView.findViewById(R.id.Yes);
mTextView = (TextView) itemView.findViewById(R.id.textView);
mImageView = (ImageView) itemView.findViewById(R.id.imageView);
mCardVew = (CardView) itemView.findViewById(R.id.cardView);
Log.d(TAG,"CardViewHolder");
}
}
}
activity_main:
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by 1 on 04.10.2016.
*/
public class TextFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
View rootView =
inflater.inflate(R.layout.fragment1, container,false);
return rootView;
}
}
项:
<?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="match_parent"
tools:context="com.vkramarenko.myapplication.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result"
android:id="@+id/result"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/Yes"
android:layout_alignParentEnd="true" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"
android:layout_above="@+id/result">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
片段1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cardView"
card_view:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no"
android:id="@+id/No"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.vkramarenko.myapplication.TextFragment"
android:id="@+id/textFragment"
android:layout_above="@+id/No"
android:layout_alignEnd="@+id/No" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/yes"
android:id="@+id/Yes"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/No" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/image1"
android:contentDescription=""
android:layout_above="@+id/textFragment"
android:layout_alignEnd="@+id/No" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
答案 0 :(得分:1)
无法使用xml添加片段。
您需要使用片段管理器通过在活动或其他片段中执行片段事务来添加它。
在您的示例中,不必使用片段,因为它只包含单个TextView。
您可以直接使用TextView替换项目xml中的片段标记。
答案 1 :(得分:0)
所以几周后我发现了问题。在更新到Sceneform SDK 1.16时,你们发生了重大变化。较小版本的碰撞,但是会导致我在问题中提及的那些崩溃。解决方案是:
另请参阅我的博客文章:https://csaba.page/blog/sceneform-breaking-change.html